Renaming casted model to embeddable and preparing for 1.1.0 launch@

master v1.1.0
Sam Lown 2011-06-25 19:24:43 +02:00
parent 05ed7b127f
commit 8efa5208da
13 changed files with 51 additions and 33 deletions

View File

@ -2,8 +2,9 @@
## 1.1.0 - 2011-06-25 ## 1.1.0 - 2011-06-25
* Major Fixes * Major Alterations
* CastedModel no longer requires a Hash. Automatically includes all required methods. * CastedModel no longer requires a Hash. Automatically includes all required methods.
* CastedModel module renamed to Embeddable (old still works!)
* Minor Fixes * Minor Fixes
* Validation callbacks now support context (thanks kostia) * Validation callbacks now support context (thanks kostia)

View File

@ -47,8 +47,8 @@ module CouchRest
# #
# Options supported: # Options supported:
# #
# * :directly_set_attributes: true when data comes directly from database # * :directly_set_attributes, true when data comes directly from database
# * :database: provide an alternative database # * :database, provide an alternative database
# #
# If a block is provided the new model will be passed into the # If a block is provided the new model will be passed into the
# block so that it can be populated. # block so that it can be populated.
@ -64,6 +64,7 @@ module CouchRest
yield self if block_given? yield self if block_given?
after_initialize if respond_to?(:after_initialize) after_initialize if respond_to?(:after_initialize)
run_callbacks(:initialize) { self }
end end

View File

@ -1,5 +1,5 @@
module CouchRest::Model module CouchRest::Model
module CastedModel module Embeddable
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
@ -26,6 +26,7 @@ module CouchRest::Model
def initialize(keys = {}, options = {}) def initialize(keys = {}, options = {})
super() super()
prepare_all_attributes(keys, options) prepare_all_attributes(keys, options)
run_callbacks(:initialize) { self }
end end
end end
end end
@ -61,6 +62,14 @@ module CouchRest::Model
end end
alias :attributes= :update_attributes_without_saving 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
end end

View File

@ -167,7 +167,7 @@ module CouchRest
type = options.delete(:type) || options.delete(:cast_as) type = options.delete(:type) || options.delete(:cast_as)
if block_given? if block_given?
type = Class.new do type = Class.new do
include CastedModel include Embeddable
end end
if block.arity == 1 # Traditional, with options if block.arity == 1 # Traditional, with options
type.class_eval { yield type } type.class_eval { yield type }

View File

@ -45,7 +45,7 @@ module CouchRest::Model
# Cast an individual value, not an array # Cast an individual value, not an array
def cast_value(parent, value) 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) value = typecast_value(value, self)
associate_casted_value_to_parent(parent, value) associate_casted_value_to_parent(parent, value)
end end

View File

@ -6,7 +6,7 @@ module CouchRest
def self.generator def self.generator
config.respond_to?(:app_generators) ? :app_generators : :generators config.respond_to?(:app_generators) ? :app_generators : :generators
end end
config.send(generator).orm :couchrest_model config.send(generator).orm :couchrest_model
config.send(generator).test_framework :test_unit, :fixture => false config.send(generator).test_framework :test_unit, :fixture => false

View File

@ -33,7 +33,6 @@ require "couchrest/model/property_protection"
require "couchrest/model/properties" require "couchrest/model/properties"
require "couchrest/model/casted_array" require "couchrest/model/casted_array"
require "couchrest/model/casted_hash" require "couchrest/model/casted_hash"
require "couchrest/model/casted_model"
require "couchrest/model/validations" require "couchrest/model/validations"
require "couchrest/model/callbacks" require "couchrest/model/callbacks"
require "couchrest/model/document_queries" require "couchrest/model/document_queries"
@ -58,7 +57,7 @@ require "couchrest/model/core_extensions/hash"
require "couchrest/model/core_extensions/time_parsing" require "couchrest/model/core_extensions/time_parsing"
# Base libraries # Base libraries
require "couchrest/model/casted_model" require "couchrest/model/embeddable"
require "couchrest/model/base" require "couchrest/model/base"
# Add rails support *after* everything has loaded # Add rails support *after* everything has loaded

View File

@ -1,6 +1,6 @@
class CatToy class CatToy
include CouchRest::Model::CastedModel include CouchRest::Model::Embeddable
property :name property :name

View File

@ -1,4 +1,4 @@
class Membership < Hash class Membership
include CouchRest::Model::CastedModel include CouchRest::Model::Embeddable
end end

View File

@ -1,7 +1,7 @@
require 'cat' require 'cat'
class Person < Hash class Person
include ::CouchRest::Model::CastedModel include ::CouchRest::Model::Embeddable
property :pet, Cat property :pet, Cat
property :name, [String] property :name, [String]

View File

@ -1,6 +1,6 @@
class Question < Hash class Question
include ::CouchRest::Model::CastedModel include ::CouchRest::Model::Embeddable
property :q property :q
property :a property :a

View File

@ -69,6 +69,17 @@ describe "Model Base" do
@doc = WithAfterInitializeMethod.new {|d| d.some_value = "foo"} @doc = WithAfterInitializeMethod.new {|d| d.some_value = "foo"}
@doc['some_value'].should eql('foo') @doc['some_value'].should eql('foo')
end 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 end
describe "ActiveModel compatability Basic" do describe "ActiveModel compatability Basic" do

View File

@ -2,7 +2,7 @@
require "spec_helper" require "spec_helper"
class WithCastedModelMixin class WithCastedModelMixin
include CouchRest::Model::CastedModel include CouchRest::Model::Embeddable
property :name property :name
property :no_value property :no_value
property :details, Object, :default => {} property :details, Object, :default => {}
@ -29,7 +29,7 @@ class DummyModel < CouchRest::Model::Base
end end
class WithCastedCallBackModel class WithCastedCallBackModel
include CouchRest::Model::CastedModel include CouchRest::Model::Embeddable
property :name property :name
property :run_before_validation property :run_before_validation
property :run_after_validation property :run_after_validation
@ -50,19 +50,7 @@ class CastedCallbackDoc < CouchRest::Model::Base
property :callback_model, WithCastedCallBackModel property :callback_model, WithCastedCallBackModel
end end
describe CouchRest::Model::CastedModel do describe CouchRest::Model::Embeddable 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 "isolated" do describe "isolated" do
before(:each) do before(:each) do
@ -81,7 +69,16 @@ describe CouchRest::Model::CastedModel do
it "should always return base_doc? as false" do it "should always return base_doc? as false" do
@obj.base_doc?.should be_false @obj.base_doc?.should be_false
end 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 end
describe "casted as an attribute, but without a value" do describe "casted as an attribute, but without a value" do