From 634813858e705dadce7b1bc9b17566c3e02b4d05 Mon Sep 17 00:00:00 2001 From: Andrew Williams Date: Sun, 6 Mar 2011 13:11:37 +1030 Subject: [PATCH] removed use_dirty config option (runs faster) --- b/couchrest.rb | 2 +- lib/couchrest/model/casted_model.rb | 2 +- lib/couchrest/model/configuration.rb | 2 - lib/couchrest/model/dirty.rb | 2 +- lib/couchrest/model/persistence.rb | 2 +- lib/couchrest/model/properties.rb | 2 +- lib/couchrest/model/property.rb | 7 ++- spec/couchrest/dirty_spec.rb | 67 +--------------------------- spec/couchrest/property_spec.rb | 4 +- 9 files changed, 11 insertions(+), 79 deletions(-) diff --git a/b/couchrest.rb b/b/couchrest.rb index d5ffe2e..644f84d 100644 --- a/b/couchrest.rb +++ b/b/couchrest.rb @@ -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 diff --git a/lib/couchrest/model/casted_model.rb b/lib/couchrest/model/casted_model.rb index 678c8ad..19a3c09 100644 --- a/lib/couchrest/model/casted_model.rb +++ b/lib/couchrest/model/casted_model.rb @@ -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 diff --git a/lib/couchrest/model/configuration.rb b/lib/couchrest/model/configuration.rb index 25ebfcb..df2bb62 100644 --- a/lib/couchrest/model/configuration.rb +++ b/lib/couchrest/model/configuration.rb @@ -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 diff --git a/lib/couchrest/model/dirty.rb b/lib/couchrest/model/dirty.rb index e0a13b0..c24a705 100644 --- a/lib/couchrest/model/dirty.rb +++ b/lib/couchrest/model/dirty.rb @@ -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) diff --git a/lib/couchrest/model/persistence.rb b/lib/couchrest/model/persistence.rb index fc1fb4f..d947931 100644 --- a/lib/couchrest/model/persistence.rb +++ b/lib/couchrest/model/persistence.rb @@ -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) diff --git a/lib/couchrest/model/properties.rb b/lib/couchrest/model/properties.rb index 99041d7..e5673ef 100644 --- a/lib/couchrest/model/properties.rb +++ b/lib/couchrest/model/properties.rb @@ -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) diff --git a/lib/couchrest/model/property.rb b/lib/couchrest/model/property.rb index 783cc69..4af8864 100644 --- a/lib/couchrest/model/property.rb +++ b/lib/couchrest/model/property.rb @@ -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 diff --git a/spec/couchrest/dirty_spec.rb b/spec/couchrest/dirty_spec.rb index cf47aeb..8f0ffa1 100644 --- a/spec/couchrest/dirty_spec.rb +++ b/spec/couchrest/dirty_spec.rb @@ -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 diff --git a/spec/couchrest/property_spec.rb b/spec/couchrest/property_spec.rb index 2c4eab0..b7a8540 100644 --- a/spec/couchrest/property_spec.rb +++ b/spec/couchrest/property_spec.rb @@ -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