Class: StateFu::TransitionQuery

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/transition_query.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (TransitionQuery) initialize(binding, options = {})



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

- (Object) method_missing(method_name, *args, &block)

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

- (Object) args

Returns the value of attribute args



3
4
5
# File 'lib/transition_query.rb', line 3

def args
  @args
end

- (Object) binding

Returns the value of attribute binding



3
4
5
# File 'lib/transition_query.rb', line 3

def binding
  @binding
end

- (Object) block

Returns the value of attribute block



3
4
5
# File 'lib/transition_query.rb', line 3

def block
  @block
end

- (Object) options

Returns the value of attribute options



3
4
5
# File 'lib/transition_query.rb', line 3

def options
  @options
end

- (Object) result=(value)

Sets the attribute result



3
4
5
# File 'lib/transition_query.rb', line 3

def result=(value)
  @result = value
end

Instance Method Details

- (Object) cyclic

Chainable Filters



42
43
44
45
# File 'lib/transition_query.rb', line 42

def cyclic
  @options.merge! :cyclic => true
  self
end

- (Object) each(*a, &b)



13
14
15
# File 'lib/transition_query.rb', line 13

def each *a, &b
  result.each *a, &b
end

- (Object) events



121
122
123
# File 'lib/transition_query.rb', line 121

def events
  map {|t| t.event }.extend EventArray
end

- (Object) find(destination = nil, &block)

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)
  # use the prepared event & target, and block, if none are supplied
  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

- (Object) for_event(event)



68
69
70
71
# File 'lib/transition_query.rb', line 68

def for_event event
  @options.merge! :event => event
  self
end

- (Object) next



102
103
104
105
# File 'lib/transition_query.rb', line 102

def next
  @options[:cyclic] ||= false
  singular
end

- (Object) next_event



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

- (Object) next_state



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

- (Object) not_cyclic



47
48
49
50
# File 'lib/transition_query.rb', line 47

def not_cyclic
  @options.merge! :cyclic => false
  self
end

- (Object) not_valid Also known as: invalid



57
58
59
60
# File 'lib/transition_query.rb', line 57

def not_valid
  @options.merge! :valid => false
  self
end

- (Object) simple



73
74
75
76
# File 'lib/transition_query.rb', line 73

def simple
  @options.merge! :simple => true
  self
end

- (Object) singular



94
95
96
# File 'lib/transition_query.rb', line 94

def singular
  result.first if result.length == 1
end

- (Boolean) singular?



98
99
100
# File 'lib/transition_query.rb', line 98

def singular?
  !!singular
end

- (Object) targets



125
126
127
# File 'lib/transition_query.rb', line 125

def targets
  map {|t| t.target }.extend StateArray
end

- (Object) to(state)



63
64
65
66
# File 'lib/transition_query.rb', line 63

def to state
  @options.merge! :target => state
  self
end

- (Object) valid



52
53
54
55
# File 'lib/transition_query.rb', line 52

def valid
  @options.merge! :valid => true
  self
end

- (Object) with(*args, &block)

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