Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/lib/dm-core.rb b/lib/dm-core.rb
- index 6165b18..070f903 100644
- --- a/lib/dm-core.rb
- +++ b/lib/dm-core.rb
- @@ -169,26 +169,19 @@ module DataMapper
- # @param [Symbol] args the name of a repository to act within or return, :default is default
- # @yield [Proc] (optional) block to execute within the context of the named repository
- # @demo spec/integration/repository_spec.rb
- - def self.repository(*args, &block) # :yields: current_context
- - if args.size > 1
- - raise ArgumentError, "Can only pass in one optional argument, but passed in #{args.size} arguments", caller
- - end
- -
- - if args.any? && !args.first.kind_of?(Symbol)
- - raise ArgumentError, "First optional argument must be a Symbol, but was #{args.first.inspect}", caller
- - end
- -
- - name = args.first
- -
- + def self.repository(name = nil) # :yields: current_context
- current_repository = if name
- + raise ArgumentError, "First optional argument must be a Symbol, but was #{args.first.inspect}" unless name.is_a?(Symbol)
- Repository.context.detect { |r| r.name == name } || Repository.new(name)
- else
- Repository.context.last || Repository.new(Repository.default_name)
- end
- - return current_repository unless block_given?
- -
- - current_repository.scope(&block)
- + if block_given?
- + current_repository.scope { |*block_args| yield(*block_args) }
- + else
- + current_repository
- + end
- end
- # A logger should always be present. Lets be consistent with DO
- diff --git a/lib/dm-core/adapters/data_objects_adapter.rb b/lib/dm-core/adapters/data_objects_adapter.rb
- index 4197732..6204aa6 100644
- --- a/lib/dm-core/adapters/data_objects_adapter.rb
- +++ b/lib/dm-core/adapters/data_objects_adapter.rb
- @@ -164,7 +164,7 @@ module DataMapper
- end
- end
- - def with_connection(&block)
- + def with_connection
- connection = nil
- begin
- connection = create_connection
- @@ -177,7 +177,7 @@ module DataMapper
- end
- end
- - def with_reader(statement, bind_values = [], &block)
- + def with_reader(statement, bind_values = [])
- with_connection do |connection|
- reader = nil
- begin
- diff --git a/lib/dm-core/adapters/postgres_adapter.rb b/lib/dm-core/adapters/postgres_adapter.rb
- index ed25893..1c8774e 100644
- --- a/lib/dm-core/adapters/postgres_adapter.rb
- +++ b/lib/dm-core/adapters/postgres_adapter.rb
- @@ -86,7 +86,7 @@ module DataMapper
- end
- # TODO: move to dm-more/dm-migrations
- - def without_notices(&block)
- + def without_notices
- # execute the block with NOTICE messages disabled
- begin
- execute('SET client_min_messages = warning')
- diff --git a/lib/dm-core/associations/one_to_many.rb b/lib/dm-core/associations/one_to_many.rb
- index b079563..24d04ce 100644
- --- a/lib/dm-core/associations/one_to_many.rb
- +++ b/lib/dm-core/associations/one_to_many.rb
- @@ -132,7 +132,7 @@ module DataMapper
- orphan_resource(super)
- end
- - def delete(resource, &block)
- + def delete(resource)
- assert_mutable
- orphan_resource(super)
- end
- diff --git a/lib/dm-core/associations/relationship.rb b/lib/dm-core/associations/relationship.rb
- index 3007f10..0e202ec 100644
- --- a/lib/dm-core/associations/relationship.rb
- +++ b/lib/dm-core/associations/relationship.rb
- @@ -157,14 +157,14 @@ module DataMapper
- end
- # @api private
- - def with_repository(object = nil, &block)
- + def with_repository(object = nil)
- other_model = object.model == child_model ? parent_model : child_model if object.respond_to?(:model)
- other_model = object == child_model ? parent_model : child_model if object.kind_of?(DataMapper::Resource)
- if other_model && other_model.repository == object.repository && object.repository.name != @repository_name
- - object.repository.scope(&block)
- + object.repository.scope { |block_args| yield(*block_args) }
- else
- - repository(@repository_name, &block)
- + repository(@repository_name) { |block_args| yield(*block_args) }
- end
- end
- diff --git a/lib/dm-core/collection.rb b/lib/dm-core/collection.rb
- index a260ffe..7e09b3d 100644
- --- a/lib/dm-core/collection.rb
- +++ b/lib/dm-core/collection.rb
- @@ -295,7 +295,7 @@ module DataMapper
- # @see Array#delete
- #
- # @api public
- - def delete(resource, &block)
- + def delete(resource)
- orphan_resource(super)
- end
- diff --git a/lib/dm-core/model.rb b/lib/dm-core/model.rb
- index a710297..e8e26c8 100644
- --- a/lib/dm-core/model.rb
- +++ b/lib/dm-core/model.rb
- @@ -102,13 +102,17 @@ module DataMapper
- # if given a block, otherwise the requested repository.
- #-
- # @api public
- - def repository(name = nil, &block)
- + def repository(name = nil)
- #
- # There has been a couple of different strategies here, but me (zond) and dkubb are at least
- # united in the concept of explicitness over implicitness. That is - the explicit wish of the
- # caller (+name+) should be given more priority than the implicit wish of the caller (Repository.context.last).
- #
- - DataMapper.repository(name || repository_name, &block)
- + if block_given?
- + DataMapper.repository(name || repository_name) { |*block_args| yield(*block_args) }
- + else
- + DataMapper.repository(name || repository_name)
- + end
- end
- ##
- @@ -219,7 +223,8 @@ module DataMapper
- end
- def default_order(repository_name = default_repository_name)
- - key(repository_name).map { |property| Query::Direction.new(property) }
- + @default_order ||= {}
- + @default_order[repository_name] ||= key(repository_name).map { |property| Query::Direction.new(property) }
- end
- def get(*key)
- @@ -451,8 +456,8 @@ module DataMapper
- # @api public
- #
- # TODO: move to dm-more/dm-transactions
- - def transaction(&block)
- - DataMapper::Transaction.new(self, &block)
- + def transaction
- + DataMapper::Transaction.new(self) { |block_args| yield(*block_args) }
- end
- end # module Transaction
- diff --git a/lib/dm-core/property.rb b/lib/dm-core/property.rb
- index de9213f..9f70504 100644
- --- a/lib/dm-core/property.rb
- +++ b/lib/dm-core/property.rb
- @@ -432,13 +432,6 @@ module DataMapper
- new_value = typecast(value)
- old_value = get!(resource)
- - # skip setting the property if the new value is equal
- - # to the old value, and the old value was defined
- - # ---
- - # [YK] Why bother? Does this change the result at all?
- - # ---
- - # return if new_value == old_value && resource.attribute_loaded?(name)
- -
- set_original_value(resource, old_value)
- set!(resource, new_value)
- diff --git a/lib/dm-core/property_set.rb b/lib/dm-core/property_set.rb
- index 40d75b6..629d4fc 100755
- --- a/lib/dm-core/property_set.rb
- +++ b/lib/dm-core/property_set.rb
- @@ -8,6 +8,7 @@ module DataMapper
- end
- def []=(name, property)
- + @key, @defaults = nil
- if existing_property = detect { |p| p.name == name }
- property.hash
- @entries[@entries.index(existing_property)] = property
- @@ -22,10 +23,12 @@ module DataMapper
- end
- def slice(*names)
- + @key, @defaults = nil
- @property_for.values_at(*names)
- end
- def add(*properties)
- + @key, @defaults = nil
- @entries.push(*properties)
- properties.each { |property| property.hash }
- self
- @@ -47,11 +50,11 @@ module DataMapper
- end
- def defaults
- - reject { |property| property.lazy? }
- + @defaults ||= reject { |property| property.lazy? }
- end
- def key
- - select { |property| property.key? }
- + @key ||= select { |property| property.key? }
- end
- def indexes
- @@ -132,6 +135,7 @@ module DataMapper
- end
- def initialize_copy(orig)
- + @key, @defaults = nil
- @entries = orig.entries.dup
- @property_for = hash_for_property_for
- end
- diff --git a/lib/dm-core/query.rb b/lib/dm-core/query.rb
- index 81c2b32..3c48b88 100644
- --- a/lib/dm-core/query.rb
- +++ b/lib/dm-core/query.rb
- @@ -190,7 +190,7 @@ module DataMapper
- @includes = normalize_includes(@includes)
- # treat all non-options as conditions
- - (options.keys - OPTIONS - OPTIONS.map { |option| option.to_s }).each do |k|
- + (options.keys - OPTIONS).each do |k|
- append_condition(k, options[k])
- end
- @@ -218,53 +218,50 @@ module DataMapper
- # validate the options
- def assert_valid_options(options)
- - # validate the reload option and unique option
- - ([ :reload, :unique ] & options.keys).each do |attribute|
- - if options[attribute] != true && options[attribute] != false
- - raise ArgumentError, "+options[:#{attribute}]+ must be true or false, but was #{options[attribute].inspect}", caller(2)
- - end
- - end
- -
- - # validate the offset and limit options
- - ([ :offset, :limit ] & options.keys).each do |attribute|
- - value = options[attribute]
- - assert_kind_of "options[:#{attribute}]", value, Integer
- - end
- -
- - if options.has_key?(:offset) && options[:offset] < 0
- - raise ArgumentError, "+options[:offset]+ must be greater than or equal to 0, but was #{options[:offset].inspect}", caller(2)
- - end
- -
- - if options.has_key?(:limit) && options[:limit] < 1
- - raise ArgumentError, "+options[:limit]+ must be greater than or equal to 1, but was #{options[:limit].inspect}", caller(2)
- - end
- + # [DB] This might look more ugly now, but it's 2x as fast as the old code
- + # [DB] This is one of the heavy spots for Query.new I found during profiling.
- + options.each_pair do |attribute, value|
- +
- + # validate the reload option and unique option
- + if [:reload, :unique].include? attribute
- + if value != true && value != false
- + raise ArgumentError, "+options[:#{attribute}]+ must be true or false, but was #{value.inspect}", caller(2)
- + end
- - # validate the order, fields, links, includes and conditions options
- - ([ :order, :fields, :links, :includes ] & options.keys).each do |attribute|
- - value = options[attribute]
- - assert_kind_of "options[:#{attribute}]", value, Array
- + # validate the offset and limit options
- + elsif [:offset, :limit].include? attribute
- + assert_kind_of "options[:#{attribute}]", value, Integer
- + if attribute == :offset && value < 0
- + raise ArgumentError, "+options[:offset]+ must be greater than or equal to 0, but was #{value.inspect}", caller(2)
- + elsif attribute == :limit && value < 1
- + raise ArgumentError, "+options[:limit]+ must be greater than or equal to 1, but was #{options[:limit].inspect}", caller(2)
- + end
- - if value.empty?
- - if attribute == :fields
- - if options[:unique] == false
- - raise ArgumentError, '+options[:fields]+ cannot be empty if +options[:unique] is false', caller(2)
- + # validate the :order, :fields, :links and :includes options
- + elsif [ :order, :fields, :links, :includes ].include? attribute
- + assert_kind_of "options[:#{attribute}]", value, Array
- +
- + if value.empty?
- + if attribute == :fields
- + if options[:unique] == false
- + raise ArgumentError, '+options[:fields]+ cannot be empty if +options[:unique] is false', caller(2)
- + end
- + elsif attribute == :order
- + if options[:fields] && options[:fields].any? { |p| !p.kind_of?(Operator) }
- + raise ArgumentError, '+options[:order]+ cannot be empty if +options[:fields] contains a non-operator', caller(2)
- + end
- + else
- + raise ArgumentError, "+options[:#{attribute}]+ cannot be empty", caller(2)
- end
- - elsif attribute == :order
- - if options[:fields] && options[:fields].any? { |p| !p.kind_of?(Operator) }
- - raise ArgumentError, '+options[:order]+ cannot be empty if +options[:fields] contains a non-operator', caller(2)
- - end
- - else
- - raise ArgumentError, "+options[:#{attribute}]+ cannot be empty", caller(2)
- end
- - end
- - end
- - if options.has_key?(:conditions)
- - value = options[:conditions]
- - assert_kind_of 'options[:conditions]', value, Hash, Array
- + # validates the :conditions option
- + elsif :conditions == attribute
- + assert_kind_of 'options[:conditions]', value, Hash, Array
- - if value.empty?
- - raise ArgumentError, '+options[:conditions]+ cannot be empty', caller(2)
- + if value.empty?
- + raise ArgumentError, '+options[:conditions]+ cannot be empty', caller(2)
- + end
- end
- end
- end
- diff --git a/lib/dm-core/repository.rb b/lib/dm-core/repository.rb
- index 2f0627f..85e9616 100755
- --- a/lib/dm-core/repository.rb
- +++ b/lib/dm-core/repository.rb
- @@ -33,11 +33,11 @@ module DataMapper
- end
- def identity_map(model)
- - @identity_maps[model]
- + @identity_maps[model] ||= IdentityMap.new
- end
- # TODO: spec this
- - def scope(&block)
- + def scope
- Repository.context << self
- begin
- @@ -97,7 +97,7 @@ module DataMapper
- assert_kind_of 'name', name, Symbol
- @name = name
- - @identity_maps = Hash.new { |h,model| h[model] = IdentityMap.new }
- + @identity_maps = {}
- end
- # TODO: move to dm-more/dm-migrations
- diff --git a/lib/dm-core/resource.rb b/lib/dm-core/resource.rb
- index d011a60..308b9c3 100644
- --- a/lib/dm-core/resource.rb
- +++ b/lib/dm-core/resource.rb
- @@ -588,7 +588,7 @@ module DataMapper
- raise IncompleteResourceError, "#{model.name} must have a key."
- end
- - class << self; @_valid_model = true; end
- + self.class.instance_variable_set("@_valid_model", true)
- end
- # TODO document
- @@ -627,8 +627,8 @@ module DataMapper
- # @api public
- #
- # TODO: move to dm-more/dm-transactions
- - def transaction(&block)
- - model.transaction(&block)
- + def transaction
- + model.transaction { |*block_args| yield(*block_args) }
- end
- end # module Transaction
- diff --git a/lib/dm-core/scope.rb b/lib/dm-core/scope.rb
- index 6c90cb9..646f51c 100644
- --- a/lib/dm-core/scope.rb
- +++ b/lib/dm-core/scope.rb
- @@ -17,13 +17,13 @@ module DataMapper
- protected
- # @api semipublic
- - def with_scope(query, &block)
- + def with_scope(query)
- # merge the current scope with the passed in query
- - with_exclusive_scope(self.query ? self.query.merge(query) : query, &block)
- + with_exclusive_scope(self.query ? self.query.merge(query) : query) {|*block_args| yield(*block_args) }
- end
- # @api semipublic
- - def with_exclusive_scope(query, &block)
- + def with_exclusive_scope(query)
- query = DataMapper::Query.new(repository, self, query) if query.kind_of?(Hash)
- scope_stack << query
- diff --git a/lib/dm-core/support/kernel.rb b/lib/dm-core/support/kernel.rb
- index 33a6272..2fda366 100644
- --- a/lib/dm-core/support/kernel.rb
- +++ b/lib/dm-core/support/kernel.rb
- @@ -1,7 +1,11 @@
- module Kernel
- # Delegates to DataMapper::repository.
- # Will not overwrite if a method of the same name is pre-defined.
- - def repository(*args, &block)
- - DataMapper.repository(*args, &block)
- + def repository(*args)
- + if block_given?
- + DataMapper.repository(*args) { |*block_args| yield(*block_args) }
- + else
- + DataMapper.repository(*args)
- + end
- end
- end # module Kernel
- diff --git a/lib/dm-core/transaction.rb b/lib/dm-core/transaction.rb
- index 732466a..1329fb0 100644
- --- a/lib/dm-core/transaction.rb
- +++ b/lib/dm-core/transaction.rb
- @@ -13,12 +13,12 @@ module DataMapper
- # In fact, it just calls #link with the given arguments at the end of the
- # constructor.
- #
- - def initialize(*things, &block)
- + def initialize(*things)
- @transaction_primitives = {}
- @state = :none
- @adapters = {}
- link(*things)
- - commit(&block) if block_given?
- + commit { |*block_args| yield(*block_args) } if block_given?
- end
- #
- @@ -39,7 +39,7 @@ module DataMapper
- # within this transaction. The transaction will begin and commit around
- # the block, and rollback if an exception is raised.
- #
- - def link(*things, &block)
- + def link(*things)
- raise "Illegal state for link: #{@state}" unless @state == :none
- things.each do |thing|
- if thing.is_a?(Array)
- @@ -56,7 +56,7 @@ module DataMapper
- raise "Unknown argument to #{self}#link: #{thing.inspect}"
- end
- end
- - return commit(&block) if block_given?
- + return commit { |*block_args| yield(*block_args) } if block_given?
- return self
- end
- @@ -83,12 +83,12 @@ module DataMapper
- # If no block is given, it will simply commit any changes made since the
- # Transaction did #begin.
- #
- - def commit(&block)
- + def commit
- if block_given?
- raise "Illegal state for commit with block: #{@state}" unless @state == :none
- begin
- self.begin
- - rval = within(&block)
- + rval = within { |*block_args| yield(*block_args) }
- self.commit if @state == :begin
- return rval
- rescue Exception => e
- @@ -128,7 +128,7 @@ module DataMapper
- # adapter it is associated with, and it will ensures that it will pop the
- # Transaction away again after the block is finished.
- #
- - def within(&block)
- + def within
- raise "No block provided" unless block_given?
- raise "Illegal state for within: #{@state}" unless @state == :begin
- @adapters.each do |adapter, state|
- diff --git a/script/performance.rb b/script/performance.rb
- index acd2588..47536e5 100755
- --- a/script/performance.rb
- +++ b/script/performance.rb
- @@ -1,6 +1,7 @@
- #!/usr/bin/env ruby
- require File.join(File.dirname(__FILE__), '..', 'lib', 'dm-core')
- +require File.join(File.dirname(__FILE__), '..', 'lib', 'dm-core', 'version')
- require 'rubygems'
- require 'ftools'
- @@ -131,7 +132,7 @@ puts "Benchmarks will now run #{TIMES} times"
- RBench.run(TIMES) do
- column :times
- - column :dm, :title => "DM 0.9.4"
- + column :dm, :title => "DM #{DataMapper::VERSION}"
- column :ar, :title => "AR 2.1"
- column :diff, :compare => [:dm,:ar]
- @@ -183,6 +184,13 @@ RBench.run(TIMES) do
- ar { ARExhibit.create(create_exhibit) }
- end
- + report "Resource#attributes" do
- + attrs_first = {:name => 'sam', :zoo_id => 1}
- + attrs_second = {:name => 'tom', :zoo_id => 1}
- + dm { e = Exhibit.new(attrs_first); e.attributes = attrs_second }
- + ar { e = ARExhibit.new(attrs_first); e.attributes = attrs_second }
- + end
- +
- report "Resource#update" do
- dm { e = Exhibit.get(1); e.name = 'bob'; e.save }
- ar { e = ARExhibit.find(1); e.name = 'bob'; e.save }
Add Comment
Please, Sign In to add comment