File: method_delegation.rb

package info (click to toggle)
ruby-rgen 0.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,428 kB
  • sloc: ruby: 11,344; xml: 1,368; yacc: 72; makefile: 10
file content (114 lines) | stat: -rw-r--r-- 2,814 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
module RGen

module Util
  
module MethodDelegation

class << self
  
  def registerDelegate(delegate, object, method)
    method = method.to_sym
    createDelegateStore(object)
    if object._methodDelegates[method]
      object._methodDelegates[method] << delegate
    else
      object._methodDelegates[method] = [delegate]
      createDelegatingMethod(object, method)
    end
  end
  
  def unregisterDelegate(delegate, object, method)
    method = method.to_sym
    return unless object.respond_to?(:_methodDelegates)
    return unless object._methodDelegates[method]
    object._methodDelegates[method].delete(delegate)
    if object._methodDelegates[method].empty?
      object._methodDelegates[method] = nil
      removeDelegatingMethod(object, method)
      removeDelegateStore(object)
    end
  end

  private
  
  def createDelegateStore(object)
    return if object.respond_to?(:_methodDelegates)
    class << object
      def _methodDelegates
        @_methodDelegates ||= {}
      end
    end
  end
  
  def removeDelegateStore(object)
    return unless object.respond_to?(:_methodDelegates)
    class << object
      remove_method(:_methodDelegates)
    end
  end
  
  def createDelegatingMethod(object, method)
    if hasMethod(object, method)
      object.instance_eval <<-END
        class << self
          alias #{aliasMethodName(method)} #{method}
        end
      END
    end
    
    # define the delegating method
    object.instance_eval <<-END
      class << self
        def #{method}(*args, &block)
          @_methodDelegates[:#{method}].each do |d|
            catch(:continue) do
              return d.#{method}_delegated(self, *args, &block)
            end
          end
          # if aliased method does not exist, we want an exception
          #{aliasMethodName(method)}(*args, &block)
        end
      end
    END
  end

  def removeDelegatingMethod(object, method)
    if hasMethod(object, aliasMethodName(method))
      # there is an aliased original, restore it
      object.instance_eval <<-END
        class << self
          alias #{method} #{aliasMethodName(method)}
          remove_method(:#{aliasMethodName(method)})
        end
      END
    else
      # just delete the delegating method
      object.instance_eval <<-END
        class << self
          remove_method(:#{method})
        end
      END
    end
  end
  
  def hasMethod(object, method)
    # in Ruby 1.9, #methods returns symbols
    if object.methods.first.is_a?(Symbol)
      method = method.to_sym
    else
      method = method.to_s
    end
    object.methods.include?(method)
  end

  def aliasMethodName(method)
    "#{method}_delegate_original"
  end    
end

end

end

end