removed use_dirty config option (runs faster)
This commit is contained in:
parent
3ad4e1e979
commit
634813858e
|
@ -111,8 +111,8 @@ begin
|
||||||
run_benchmark
|
run_benchmark
|
||||||
end
|
end
|
||||||
set_dirty(false)
|
set_dirty(false)
|
||||||
|
puts "\nwith use_dirty false"
|
||||||
end
|
end
|
||||||
|
|
||||||
puts "\nwith use_dirty false"
|
|
||||||
run_benchmark
|
run_benchmark
|
||||||
end
|
end
|
||||||
|
|
|
@ -21,7 +21,7 @@ module CouchRest::Model
|
||||||
end
|
end
|
||||||
|
|
||||||
def []= key, value
|
def []= key, value
|
||||||
couchrest_attribute_will_change!(key) if use_dirty && self[key] != value
|
couchrest_attribute_will_change!(key) if self[key] != value
|
||||||
super(key.to_s, value)
|
super(key.to_s, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -10,12 +10,10 @@ module CouchRest
|
||||||
included do
|
included do
|
||||||
add_config :model_type_key
|
add_config :model_type_key
|
||||||
add_config :mass_assign_any_attribute
|
add_config :mass_assign_any_attribute
|
||||||
add_config :use_dirty
|
|
||||||
|
|
||||||
configure do |config|
|
configure do |config|
|
||||||
config.model_type_key = 'couchrest-type' # 'model'?
|
config.model_type_key = 'couchrest-type' # 'model'?
|
||||||
config.mass_assign_any_attribute = false
|
config.mass_assign_any_attribute = false
|
||||||
config.use_dirty = true
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ module CouchRest
|
||||||
|
|
||||||
def use_dirty?
|
def use_dirty?
|
||||||
bdoc = base_doc
|
bdoc = base_doc
|
||||||
bdoc && bdoc.use_dirty && !bdoc.disable_dirty
|
bdoc && !bdoc.disable_dirty
|
||||||
end
|
end
|
||||||
|
|
||||||
def couchrest_attribute_will_change!(attr)
|
def couchrest_attribute_will_change!(attr)
|
||||||
|
|
|
@ -30,7 +30,7 @@ module CouchRest
|
||||||
def update(options = {})
|
def update(options = {})
|
||||||
raise "Calling #{self.class.name}#update on document that has not been created!" if self.new?
|
raise "Calling #{self.class.name}#update on document that has not been created!" if self.new?
|
||||||
return false unless perform_validations(options)
|
return false unless perform_validations(options)
|
||||||
return true if use_dirty? && !self.changed?
|
return true if !self.changed?
|
||||||
_run_update_callbacks do
|
_run_update_callbacks do
|
||||||
_run_save_callbacks do
|
_run_save_callbacks do
|
||||||
result = database.save_doc(self)
|
result = database.save_doc(self)
|
||||||
|
|
|
@ -203,7 +203,7 @@ module CouchRest
|
||||||
end
|
end
|
||||||
type = [type] # inject as an array
|
type = [type] # inject as an array
|
||||||
end
|
end
|
||||||
property = Property.new(name, type, options.merge(:use_dirty => use_dirty))
|
property = Property.new(name, type, options)
|
||||||
create_property_getter(property)
|
create_property_getter(property)
|
||||||
create_property_setter(property) unless property.read_only == true
|
create_property_setter(property) unless property.read_only == true
|
||||||
if property.type_class.respond_to?(:validates_casted_model)
|
if property.type_class.respond_to?(:validates_casted_model)
|
||||||
|
|
|
@ -4,7 +4,7 @@ module CouchRest::Model
|
||||||
|
|
||||||
include ::CouchRest::Model::Typecast
|
include ::CouchRest::Model::Typecast
|
||||||
|
|
||||||
attr_reader :name, :type, :type_class, :read_only, :alias, :default, :casted, :init_method, :use_dirty, :options
|
attr_reader :name, :type, :type_class, :read_only, :alias, :default, :casted, :init_method, :options
|
||||||
|
|
||||||
# Attribute to define.
|
# Attribute to define.
|
||||||
# All Properties are assumed casted unless the type is nil.
|
# All Properties are assumed casted unless the type is nil.
|
||||||
|
@ -38,8 +38,8 @@ module CouchRest::Model
|
||||||
end
|
end
|
||||||
arr = value.collect { |data| cast_value(parent, data) }
|
arr = value.collect { |data| cast_value(parent, data) }
|
||||||
# allow casted_by calls to be passed up chain by wrapping in CastedArray
|
# allow casted_by calls to be passed up chain by wrapping in CastedArray
|
||||||
value = (use_dirty || type_class != String) ? CastedArray.new(arr, self) : arr
|
value = CastedArray.new(arr, self)
|
||||||
value.casted_by = parent if value.respond_to?(:casted_by)
|
value.casted_by = parent
|
||||||
elsif (type == Object || type == Hash) && (value.class == Hash)
|
elsif (type == Object || type == Hash) && (value.class == Hash)
|
||||||
# allow casted_by calls to be passed up chain by wrapping in CastedHash
|
# allow casted_by calls to be passed up chain by wrapping in CastedHash
|
||||||
value = CouchRest::Model::CastedHash[value]
|
value = CouchRest::Model::CastedHash[value]
|
||||||
|
@ -94,7 +94,6 @@ module CouchRest::Model
|
||||||
@alias = options.delete(:alias) if options[:alias]
|
@alias = options.delete(:alias) if options[:alias]
|
||||||
@default = options.delete(:default) unless options[:default].nil?
|
@default = options.delete(:default) unless options[:default].nil?
|
||||||
@init_method = options[:init_method] ? options.delete(:init_method) : 'new'
|
@init_method = options[:init_method] ? options.delete(:init_method) : 'new'
|
||||||
@use_dirty = options.delete(:use_dirty)
|
|
||||||
@options = options
|
@options = options
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -24,72 +24,7 @@ class DummyModel < CouchRest::Model::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# set dirty configuration, return previous configuration setting
|
describe "Dirty" do
|
||||||
def set_dirty(value)
|
|
||||||
orig = nil
|
|
||||||
CouchRest::Model::Base.configure do |config|
|
|
||||||
orig = config.use_dirty
|
|
||||||
config.use_dirty = value
|
|
||||||
end
|
|
||||||
Card.instance_eval do
|
|
||||||
self.use_dirty = value
|
|
||||||
end
|
|
||||||
orig
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "With use_dirty(off)" do
|
|
||||||
|
|
||||||
before(:all) do
|
|
||||||
@use_dirty_orig = set_dirty(false)
|
|
||||||
end
|
|
||||||
|
|
||||||
# turn dirty back to default
|
|
||||||
after(:all) do
|
|
||||||
set_dirty(@use_dirty_orig)
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "changes" do
|
|
||||||
|
|
||||||
it "should not respond to the changes method" do
|
|
||||||
@card = Card.new
|
|
||||||
@card.first_name = "andrew"
|
|
||||||
@card.changes.should == {}
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "changed?" do
|
|
||||||
|
|
||||||
it "should not record changes" do
|
|
||||||
@card = Card.new
|
|
||||||
@card.first_name = "andrew"
|
|
||||||
@card.changed?.should be_false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "save" do
|
|
||||||
|
|
||||||
it "should save unchanged records" do
|
|
||||||
@card = Card.create!(:first_name => "matt")
|
|
||||||
@card = Card.find(@card.id)
|
|
||||||
@card.database.should_receive(:save_doc).and_return({"ok" => true})
|
|
||||||
@card.save
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "With use_dirty(on)" do
|
|
||||||
|
|
||||||
before(:all) do
|
|
||||||
@use_dirty_orig = set_dirty(true)
|
|
||||||
end
|
|
||||||
|
|
||||||
# turn dirty back to default
|
|
||||||
after(:all) do
|
|
||||||
set_dirty(@use_dirty_orig)
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "changes" do
|
describe "changes" do
|
||||||
|
|
||||||
|
|
|
@ -843,10 +843,10 @@ describe "Property Class" do
|
||||||
property.cast(parent, ["2010-06-01", "2010-06-02"]).class.should eql(CouchRest::Model::CastedArray)
|
property.cast(parent, ["2010-06-01", "2010-06-02"]).class.should eql(CouchRest::Model::CastedArray)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not set a CastedArray on array of Strings" do
|
it "should set a CastedArray on array of Strings" do
|
||||||
property = CouchRest::Model::Property.new(:test, [String])
|
property = CouchRest::Model::Property.new(:test, [String])
|
||||||
parent = mock("FooObject")
|
parent = mock("FooObject")
|
||||||
property.cast(parent, ["2010-06-01", "2010-06-02"]).class.should_not eql(CouchRest::Model::CastedArray)
|
property.cast(parent, ["2010-06-01", "2010-06-02"]).class.should eql(CouchRest::Model::CastedArray)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should raise and error if value is array when type is not" do
|
it "should raise and error if value is array when type is not" do
|
||||||
|
|
Loading…
Reference in a new issue