From 1a0ffc5ace08472ef6042604ea5a7dc8a789c428 Mon Sep 17 00:00:00 2001 From: Dan Ungureanu Date: Wed, 1 Aug 2018 18:42:40 +0200 Subject: [PATCH] FEATURE: Added method to get multiple values at once from PluginStore. (#6225) --- app/models/plugin_store.rb | 10 ++++++++++ spec/models/plugin_store_spec.rb | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/app/models/plugin_store.rb b/app/models/plugin_store.rb index 72d9859e70e..3626141d533 100644 --- a/app/models/plugin_store.rb +++ b/app/models/plugin_store.rb @@ -10,6 +10,10 @@ class PluginStore self.class.get(plugin_name, key) end + def get_all(keys) + self.class.get_all(plugin_name, keys) + end + def set(key, value) self.class.set(plugin_name, key, value) end @@ -24,6 +28,12 @@ class PluginStore end end + def self.get_all(plugin_name, keys) + rows = PluginStoreRow.where('plugin_name = ? AND key IN (?)', plugin_name, keys).to_a + + Hash[rows.map { |row| [row.key, cast_value(row.type_name, row.value)] }] + end + def self.set(plugin_name, key, value) hash = { plugin_name: plugin_name, key: key } row = PluginStoreRow.find_by(hash) || PluginStoreRow.new(hash) diff --git a/spec/models/plugin_store_spec.rb b/spec/models/plugin_store_spec.rb index 27966c17247..fc53e3e390e 100644 --- a/spec/models/plugin_store_spec.rb +++ b/spec/models/plugin_store_spec.rb @@ -14,6 +14,11 @@ describe PluginStore do value == store.get(k) ? value : "values mismatch" end + def get_all(k) + value = PluginStore.get_all("my_plugin", k) + value == store.get_all(k) ? value : "values mismatch" + end + def remove_row(k) PluginStore.remove("my_plugin", k) store.remove(k) @@ -43,6 +48,18 @@ describe PluginStore do expect(get("hello")).to eq(nil) end + it "gets all requested values" do + set("hello_str", "world") + set("hello_int", 1) + set("hello_bool", true) + + expect(get_all(["hello_str", "hello_int", "hello_bool"])).to eq({ + "hello_str": "world", + "hello_int": 1, + "hello_bool": true, + }.stringify_keys) + end + it "handles hashes correctly" do val = { "hi" => "there", "1" => 1 }