Class: StateFu::TransitionQuery
- Inherits:
-
Object
- Object
- StateFu::TransitionQuery
show all
- Includes:
- Enumerable
- Defined in:
- lib/transition_query.rb
Defined Under Namespace
Modules: Result
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(binding, options = {}) ⇒ TransitionQuery
Returns a new instance of TransitionQuery.
5
6
7
8
9
|
# File 'lib/transition_query.rb', line 5
def initialize(binding, options={})
defaults = { :valid => true, :cyclic => nil }
@options = defaults.merge(options).symbolize_keys
@binding = binding
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
this is a cheap way of passing any call to #each, #length, etc to result(). TODO: be explicit, eg using delegate, and remove method_missing.
22
23
24
25
26
27
28
|
# File 'lib/transition_query.rb', line 22
def method_missing(method_name, *args, &block)
if result.respond_to?(method_name, true)
result.__send__(method_name, *args, &block)
else
super(method_name, *args, &block)
end
end
|
Instance Attribute Details
Returns the value of attribute args.
3
4
5
|
# File 'lib/transition_query.rb', line 3
def args
@args
end
|
Returns the value of attribute binding.
3
4
5
|
# File 'lib/transition_query.rb', line 3
def binding
@binding
end
|
Returns the value of attribute block.
3
4
5
|
# File 'lib/transition_query.rb', line 3
def block
@block
end
|
Returns the value of attribute options.
3
4
5
|
# File 'lib/transition_query.rb', line 3
def options
@options
end
|
#result=(value) ⇒ Object
Sets the attribute result
3
4
5
|
# File 'lib/transition_query.rb', line 3
def result=(value)
@result = value
end
|
Instance Method Details
42
43
44
45
|
# File 'lib/transition_query.rb', line 42
def cyclic
@options.merge! :cyclic => true
self
end
|
#each(*a, &b) ⇒ Object
13
14
15
|
# File 'lib/transition_query.rb', line 13
def each *a, &b
result.each *a, &b
end
|
121
122
123
|
# File 'lib/transition_query.rb', line 121
def events
map {|t| t.event }.extend EventArray
end
|
#find(destination = nil, &block) ⇒ Object
find a transition by event and optionally (optional if it can be inferred) target.
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/transition_query.rb', line 83
def find(destination=nil, &block)
event, target = destination.nil? ? [options[:event], options[:target]] : parse_destination(destination)
block ||= @block
returning Transition.new(binding, event, target, &block) do |transition|
if @args
transition.args = @args
end
end
end
|
#for_event(event) ⇒ Object
68
69
70
71
|
# File 'lib/transition_query.rb', line 68
def for_event event
@options.merge! :event => event
self
end
|
102
103
104
105
|
# File 'lib/transition_query.rb', line 102
def next
@options[:cyclic] ||= false
singular
end
|
#next_event ⇒ Object
114
115
116
117
118
119
|
# File 'lib/transition_query.rb', line 114
def next_event
@options[:cyclic] ||= false
if result.map(&:event).uniq.length == 1
result.first.event
end
end
|
#next_state ⇒ Object
107
108
109
110
111
112
|
# File 'lib/transition_query.rb', line 107
def next_state
@options[:cyclic] ||= false
if result.map(&:target).uniq.length == 1
result.first.target
end
end
|
#not_cyclic ⇒ Object
47
48
49
50
|
# File 'lib/transition_query.rb', line 47
def not_cyclic
@options.merge! :cyclic => false
self
end
|
#not_valid ⇒ Object
Also known as:
invalid
57
58
59
60
|
# File 'lib/transition_query.rb', line 57
def not_valid
@options.merge! :valid => false
self
end
|
73
74
75
76
|
# File 'lib/transition_query.rb', line 73
def simple
@options.merge! :simple => true
self
end
|
94
95
96
|
# File 'lib/transition_query.rb', line 94
def singular
result.first if result.length == 1
end
|
#singular? ⇒ Boolean
98
99
100
|
# File 'lib/transition_query.rb', line 98
def singular?
!!singular
end
|
125
126
127
|
# File 'lib/transition_query.rb', line 125
def targets
map {|t| t.target }.extend StateArray
end
|
#to(state) ⇒ Object
63
64
65
66
|
# File 'lib/transition_query.rb', line 63
def to state
@options.merge! :target => state
self
end
|
52
53
54
55
|
# File 'lib/transition_query.rb', line 52
def valid
@options.merge! :valid => true
self
end
|
#with(*args, &block) ⇒ Object
prepare the query with arguments / block so that they can be applied to the transition once one is selected
32
33
34
35
36
|
# File 'lib/transition_query.rb', line 32
def with(*args, &block)
@args = args
@block = block if block_given?
self
end
|