From 6fca60ebe4332e1b211690bd8a25f5781ccf7e3d Mon Sep 17 00:00:00 2001 From: YAMAMOTO Yohei Date: Thu, 2 Apr 2009 22:00:28 +0800 Subject: [PATCH] fixed a bug with CastedModel constructer calling Hash#initialize(ifnone) Signed-off-by: Matt Aimonetti --- lib/couchrest/more/casted_model.rb | 3 ++- spec/couchrest/more/casted_model_spec.rb | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/couchrest/more/casted_model.rb b/lib/couchrest/more/casted_model.rb index ab0bbf7..8ab794e 100644 --- a/lib/couchrest/more/casted_model.rb +++ b/lib/couchrest/more/casted_model.rb @@ -9,7 +9,8 @@ module CouchRest end def initialize(keys={}) - super + raise StandardError unless self.is_a? Hash + super() keys.each do |k,v| self[k.to_s] = v end if keys diff --git a/spec/couchrest/more/casted_model_spec.rb b/spec/couchrest/more/casted_model_spec.rb index a2b9216..82558c1 100644 --- a/spec/couchrest/more/casted_model_spec.rb +++ b/spec/couchrest/more/casted_model_spec.rb @@ -4,6 +4,7 @@ require File.join(FIXTURE_PATH, 'more', 'card') class WithCastedModelMixin < Hash include CouchRest::CastedModel property :name + property :no_value end class DummyModel < CouchRest::ExtendedDocument @@ -54,6 +55,14 @@ describe CouchRest::CastedModel do it "should know who casted it" do @casted_obj.casted_by.should == @obj end + + it "should return nil for the 'no_value' attribute" do + @casted_obj.no_value.should be_nil + end + + it "should return nil for the unknown attribute" do + @casted_obj["unknown"].should be_nil + end end describe "casted as an array of a different type" do