From 1019a44d26b0f732ecb64c154748897db9b35a5e Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Sat, 5 Feb 2011 22:38:22 +0100 Subject: [PATCH] Anonymous casted model properties no longer require block parameter --- Gemfile.lock | 1 - README.md | 10 ++++++---- history.txt | 5 +++++ lib/couchrest/model/properties.rb | 6 +++++- spec/couchrest/casted_model_spec.rb | 11 +++++++++++ 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 38fe5d9..0f7d5b3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/README.md b/README.md index 32bcd9a..d64a990 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/history.txt b/history.txt index dac561a..423c303 100644 --- a/history.txt +++ b/history.txt @@ -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 diff --git a/lib/couchrest/model/properties.rb b/lib/couchrest/model/properties.rb index 3f021ed..481d135 100644 --- a/lib/couchrest/model/properties.rb +++ b/lib/couchrest/model/properties.rb @@ -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) diff --git a/spec/couchrest/casted_model_spec.rb b/spec/couchrest/casted_model_spec.rb index 8d7b634..96ce2ae 100644 --- a/spec/couchrest/casted_model_spec.rb +++ b/spec/couchrest/casted_model_spec.rb @@ -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