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
* 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)

View File

@ -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

View File

@ -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

View File

@ -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 }

View File

@ -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

View File

@ -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

View File

@ -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

View File

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

View File

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

View File

@ -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]

View File

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

View File

@ -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

View File

@ -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