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
|
# File 'lib/seeing_is_believing/safe.rb', line 3
def self.build(klass, *method_names)
options = {}
options = method_names.pop if method_names.last.kind_of? ::Hash
Class.new do
class << self
alias [] new
end
define_method :initialize do |instance|
@_instance = instance
end
methods = method_names.map { |name| [name, klass.instance_method(name)] }
methods.each do |name, method|
define_method(name) do |*args, &block|
method.bind(@_instance).call(*args, &block)
end
end
singleton_methods = options.fetch(:class, []).map { |name| [name, klass.method(name)] }
singleton_methods.each do |name, method|
define_singleton_method name do |*args, &block|
method.call(*args, &block)
end
end
end
end
|