Anonymous casted model properties no longer require block parameter

This commit is contained in:
Sam Lown 2011-02-05 22:38:22 +01:00
parent 1ccb96bf56
commit 1019a44d26
5 changed files with 27 additions and 6 deletions

View file

@ -6,7 +6,6 @@ PATH
couchrest (~> 1.0.1)
mime-types (~> 1.15)
railties (~> 3.0.0)
rspec (>= 2.0.0)
tzinfo (~> 0.3.22)
GEM

View file

@ -238,9 +238,9 @@ you'd like to model, CouchRest Model supports creating anonymous classes:
class Cat < CouchRest::Model::Base
property :name, String
property :toys do |toy|
toy.property :name, String
toy.property :rating, Integer
property :toys do
property :name, String
property :rating, Integer
end
end
@ -248,7 +248,9 @@ you'd like to model, CouchRest Model supports creating anonymous classes:
@cat.toys.last.rating == 5
@cat.toys.last.name == 'catnip ball'
Anonymous classes will *only* create arrays of objects.
Anonymous classes will *only* create arrays of objects. If you're more of the traditional type, a block parameter
can be provided allowing you to use this variable before each method call inside the anonymous class. This is useful
if you need to access variables outside of the block.
## Assocations

View file

@ -1,3 +1,8 @@
== 1.0.1
* Minor enhancements:
* A yield parameter in an anonymous casted model property block is no longer required (@samlown)
== CouchRest Model 1.0.0
* Major enhancements

View file

@ -156,7 +156,11 @@ module CouchRest
type = Class.new(Hash) do
include CastedModel
end
type.class_eval { yield type }
if block.arity == 1 # Traditional, with options
type.class_eval { yield type }
else
type.instance_exec(&block)
end
type = [type] # inject as an array
end
property = Property.new(name, type, options)

View file

@ -24,6 +24,9 @@ class DummyModel < CouchRest::Model::Base
property :sub_models do |child|
child.property :title
end
property :param_free_sub_models do
property :title
end
end
class WithCastedCallBackModel < Hash
@ -100,6 +103,14 @@ describe CouchRest::Model::CastedModel do
@obj.sub_models << {:title => 'test'}
@obj.sub_models.first.title.should eql('test')
end
it "should be empty intitally (without params)" do
@obj.param_free_sub_models.should_not be_nil
@obj.param_free_sub_models.should be_empty
end
it "should be updatable using a hash (without params)" do
@obj.param_free_sub_models << {:title => 'test'}
@obj.param_free_sub_models.first.title.should eql('test')
end
end
describe "casted as attribute" do