Class: Workflow::State
- Inherits:
-
Object
- Object
- Workflow::State
- Includes:
- Comparable
- Defined in:
- lib/workflow/state.rb
Instance Attribute Summary collapse
-
#events ⇒ Object
Returns the value of attribute events.
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#name ⇒ Object
Returns the value of attribute name.
-
#on_entry ⇒ Object
Returns the value of attribute on_entry.
-
#on_exit ⇒ Object
Returns the value of attribute on_exit.
-
#sequence ⇒ Object
readonly
Returns the value of attribute sequence.
Instance Method Summary collapse
- #<=>(other_state) ⇒ Object
- #find_event(name) ⇒ Object
-
#initialize(name, sequence, **meta) ⇒ State
constructor
A new instance of State.
- #inspect ⇒ Object
-
#on(name, to: nil, meta: nil, &transitions) ⇒ nil
Define an event on this specification.
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, **) @name, @sequence, @events, = name.to_sym, sequence, [], end |
Instance Attribute Details
#events ⇒ Object
Returns the value of attribute events.
5 6 7 |
# File 'lib/workflow/state.rb', line 5 def events @events end |
#meta ⇒ Object
Returns the value of attribute meta.
5 6 7 |
# File 'lib/workflow/state.rb', line 5 def end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/workflow/state.rb', line 5 def name @name end |
#on_entry ⇒ Object
Returns the value of attribute on_entry.
5 6 7 |
# File 'lib/workflow/state.rb', line 5 def on_entry @on_entry end |
#on_exit ⇒ Object
Returns the value of attribute on_exit.
5 6 7 |
# File 'lib/workflow/state.rb', line 5 def on_exit @on_exit end |
#sequence ⇒ Object (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 |
#inspect ⇒ Object
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
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, ) 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 |