From 8efa5208da250824f29939937aec70e746d44406 Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Sat, 25 Jun 2011 19:24:43 +0200 Subject: [PATCH] Renaming casted model to embeddable and preparing for 1.1.0 launch@ --- history.md | 3 +- lib/couchrest/model/base.rb | 5 ++-- .../model/{casted_model.rb => embeddable.rb} | 11 ++++++- lib/couchrest/model/properties.rb | 2 +- lib/couchrest/model/property.rb | 2 +- lib/couchrest/railtie.rb | 2 +- lib/couchrest_model.rb | 3 +- spec/fixtures/models/cat.rb | 2 +- spec/fixtures/models/membership.rb | 4 +-- spec/fixtures/models/person.rb | 4 +-- spec/fixtures/models/question.rb | 6 ++-- spec/unit/base_spec.rb | 11 +++++++ ...asted_model_spec.rb => embeddable_spec.rb} | 29 +++++++++---------- 13 files changed, 51 insertions(+), 33 deletions(-) rename lib/couchrest/model/{casted_model.rb => embeddable.rb} (87%) rename spec/unit/{casted_model_spec.rb => embeddable_spec.rb} (97%) diff --git a/history.md b/history.md index cff753c..f7fd395 100644 --- a/history.md +++ b/history.md @@ -2,8 +2,9 @@ ## 1.1.0 - 2011-06-25 -* Major Fixes +* Major Alterations * CastedModel no longer requires a Hash. Automatically includes all required methods. + * CastedModel module renamed to Embeddable (old still works!) * Minor Fixes * Validation callbacks now support context (thanks kostia) diff --git a/lib/couchrest/model/base.rb b/lib/couchrest/model/base.rb index 53cdc42..ed46699 100644 --- a/lib/couchrest/model/base.rb +++ b/lib/couchrest/model/base.rb @@ -47,8 +47,8 @@ module CouchRest # # Options supported: # - # * :directly_set_attributes: true when data comes directly from database - # * :database: provide an alternative database + # * :directly_set_attributes, true when data comes directly from database + # * :database, provide an alternative database # # If a block is provided the new model will be passed into the # block so that it can be populated. @@ -64,6 +64,7 @@ module CouchRest yield self if block_given? after_initialize if respond_to?(:after_initialize) + run_callbacks(:initialize) { self } end diff --git a/lib/couchrest/model/casted_model.rb b/lib/couchrest/model/embeddable.rb similarity index 87% rename from lib/couchrest/model/casted_model.rb rename to lib/couchrest/model/embeddable.rb index e445f96..2da140c 100644 --- a/lib/couchrest/model/casted_model.rb +++ b/lib/couchrest/model/embeddable.rb @@ -1,5 +1,5 @@ module CouchRest::Model - module CastedModel + module Embeddable extend ActiveSupport::Concern included do @@ -26,6 +26,7 @@ module CouchRest::Model def initialize(keys = {}, options = {}) super() prepare_all_attributes(keys, options) + run_callbacks(:initialize) { self } end end end @@ -61,6 +62,14 @@ module CouchRest::Model end alias :attributes= :update_attributes_without_saving + end # End Embeddable + + # Provide backwards compatability with previous versions (pre 1.1.0) + module CastedModel + extend ActiveSupport::Concern + included do + include CouchRest::Model::Embeddable + end end end diff --git a/lib/couchrest/model/properties.rb b/lib/couchrest/model/properties.rb index 94ad48d..7b2c494 100644 --- a/lib/couchrest/model/properties.rb +++ b/lib/couchrest/model/properties.rb @@ -167,7 +167,7 @@ module CouchRest type = options.delete(:type) || options.delete(:cast_as) if block_given? type = Class.new do - include CastedModel + include Embeddable end if block.arity == 1 # Traditional, with options type.class_eval { yield type } diff --git a/lib/couchrest/model/property.rb b/lib/couchrest/model/property.rb index 9beb5c7..f303b07 100644 --- a/lib/couchrest/model/property.rb +++ b/lib/couchrest/model/property.rb @@ -45,7 +45,7 @@ module CouchRest::Model # Cast an individual value, not an array def cast_value(parent, value) - raise "An array inside an array cannot be casted, use CastedModel" if value.is_a?(Array) + 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/lib/couchrest/railtie.rb b/lib/couchrest/railtie.rb index b08d00f..0c4d04a 100644 --- a/lib/couchrest/railtie.rb +++ b/lib/couchrest/railtie.rb @@ -6,7 +6,7 @@ module CouchRest def self.generator config.respond_to?(:app_generators) ? :app_generators : :generators end - + config.send(generator).orm :couchrest_model config.send(generator).test_framework :test_unit, :fixture => false diff --git a/lib/couchrest_model.rb b/lib/couchrest_model.rb index 97a491c..699da9e 100644 --- a/lib/couchrest_model.rb +++ b/lib/couchrest_model.rb @@ -33,7 +33,6 @@ require "couchrest/model/property_protection" require "couchrest/model/properties" require "couchrest/model/casted_array" require "couchrest/model/casted_hash" -require "couchrest/model/casted_model" require "couchrest/model/validations" require "couchrest/model/callbacks" require "couchrest/model/document_queries" @@ -58,7 +57,7 @@ require "couchrest/model/core_extensions/hash" require "couchrest/model/core_extensions/time_parsing" # Base libraries -require "couchrest/model/casted_model" +require "couchrest/model/embeddable" require "couchrest/model/base" # Add rails support *after* everything has loaded diff --git a/spec/fixtures/models/cat.rb b/spec/fixtures/models/cat.rb index d8e5584..a28f505 100644 --- a/spec/fixtures/models/cat.rb +++ b/spec/fixtures/models/cat.rb @@ -1,6 +1,6 @@ class CatToy - include CouchRest::Model::CastedModel + include CouchRest::Model::Embeddable property :name diff --git a/spec/fixtures/models/membership.rb b/spec/fixtures/models/membership.rb index 0c7f639..81c804e 100644 --- a/spec/fixtures/models/membership.rb +++ b/spec/fixtures/models/membership.rb @@ -1,4 +1,4 @@ -class Membership < Hash - include CouchRest::Model::CastedModel +class Membership + include CouchRest::Model::Embeddable end diff --git a/spec/fixtures/models/person.rb b/spec/fixtures/models/person.rb index de5de5c..71525e4 100644 --- a/spec/fixtures/models/person.rb +++ b/spec/fixtures/models/person.rb @@ -1,7 +1,7 @@ require 'cat' -class Person < Hash - include ::CouchRest::Model::CastedModel +class Person + include ::CouchRest::Model::Embeddable property :pet, Cat property :name, [String] diff --git a/spec/fixtures/models/question.rb b/spec/fixtures/models/question.rb index 5efcd20..ada5bd9 100644 --- a/spec/fixtures/models/question.rb +++ b/spec/fixtures/models/question.rb @@ -1,6 +1,6 @@ -class Question < Hash - include ::CouchRest::Model::CastedModel - +class Question + include ::CouchRest::Model::Embeddable + property :q property :a diff --git a/spec/unit/base_spec.rb b/spec/unit/base_spec.rb index 1454a1f..de4a619 100644 --- a/spec/unit/base_spec.rb +++ b/spec/unit/base_spec.rb @@ -69,6 +69,17 @@ describe "Model Base" do @doc = WithAfterInitializeMethod.new {|d| d.some_value = "foo"} @doc['some_value'].should eql('foo') end + + it "should call after_initialize callback if available" do + klass = Class.new(CouchRest::Model::Base) + klass.class_eval do # for ruby 1.8.7 + property :name + after_initialize :set_name + def set_name; self.name = "foobar"; end + end + @doc = klass.new + @doc.name.should eql("foobar") + end end describe "ActiveModel compatability Basic" do diff --git a/spec/unit/casted_model_spec.rb b/spec/unit/embeddable_spec.rb similarity index 97% rename from spec/unit/casted_model_spec.rb rename to spec/unit/embeddable_spec.rb index 5c8b6f5..c56f304 100644 --- a/spec/unit/casted_model_spec.rb +++ b/spec/unit/embeddable_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" class WithCastedModelMixin - include CouchRest::Model::CastedModel + include CouchRest::Model::Embeddable property :name property :no_value property :details, Object, :default => {} @@ -29,7 +29,7 @@ class DummyModel < CouchRest::Model::Base end class WithCastedCallBackModel - include CouchRest::Model::CastedModel + include CouchRest::Model::Embeddable property :name property :run_before_validation property :run_after_validation @@ -50,19 +50,7 @@ class CastedCallbackDoc < CouchRest::Model::Base property :callback_model, WithCastedCallBackModel end -describe CouchRest::Model::CastedModel do - - describe "A non hash class including CastedModel" do - it "should fail raising and include error" do - lambda do - class NotAHashButWithCastedModelMixin - include CouchRest::CastedModel - property :name - end - - end.should raise_error - end - end +describe CouchRest::Model::Embeddable do describe "isolated" do before(:each) do @@ -81,7 +69,16 @@ describe CouchRest::Model::CastedModel do it "should always return base_doc? as false" do @obj.base_doc?.should be_false end - + it "should call after_initialize callback if available" do + klass = Class.new do + include CouchRest::Model::CastedModel + after_initialize :set_name + property :name + def set_name; self.name = "foobar"; end + end + @obj = klass.new + @obj.name.should eql("foobar") + end end describe "casted as an attribute, but without a value" do