From 3f1b2ea0c63db643447cf5ea6306df26c950da7f Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Tue, 19 Jul 2011 18:03:31 +0200 Subject: [PATCH] Casting array type properties now possible --- history.md | 5 +++++ lib/couchrest/model/property.rb | 3 +-- spec/unit/property_spec.rb | 15 +++++++++------ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/history.md b/history.md index e1c8329..26bdae8 100644 --- a/history.md +++ b/history.md @@ -1,5 +1,10 @@ # CouchRest Model Change History +## 1.1.2 - 2011-07-XX + +* Minor fix + * Removing restriction that prohibited objects that cast as an array to be loaded. + ## 1.1.1 - 2011-07-04 * Minor fix diff --git a/lib/couchrest/model/property.rb b/lib/couchrest/model/property.rb index f303b07..6058fda 100644 --- a/lib/couchrest/model/property.rb +++ b/lib/couchrest/model/property.rb @@ -43,9 +43,8 @@ module CouchRest::Model end end - # Cast an individual value, not an array + # Cast an individual value def cast_value(parent, value) - raise "An array inside an array cannot be casted, use Embeddable module" if value.is_a?(Array) value = typecast_value(value, self) associate_casted_value_to_parent(parent, value) end diff --git a/spec/unit/property_spec.rb b/spec/unit/property_spec.rb index b799908..49548e9 100644 --- a/spec/unit/property_spec.rb +++ b/spec/unit/property_spec.rb @@ -442,15 +442,18 @@ describe "Property Class" do ary.last.should eql(Date.new(2011, 05, 22)) end - it "should raise and error if value is array when type is not" do - property = CouchRest::Model::Property.new(:test, Date) + it "should cast an object that provides an array" do + prop = Class.new do + attr_accessor :ary + def initialize(val); self.ary = val; end + def as_json; ary; end + end + property = CouchRest::Model::Property.new(:test, prop) parent = mock("FooClass") - lambda { - cast = property.cast(parent, [Date.new(2010, 6, 1)]) - }.should raise_error + cast = property.cast(parent, [1, 2]) + cast.ary.should eql([1, 2]) end - it "should set parent as casted_by object in CastedArray" do property = CouchRest::Model::Property.new(:test, [Object]) parent = mock("FooObject")