Class: StateFu::TransitionQuery

Inherits:
Object
  • Object
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

calling result() will cause the set of transitions to be calculated - the cat will then be either dead or alive; until then it’s a litte from column A, a little from column B.



20
21
22
23
24
25
26
# File 'lib/transition_query.rb', line 20

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

#argsObject

Returns the value of attribute args.



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

def args
  @args
end

#bindingObject

Returns the value of attribute binding.



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

def binding
  @binding
end

#blockObject

Returns the value of attribute block.



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

def block
  @block
end

#optionsObject

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

Parameters:

  • value

    the value to set the attribute result to.



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

def result=(value)
  @result = value
end

Instance Method Details

#cyclicObject

Chainable Filters



50
51
52
53
# File 'lib/transition_query.rb', line 50

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

#eventsObject



129
130
131
# File 'lib/transition_query.rb', line 129

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.



91
92
93
94
95
96
97
98
99
100
# File 'lib/transition_query.rb', line 91

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

#for_event(event) ⇒ Object



76
77
78
79
# File 'lib/transition_query.rb', line 76

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

#nextObject



110
111
112
113
# File 'lib/transition_query.rb', line 110

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

#next_eventObject



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

def next_event
  @options[:cyclic] ||= false
  if result.map(&:event).uniq.length == 1
    result.first.event
  end
end

#next_stateObject



115
116
117
118
119
120
# File 'lib/transition_query.rb', line 115

def next_state
  @options[:cyclic] ||= false
  if result.map(&:target).uniq.length == 1
    result.first.target
  end
end

#not_cyclicObject



55
56
57
58
# File 'lib/transition_query.rb', line 55

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

#not_validObject Also known as: invalid



65
66
67
68
# File 'lib/transition_query.rb', line 65

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

#simpleObject



81
82
83
84
# File 'lib/transition_query.rb', line 81

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

#singularObject



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

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

#singular?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/transition_query.rb', line 106

def singular?
  !!singular
end

#targetsObject



133
134
135
# File 'lib/transition_query.rb', line 133

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

#to(state) ⇒ Object



71
72
73
74
# File 'lib/transition_query.rb', line 71

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

#validObject



60
61
62
63
# File 'lib/transition_query.rb', line 60

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



30
31
32
33
34
# File 'lib/transition_query.rb', line 30

def with(*args, &block)
  @args  = args
  @block = block if block_given?
  self
end