/
double_definition_creator_proxy.rb
63 lines (56 loc) · 1.6 KB
/
double_definition_creator_proxy.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
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
module RR
module DoubleDefinitions
class DoubleDefinitionCreatorProxy
class << self
def blank_slate_methods
instance_methods.each do |m|
unless m =~ /^_/ || m.to_s == 'object_id' || m.to_s == "instance_eval" || m.to_s == 'respond_to?'
alias_method "__blank_slated_#{m}", m
undef_method m
end
end
end
end
def initialize(creator, &block) #:nodoc:
@creator = creator
class << self
def __apply_blank_slate
@apply_blank_slate = true
end
def __apply_blank_slate?
@apply_blank_slate ||= false
end
blank_slate_methods
def instance_eval
return_value = super
class << self
alias_method "__blank_slated_instance_eval", "instance_eval"
undef_method :instance_eval
alias_method "__blank_slated_respond_to?", "respond_to?"
undef_method :respond_to?
end
return_value
end
def method_missing(method_name, *args, &block)
if __apply_blank_slate?
@creator.create(method_name, *args, &block)
else
__blank_slated_send("__blank_slated_#{method_name}", *args, &block)
end
end
end
__apply_blank_slate
if block_given?
if block.arity == 1
yield(self)
else
instance_eval(&block)
end
end
end
def __creator__
@creator
end
end
end
end