Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Simplified logic to determine if a model/resource method exists
Browse files Browse the repository at this point in the history
[#1282 state:resolved]
  • Loading branch information
dkubb committed May 22, 2010
1 parent afe1a49 commit acd2f5c
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 38 deletions.
2 changes: 1 addition & 1 deletion lib/dm-core/collection.rb
Expand Up @@ -1441,7 +1441,7 @@ def sliced_query(offset, limit)
def method_missing(method, *args, &block)
relationships = self.relationships

if model.model_method_defined?(method)
if model.respond_to?(method)
delegate_to_model(method, *args, &block)
elsif relationship = relationships[method] || relationships[method.to_s.singularize.to_sym]
delegate_to_relationship(relationship, *args)
Expand Down
32 changes: 0 additions & 32 deletions lib/dm-core/model.rb
Expand Up @@ -668,16 +668,6 @@ def repositories
[ repository ].to_set + @properties.keys.map { |repository_name| DataMapper.repository(repository_name) }
end

# @api private
def model_method_defined?(method)
model_methods.include?(method.to_s)
end

# @api private
def resource_method_defined?(method)
resource_methods.include?(method.to_s)
end

private

# @api private
Expand Down Expand Up @@ -774,28 +764,6 @@ def assert_valid(force = false) # :nodoc:
end
end

# @api private
def model_methods
@model_methods ||= ancestor_instance_methods { |mod| mod.singleton_class }
end

# @api private
def resource_methods
@resource_methods ||= ancestor_instance_methods { |mod| mod }
end

# @api private
def ancestor_instance_methods
methods = Set.new

ancestors.each do |mod|
next unless mod <= DataMapper::Resource
methods.merge(yield(mod).instance_methods(false).map { |method| method.to_s })
end

methods
end

# Raises an exception if #get receives the wrong number of arguments
#
# @param [Array] key
Expand Down
6 changes: 3 additions & 3 deletions lib/dm-core/model/property.rb
Expand Up @@ -224,7 +224,7 @@ def create_reader_for(property)
instance_variable_name = property.instance_variable_name
primitive = property.primitive

unless resource_method_defined?(name)
unless method_defined?(name)
class_eval <<-RUBY, __FILE__, __LINE__ + 1
chainable do
#{reader_visibility}
Expand All @@ -239,7 +239,7 @@ def #{name}

boolean_reader_name = "#{name}?"

if primitive == TrueClass && !resource_method_defined?(boolean_reader_name)
if primitive == TrueClass && !method_defined?(boolean_reader_name)
class_eval <<-RUBY, __FILE__, __LINE__ + 1
#{reader_visibility}
alias #{boolean_reader_name} #{name}
Expand All @@ -256,7 +256,7 @@ def create_writer_for(property)

writer_name = "#{name}="

return if resource_method_defined?(writer_name)
return if method_defined?(writer_name)

class_eval <<-RUBY, __FILE__, __LINE__ + 1
chainable do
Expand Down
4 changes: 2 additions & 2 deletions lib/dm-core/model/relationship.rb
Expand Up @@ -328,7 +328,7 @@ def create_relationship_reader(relationship)
name = relationship.name
reader_name = name.to_s

return if resource_method_defined?(reader_name)
return if method_defined?(reader_name)

reader_visibility = relationship.reader_visibility

Expand All @@ -355,7 +355,7 @@ def create_relationship_writer(relationship)
name = relationship.name
writer_name = "#{name}="

return if resource_method_defined?(writer_name)
return if method_defined?(writer_name)

writer_visibility = relationship.writer_visibility

Expand Down

0 comments on commit acd2f5c

Please sign in to comment.