Class: Workflow::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/workflow/event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, transitions_to, condition = nil, meta = {}, &action) ⇒ Event

Returns a new instance of Event.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/workflow/event.rb', line 6

def initialize(name, transitions_to, condition = nil, meta = {}, &action)
  @name = name
  @transitions_to = transitions_to.to_sym
  @meta = meta
  @action = action
  @condition = if condition.nil? || condition.is_a?(Symbol) || condition.respond_to?(:call)
                 condition
               else
                 raise TypeError, 'condition must be nil, an instance method name symbol or a callable (eg. a proc or lambda)'
               end
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



4
5
6
# File 'lib/workflow/event.rb', line 4

def action
  @action
end

#conditionObject

Returns the value of attribute condition.



4
5
6
# File 'lib/workflow/event.rb', line 4

def condition
  @condition
end

#metaObject

Returns the value of attribute meta.



4
5
6
# File 'lib/workflow/event.rb', line 4

def meta
  @meta
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/workflow/event.rb', line 4

def name
  @name
end

#transitions_toObject

Returns the value of attribute transitions_to.



4
5
6
# File 'lib/workflow/event.rb', line 4

def transitions_to
  @transitions_to
end

Instance Method Details

#condition_applicable?(object) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/workflow/event.rb', line 18

def condition_applicable?(object)
  if condition
    if condition.is_a?(Symbol)
      object.send(condition)
    else
      condition.call(object)
    end
  else
    true
  end
end

#draw(graph, from_state) ⇒ Object



30
31
32
# File 'lib/workflow/event.rb', line 30

def draw(graph, from_state)
  graph.add_edges(from_state.name.to_s, transitions_to.to_s, meta.merge(:label => to_s))
end

#to_sObject



34
35
36
# File 'lib/workflow/event.rb', line 34

def to_s
  @name.to_s
end