Skip to content

Commit

Permalink
removed use_dirty config option (runs faster)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobakasu committed Mar 6, 2011
1 parent 3ad4e1e commit 6348138
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 79 deletions.
2 changes: 1 addition & 1 deletion b/couchrest.rb
Expand Up @@ -111,8 +111,8 @@ def run_benchmark
run_benchmark
end
set_dirty(false)
puts "\nwith use_dirty false"
end

puts "\nwith use_dirty false"
run_benchmark
end
2 changes: 1 addition & 1 deletion lib/couchrest/model/casted_model.rb
Expand Up @@ -21,7 +21,7 @@ def initialize(keys = {})
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

Expand Down
2 changes: 0 additions & 2 deletions lib/couchrest/model/configuration.rb
Expand Up @@ -10,12 +10,10 @@ module Configuration
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

Expand Down
2 changes: 1 addition & 1 deletion lib/couchrest/model/dirty.rb
Expand Up @@ -22,7 +22,7 @@ module Dirty

def use_dirty?
bdoc = base_doc
bdoc && bdoc.use_dirty && !bdoc.disable_dirty
bdoc && !bdoc.disable_dirty
end

def couchrest_attribute_will_change!(attr)
Expand Down
2 changes: 1 addition & 1 deletion lib/couchrest/model/persistence.rb
Expand Up @@ -30,7 +30,7 @@ def create!
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)
Expand Down
2 changes: 1 addition & 1 deletion lib/couchrest/model/properties.rb
Expand Up @@ -203,7 +203,7 @@ def define_property(name, options={}, &block)
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)
Expand Down
7 changes: 3 additions & 4 deletions lib/couchrest/model/property.rb
Expand Up @@ -4,7 +4,7 @@ class Property

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.
Expand Down Expand Up @@ -38,8 +38,8 @@ def cast(parent, value)
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]
Expand Down Expand Up @@ -94,7 +94,6 @@ def parse_options(options)
@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

Expand Down
67 changes: 1 addition & 66 deletions spec/couchrest/dirty_spec.rb
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/couchrest/property_spec.rb
Expand Up @@ -843,10 +843,10 @@
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
Expand Down

0 comments on commit 6348138

Please sign in to comment.