/
module.rb
37 lines (31 loc) · 1.29 KB
/
module.rb
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
# The original Module class.
#
class Module
def forward *methods
to = extract_to_from_options methods,
"Forwarding needs a target. Supply an options hash with a :to key as the last argument (e.g. forward :something, :to => :an_array_reader)."
forwarding methods do |method|
"def #{method}(*args, &block)\n#{to}.__send__(#{method.inspect}, *args, &block)\nend\n"
end
end
def each_forward *methods
to = extract_to_from_options methods,
"Multi forwarding needs a target. Supply an options hash with a :to key as the last argument (e.g. each_forward :something, :to => :an_array_reader)."
forwarding methods do |method|
"def #{method}(*args, &block)\n#{to}.each{ |t| t.__send__(#{method.inspect}, *args, &block) }\nend\n"
end
end
private
def extract_to_from_options args, error_string
options = args.pop
unless options.is_a?(Hash) && to = options[:to]
raise ArgumentError, "Multi forwarding needs a target. Supply an options hash with a :to key as the last argument (e.g. each_forward :something, :to => :an_array_reader)."
end
to
end
def forwarding methods, &method_definition
methods.each do |method|
module_eval method_definition[method], "(__FORWARDING__)", 1
end
end
end