optimisations, and some fixes for ruby 1.8.7

This commit is contained in:
Andrew Williams 2011-03-06 09:58:54 +10:30
parent 2a9305ebd3
commit 3ad4e1e979
5 changed files with 47 additions and 34 deletions

View file

@ -1,5 +1,6 @@
#!/usr/bin/env ruby
require 'rubygems'
require 'benchmark'
$:.unshift(File.dirname(__FILE__) + '/../lib')
@ -50,6 +51,8 @@ def run_benchmark
n.times { b.string = "test" }
end
next if ENV["BENCHMARK_STRING"]
x.report("assign integer:") do
n.times { b.number = 1 }
end
@ -102,12 +105,14 @@ end
begin
if supports_dirty?
puts "with use_dirty true"
if !ENV['BENCHMARK_DIRTY_OFF']
set_dirty(true)
puts "with use_dirty true"
run_benchmark
puts "\nwith use_dirty false"
end
set_dirty(false)
end
puts "\nwith use_dirty false"
run_benchmark
end

View file

@ -62,6 +62,7 @@ module CouchRest::Model
super
end
# ruby 1.9
def keep_if
if use_dirty? && block_given?
self.keys.each do |key|

View file

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

View file

@ -6,7 +6,9 @@ module CouchRest
included do
extlib_inheritable_accessor(:properties) unless self.respond_to?(:properties)
extlib_inheritable_accessor(:prop_by_name) unless self.respond_to?(:prop_by_name)
self.properties ||= []
self.prop_by_name ||= {}
raise "You can only mixin Properties in a class responding to [] and []=, if you tried to mixin CastedModel, make sure your class inherits from Hash or responds to the proper methods" unless (method_defined?(:[]) && method_defined?(:[]=))
end
@ -88,7 +90,7 @@ module CouchRest
alias :attributes :properties_with_values
def find_property(property)
property.is_a?(Property) ? property : self.class.properties.detect {|p| p.to_s == property.to_s}
property.is_a?(Property) ? property : self.class.prop_by_name[property.to_s]
end
# The following methods should be accessable by the Model::Base Class, but not by anything else!
@ -208,6 +210,7 @@ module CouchRest
validates_casted_model property.name
end
properties << property
prop_by_name[property.to_s] = property
property
end

View file

@ -246,25 +246,25 @@ describe "With use_dirty(on)" do
end
it "should report changes if an array index is modified" do
should_change_array do |array|
should_change_array do |array, obj|
array[0] = "keyword"
end
end
it "should report no changes if an array index is unmodified" do
should_not_change_array do |array|
should_not_change_array do |array, obj|
array[0] = array[0]
end
end
it "should report changes if an array is appended with <<" do
should_change_array do |array|
should_change_array do |array, obj|
array << 'keyword'
end
end
it "should report changes if an array is popped" do
should_change_array do |array|
should_change_array do |array, obj|
array.pop
end
end
@ -278,13 +278,13 @@ describe "With use_dirty(on)" do
end
it "should report changes if an array is pushed" do
should_change_array do |array|
should_change_array do |array, obj|
array.push("keyword")
end
end
it "should report changes if an array is shifted" do
should_change_array do |array|
should_change_array do |array, obj|
array.shift
end
end
@ -298,13 +298,13 @@ describe "With use_dirty(on)" do
end
it "should report changes if an array is unshifted" do
should_change_array do |array|
should_change_array do |array, obj|
array.unshift("keyword")
end
end
it "should report changes if an array is cleared" do
should_change_array do |array|
should_change_array do |array, obj|
array.clear
end
end
@ -332,83 +332,87 @@ describe "With use_dirty(on)" do
end
it "should report changes if a hash is modified" do
should_change_hash do |hash|
should_change_hash do |hash, obj|
hash['color'] = 'orange'
end
end
it "should report no changes if a hash is unmodified" do
should_not_change_hash do |hash|
should_not_change_hash do |hash, obj|
hash['color'] = hash['color']
end
end
it "should report changes when deleting from a hash" do
should_change_hash do |hash|
should_change_hash do |hash, obj|
hash.delete('color')
end
end
it "should report no changes when deleting a non existent key from a hash" do
should_not_change_hash do |hash|
should_not_change_hash do |hash, obj|
hash.delete('non-existent-key')
end
end
it "should report changes when clearing a hash" do
should_change_hash do |hash|
should_change_hash do |hash, obj|
hash.clear
end
end
it "should report changes when merging changes to a hash" do
should_change_hash do |hash|
should_change_hash do |hash, obj|
hash.merge!('foo' => 'bar')
end
end
it "should report no changes when merging no changes to a hash" do
should_not_change_hash do |hash|
should_not_change_hash do |hash, obj|
hash.merge!('color' => hash['color'])
end
end
it "should report changes when replacing hash content" do
should_change_hash do |hash|
should_change_hash do |hash, obj|
hash.replace('foo' => 'bar')
end
end
it "should report no changes when replacing hash content with same content" do
should_not_change_hash do |hash|
should_not_change_hash do |hash, obj|
hash.replace(hash)
end
end
it "should report changes when removing records with delete_if" do
should_change_hash do |hash|
should_change_hash do |hash, obj|
hash.delete_if { true }
end
end
it "should report no changes when removing no records with delete_if" do
should_not_change_hash do |hash|
should_not_change_hash do |hash, obj|
hash.delete_if { false }
end
end
if {}.respond_to?(:keep_if)
it "should report changes when removing records with keep_if" do
should_change_hash do |hash|
should_change_hash do |hash, obj|
hash.keep_if { false }
end
end
it "should report no changes when removing no records with keep_if" do
should_not_change_hash do |hash|
should_not_change_hash do |hash, obj|
hash.keep_if { true }
end
end
end
end
end