Skip to content

Commit

Permalink
Casting array type properties now possible
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown committed Jul 19, 2011
1 parent 9d724ae commit 3f1b2ea
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
5 changes: 5 additions & 0 deletions 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
Expand Down
3 changes: 1 addition & 2 deletions lib/couchrest/model/property.rb
Expand Up @@ -43,9 +43,8 @@ def cast(parent, value)
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
Expand Down
15 changes: 9 additions & 6 deletions spec/unit/property_spec.rb
Expand Up @@ -442,15 +442,18 @@
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")
Expand Down

0 comments on commit 3f1b2ea

Please sign in to comment.