removed use_dirty config option (runs faster)

This commit is contained in:
Andrew Williams 2011-03-06 13:11:37 +10:30
parent 3ad4e1e979
commit 634813858e
9 changed files with 11 additions and 79 deletions

View file

@ -111,8 +111,8 @@ begin
run_benchmark
end
set_dirty(false)
puts "\nwith use_dirty false"
end
puts "\nwith use_dirty false"
run_benchmark
end

View file

@ -21,7 +21,7 @@ module CouchRest::Model
end
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)
end

View file

@ -10,12 +10,10 @@ module CouchRest
included do
add_config :model_type_key
add_config :mass_assign_any_attribute
add_config :use_dirty
configure do |config|
config.model_type_key = 'couchrest-type' # 'model'?
config.mass_assign_any_attribute = false
config.use_dirty = true
end
end

View file

@ -22,7 +22,7 @@ module CouchRest
def use_dirty?
bdoc = base_doc
bdoc && bdoc.use_dirty && !bdoc.disable_dirty
bdoc && !bdoc.disable_dirty
end
def couchrest_attribute_will_change!(attr)

View file

@ -30,7 +30,7 @@ module CouchRest
def update(options = {})
raise "Calling #{self.class.name}#update on document that has not been created!" if self.new?
return false unless perform_validations(options)
return true if use_dirty? && !self.changed?
return true if !self.changed?
_run_update_callbacks do
_run_save_callbacks do
result = database.save_doc(self)

View file

@ -203,7 +203,7 @@ module CouchRest
end
type = [type] # inject as an array
end
property = Property.new(name, type, options.merge(:use_dirty => use_dirty))
property = Property.new(name, type, options)
create_property_getter(property)
create_property_setter(property) unless property.read_only == true
if property.type_class.respond_to?(:validates_casted_model)

View file

@ -4,7 +4,7 @@ module CouchRest::Model
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.
# All Properties are assumed casted unless the type is nil.
@ -38,8 +38,8 @@ module CouchRest::Model
end
arr = value.collect { |data| cast_value(parent, data) }
# 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.casted_by = parent if value.respond_to?(:casted_by)
value = CastedArray.new(arr, self)
value.casted_by = parent
elsif (type == Object || type == Hash) && (value.class == Hash)
# allow casted_by calls to be passed up chain by wrapping in CastedHash
value = CouchRest::Model::CastedHash[value]
@ -94,7 +94,6 @@ module CouchRest::Model
@alias = options.delete(:alias) if options[:alias]
@default = options.delete(:default) unless options[:default].nil?
@init_method = options[:init_method] ? options.delete(:init_method) : 'new'
@use_dirty = options.delete(:use_dirty)
@options = options
end

View file

@ -24,72 +24,7 @@ class DummyModel < CouchRest::Model::Base
end
end
# set dirty configuration, return previous configuration setting
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 "Dirty" do
describe "changes" do

View file

@ -843,10 +843,10 @@ describe "Property Class" do
property.cast(parent, ["2010-06-01", "2010-06-02"]).class.should eql(CouchRest::Model::CastedArray)
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])
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
it "should raise and error if value is array when type is not" do