Module: Overload

Defined in:
lib/overload.rb,
lib/overload/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#kept_methodsObject

Returns the value of attribute kept_methods.



6
7
8
# File 'lib/overload.rb', line 6

def kept_methods
  @kept_methods
end

Instance Method Details

#method_added(new_method) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/overload.rb', line 15

def method_added(new_method)
  if @evil_things
    # Maybe next time.
    @evil_things = false
    return
  end

  # To allow inheriting of overloaded methods
  if kept_methods.nil?
    self.ancestors.each_with_index do |ancestor, index|
      if ancestor.respond_to?(:kept_methods) && ancestor.kept_methods
        # To prevent changing overloaded methods on the parent, we must dup
        self.kept_methods = self.ancestors[index].kept_methods.deep_dup
      end
    end
  end

  return unless kept_methods[new_method]

  new_method_object = new.method(new_method)

  if method_defined?(new_method)
    kept_methods[new_method][new_method_object.arity] = new_method_object
    instance_variable_set(:@kept_methods, kept_methods)

    overload_method = Proc.new do |*args|
      kept_methods = self.class.instance_variable_get(:@kept_methods)
      if kept_methods[__method__][args.count]
        kept_methods[__method__][args.count].call(*args)
      else
        kept_methods[__method__].keys.sort.each do |arity|
          next unless arity < 0 && arity.abs - 1 <= args.count
          return kept_methods[__method__][arity].call(*args)
        end
      end
    end

    # This was fun, but once was quite enough.
    @evil_things = true

    define_method new_method, overload_method

  end
end

#overload(*methods) ⇒ Object



8
9
10
11
12
13
# File 'lib/overload.rb', line 8

def overload(*methods)
  @kept_methods ||= {}
  methods.each do |method|
    kept_methods[method] = {}
  end
end