class MultiDispatch::UnboundMultiMethod

Attributes

body[RW]
name[RW]
patterns[RW]

Public Class Methods

new(name, *args, body) click to toggle source
# File lib/multi_dispatch/unbound_multi_method.rb, line 6
def initialize(name, *args, body)
  @name, @patterns, @body = name, args, body
end

Public Instance Methods

arity() click to toggle source
# File lib/multi_dispatch/unbound_multi_method.rb, line 44
def arity 
  @patterns.size 
end
bind(obj) click to toggle source
# File lib/multi_dispatch/unbound_multi_method.rb, line 40
def bind(obj)
  MultiMethod.new(obj, self)
end
eql?(mthd) click to toggle source
# File lib/multi_dispatch/unbound_multi_method.rb, line 48
def eql?(mthd)
  self.body == mthd.body
end
match?(name, params) click to toggle source
# File lib/multi_dispatch/unbound_multi_method.rb, line 10
def match?(name, params)
  name == @name &&
  params.size == arity &&
  params.zip(@patterns).all? do |param, pattern|
    # match by type (Class)
    (pattern.is_a?(Class) && param.kind_of?(pattern)) ||
    # match by condition passed in Proc object
    (pattern.is_a?(Proc) && pattern.call(param) rescue false) ||
    # match by value
    param == pattern
  end
end
match_distance(params) click to toggle source
# File lib/multi_dispatch/unbound_multi_method.rb, line 23
  def match_distance(params)
    params.zip(@patterns).reduce(0) do |dist, pair|
      prm, ptr = pair
      if prm == ptr ; dist -= 1 
      else
        if (ptr.is_a?(Class) && prm.kind_of?(ptr)) 
          # calculates the distance from given type to parent type
          prm = prm.class
          while (dist+=1 ; prm != ptr) do 
            prm = prm.superclass
          end
        end
      end
      dist
    end
  end
  
  def bind(obj)
    MultiMethod.new(obj, self)
  end

  def arity 
    @patterns.size 
  end

  def eql?(mthd)
    self.body == mthd.body
  end

  def to_s
    "#{self.class}: #{self.class}##{name} (#{@patterns.map { |p| p.is_a?(Proc) ? Proc : p }})"
  end

  def parameters
    @patterns
  end

end
parameters() click to toggle source
# File lib/multi_dispatch/unbound_multi_method.rb, line 56
def parameters
  @patterns
end
to_s() click to toggle source
# File lib/multi_dispatch/unbound_multi_method.rb, line 52
def to_s
  "#{self.class}: #{self.class}##{name} (#{@patterns.map { |p| p.is_a?(Proc) ? Proc : p }})"
end