Class: StateFu::Executioner
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(transition) {|_self| ... } ⇒ Executioner
give us a blank slate instance_methods.each { |m| undef_method m unless m =~ /(^__|^self|^nil?$|^send$|proxy_|^object_id|^respond_to?|^instance_exec|^instance_eval|^method$)/ }
22
23
24
25
26
27
28
29
30
|
# File 'lib/executioner.rb', line 22
def initialize transition, &block
@transition = transition
@__target__ = transition.object
@__self___ = self
yield self if block_given?
self
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
Forwards any missing method call to the target. TODO / FIXME / NOTE: we don’t (can’t ?) handle block arguments …
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/executioner.rb', line 116
def method_missing(method_name, *args)
if __target__.respond_to?(method_name, true)
begin
meth = __target__.__send__ :method, method_name
rescue NameError
super
end
__target__.instance_exec( *args, &meth)
else evaluate_with_arguments(method_name, *args)
end
end
|
Instance Attribute Details
Returns the value of attribute __self__.
46
47
48
|
# File 'lib/executioner.rb', line 46
def __self__
@__self__
end
|
#__target__ ⇒ Object
Returns the value of attribute __target__.
46
47
48
|
# File 'lib/executioner.rb', line 46
def __target__
@__target__
end
|
#transition ⇒ Object
Also known as:
t, current_transition, context, ctx
Returns the value of attribute transition.
46
47
48
|
# File 'lib/executioner.rb', line 46
def transition
@transition
end
|
Instance Method Details
42
43
44
|
# File 'lib/executioner.rb', line 42
def binding
transition.binding
end
|
#evaluate(method_name_or_proc) ⇒ Object
91
92
93
94
|
# File 'lib/executioner.rb', line 91
def evaluate method_name_or_proc
arguments = [transition, args, __target__]
evaluate_with_arguments(method_name_or_proc, *arguments)
end
|
#evaluate_with_arguments(method_name_or_proc, *arguments) ⇒ Object
delegate :machine, :to => :transition
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
|
# File 'lib/executioner.rb', line 65
def evaluate_with_arguments method_name_or_proc, *arguments
if method_name_or_proc.is_a?(Proc) && meth = method_name_or_proc
elsif meth = transition.machine.named_procs[method_name_or_proc]
elsif respond_to?( method_name_or_proc) && meth = method(method_name_or_proc)
elsif method_name_or_proc.to_s =~ /^not?_(.*)$/
return !( evaluate_with_arguments $1, *args )
else
raise NoMethodError.new( "undefined method_name `#{method_name_or_proc.to_s}' for \"#{__target__}\":#{__target__.class.to_s}" )
end
if arguments.length < meth.arity.abs && meth.arity != -1
raise ArgumentError.new([meth.arity, arguments.length].inspect)
else
arguments = arguments[0, meth.arity.abs]
end
__target__.with_methods_on(self) do
self.instance_exec *arguments, &meth
end
end
|
#executioner_method ⇒ Object
103
|
# File 'lib/executioner.rb', line 103
alias_method :executioner_method, :method
|
#executioner_respond_to? ⇒ Object
96
|
# File 'lib/executioner.rb', line 96
alias_method :executioner_respond_to?, :respond_to?
|
56
57
58
|
# File 'lib/executioner.rb', line 56
def machine
binding.machine
end
|
#method(method_name) ⇒ Object
104
105
106
107
108
109
110
|
# File 'lib/executioner.rb', line 104
def method method_name
begin
executioner_method(method_name)
rescue NameError
__target__.__send__ :method, method_name
end
end
|
#respond_to?(method_name, include_private = false) ⇒ Boolean
98
99
100
101
|
# File 'lib/executioner.rb', line 98
def respond_to? method_name, include_private = false
executioner_respond_to?(method_name, include_private) ||
__target__.__send__( :respond_to?, method_name, include_private )
end
|
60
61
62
|
# File 'lib/executioner.rb', line 60
def states
machine.states
end
|