FEATURE: Added method to get multiple values at once from PluginStore. (#6225)

This commit is contained in:
Dan Ungureanu 2018-08-01 18:42:40 +02:00 committed by Régis Hanol
parent 059862ed46
commit 1a0ffc5ace
2 changed files with 27 additions and 0 deletions

View File

@ -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)

View File

@ -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 }