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

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

#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



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

#eventsObject



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)
  # 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



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

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

#nextObject



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

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

#next_eventObject



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_stateObject



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_cyclicObject



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

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

#not_validObject Also known as: invalid



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

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

#simpleObject



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

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

#singularObject



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

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

#singular?Boolean

Returns:

  • (Boolean)


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

def singular?
  !!singular
end

#targetsObject



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

#validObject



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