From d50d47c32a5bd8a11777c37f606f7b3fe1779aed Mon Sep 17 00:00:00 2001 From: Kostiantyn Kahanskyi Date: Mon, 6 Jun 2011 18:07:33 +0200 Subject: [PATCH] Should be able to assign a casted hash to a hash property MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes https://github.com/couchrest/couchrest_model/issues/68 Signed-off-by: Marcos Tapajós --- lib/couchrest/model/property.rb | 2 +- spec/couchrest/property_spec.rb | 11 +++++++++++ spec/fixtures/more/key_chain.rb | 5 +++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/more/key_chain.rb diff --git a/lib/couchrest/model/property.rb b/lib/couchrest/model/property.rb index 7661858..0ea9962 100644 --- a/lib/couchrest/model/property.rb +++ b/lib/couchrest/model/property.rb @@ -39,7 +39,7 @@ module CouchRest::Model arr = value.collect { |data| cast_value(parent, data) } # allow casted_by calls to be passed up chain by wrapping in CastedArray CastedArray.new(arr, self, parent) - elsif (type == Object || type == Hash) && (value.class == Hash) + elsif (type == Object || type == Hash) && (value.kind_of?(Hash)) # allow casted_by calls to be passed up chain by wrapping in CastedHash CastedHash[value, self, parent] elsif !value.nil? diff --git a/spec/couchrest/property_spec.rb b/spec/couchrest/property_spec.rb index bb55bfc..13ada9f 100644 --- a/spec/couchrest/property_spec.rb +++ b/spec/couchrest/property_spec.rb @@ -9,6 +9,7 @@ require File.join(FIXTURE_PATH, 'more', 'service') require File.join(FIXTURE_PATH, 'more', 'event') require File.join(FIXTURE_PATH, 'more', 'user') require File.join(FIXTURE_PATH, 'more', 'course') +require File.join(FIXTURE_PATH, "more", "key_chain") describe "Model properties" do @@ -239,6 +240,16 @@ describe "Model properties" do end +describe "properties of hash of casted models" do + it "should be able to assign a casted hash to a hash property" do + chain = KeyChain.new + keys = {"House" => "8==$", "Office" => "<>==U"} + chain.keys = keys + chain.keys = chain.keys + chain.keys.should == keys + end +end + describe "properties of array of casted models" do before(:each) do diff --git a/spec/fixtures/more/key_chain.rb b/spec/fixtures/more/key_chain.rb new file mode 100644 index 0000000..0837e39 --- /dev/null +++ b/spec/fixtures/more/key_chain.rb @@ -0,0 +1,5 @@ +class KeyChain < CouchRest::Model::Base + use_database(DB) + + property(:keys, Hash) +end