From 680197200e54bcf8cb292b0fb6322b5aa6f684f1 Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Mon, 10 Mar 2014 10:22:59 +0100 Subject: [PATCH] Add support for arrays to PluginStore --- app/models/plugin_store.rb | 14 ++++++++++++-- spec/models/plugin_store_spec.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/app/models/plugin_store.rb b/app/models/plugin_store.rb index 0c31d02fdd4..640b61a2033 100644 --- a/app/models/plugin_store.rb +++ b/app/models/plugin_store.rb @@ -31,14 +31,24 @@ class PluginStore def self.determine_type(value) - value.is_a?(Hash) ? "JSON" : value.class.to_s + value.is_a?(Hash) || value.is_a?(Array) ? "JSON" : value.class.to_s + end + + def self.map_json(item) + if item.is_a? Hash + ActiveSupport::HashWithIndifferentAccess.new item + elsif item.is_a? Array + item.map { |subitem| map_json subitem} + else + item + end end def self.cast_value(type, value) case type when "Fixnum" then value.to_i when "TrueClass", "FalseClass" then value == "true" - when "JSON" then ActiveSupport::HashWithIndifferentAccess.new(::JSON.parse(value)) + when "JSON" then map_json(::JSON.parse(value)) else value end end diff --git a/spec/models/plugin_store_spec.rb b/spec/models/plugin_store_spec.rb index 1a3f692960a..bd84395c8d0 100644 --- a/spec/models/plugin_store_spec.rb +++ b/spec/models/plugin_store_spec.rb @@ -51,6 +51,34 @@ describe PluginStore do expect(result[:hi]).to eq("there") end + it "handles nested hashes correctly" do + + val = {"hi" => "there", "nested" => {"a" => "b", "with list" => ["a", "b", 3] }} + set("hello", val) + result = get("hello") + + expect(result).to eq(val) + + # ensure indiff access holds + expect(result[:hi]).to eq("there") + expect(result[:nested][:a]).to eq("b") + expect(result[:nested]["with list"]).to eq(["a", "b", 3]) + end + + it "handles arrays correctly" do + + val = ["a", "b", {"hash"=> "inside", "c"=> 1}] + set("hello", val) + result = get("hello") + + expect(result).to eq(val) + + # ensure indiff access holds + expect(result[2][:hash]).to eq("inside") + expect(result[2]["c"]).to eq(1) + + end + it "removes correctly" do set("hello", true) remove_row("hello")