Class: Workflow::State

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/workflow/state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, sequence, **meta) ⇒ State

Returns a new instance of State.



8
9
10
# File 'lib/workflow/state.rb', line 8

def initialize(name, sequence, **meta)
  @name, @sequence, @events, @meta = name.to_sym, sequence, [], meta
end

Instance Attribute Details

#eventsObject

Returns the value of attribute events.



5
6
7
# File 'lib/workflow/state.rb', line 5

def events
  @events
end

#metaObject

Returns the value of attribute meta.



5
6
7
# File 'lib/workflow/state.rb', line 5

def meta
  @meta
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/workflow/state.rb', line 5

def name
  @name
end

#on_entryObject

Returns the value of attribute on_entry.



5
6
7
# File 'lib/workflow/state.rb', line 5

def on_entry
  @on_entry
end

#on_exitObject

Returns the value of attribute on_exit.



5
6
7
# File 'lib/workflow/state.rb', line 5

def on_exit
  @on_exit
end

#sequenceObject (readonly)

Returns the value of attribute sequence.



6
7
8
# File 'lib/workflow/state.rb', line 6

def sequence
  @sequence
end

Instance Method Details

#<=>(other_state) ⇒ Object



81
82
83
84
85
86
# File 'lib/workflow/state.rb', line 81

def <=>(other_state)
  unless other_state.is_a?(State)
    raise StandardError.new "Other State #{other_state} is a #{other_state.class}.  I can only be compared with a Workflow::State."
  end
  self.sequence <=> other_state.sequence
end

#find_event(name) ⇒ Object



12
13
14
# File 'lib/workflow/state.rb', line 12

def find_event(name)
  events.find{|t| t.name == name}
end

#inspectObject



77
78
79
# File 'lib/workflow/state.rb', line 77

def inspect
  "<State name=#{name.inspect} events(#{events.length})=#{events.inspect}>"
end

#on(name, to: nil, meta: nil, &transitions) ⇒ nil

Define an event on this specification. Must be called within the scope of the block within a call to #state.

workflow do
 state :new do
   on :review, to: :being_reviewed

   on :submit do
     to :submitted,
       if:     [ "name == 'The Dude'", :abides?, -> (rug) {rug.tied_the_room_together?}],
       unless: :nihilist?

     to :trash, unless: :body?
     to :another_place do |article|
       article.foo?
     end
  end
end

 state :kitchen
 state :the_bar
 state :the_diner
end

Parameters:

  • name (Symbol)

    The name of the event

  • args (Hash)

Returns:

  • (nil)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/workflow/state.rb', line 48

def on(name, to: nil, meta: nil, &transitions)
  if to && block_given?
    raise Errors::WorkflowDefinitionError.new("Event target can only be received in the method call or the block, not both.")
  end

  unless to || block_given?
    raise Errors::WorkflowDefinitionError.new("No event target given for event #{name}")
  end

  if find_event(name)
    raise Errors::WorkflowDefinitionError.new("Already defined an event [#{name}] for state[#{self.name}]")
  end

  event = Workflow::Event.new(name, meta)

  if to
    event.to to
  else
    event.instance_eval(&transitions)
  end

  if event.transitions.empty?
    raise Errors::WorkflowDefinitionError.new("No transitions defined for event [#{name}] on state [#{self.name}]")
  end

  events << event
  nil
end