Skip to content

Commit

Permalink
Merge pull request #50 from Shopify/optimize-owner_class_ancestor_has…
Browse files Browse the repository at this point in the history
…_method

Optimize owner_class_ancestor_has_method?
  • Loading branch information
rafaelfranca authored Dec 7, 2016
2 parents 616a607 + 36b1c1e commit c9cef47
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/state_machines/machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2046,14 +2046,12 @@ def action_hook
# the method and is further along in the ancestor chain than this
# machine's helper module.
def owner_class_ancestor_has_method?(scope, method)
return false unless owner_class_has_method?(scope, method)

superclasses = owner_class.ancestors[1..-1].select { |ancestor| ancestor.is_a?(Class) }

if scope == :class
# Use singleton classes
current = (
class << owner_class;
self;
end)
current = owner_class.singleton_class
superclass = superclasses.first
else
current = owner_class
Expand All @@ -2068,14 +2066,16 @@ class << owner_class;

# Search for for the first ancestor that defined this method
ancestors.detect do |ancestor|
ancestor = (
class << ancestor;
self;
end) if scope == :class && ancestor.is_a?(Class)
ancestor = ancestor.singleton_class if scope == :class && ancestor.is_a?(Class)
ancestor.method_defined?(method) || ancestor.private_method_defined?(method)
end
end

def owner_class_has_method?(scope, method)
target = scope == :class ? owner_class.singleton_class : owner_class
target.method_defined?(method) || target.private_method_defined?(method)
end

# Adds helper methods for accessing naming information about states and
# events on the owner class
def define_name_helpers
Expand Down

0 comments on commit c9cef47

Please sign in to comment.