Class: Workflow::State
- Inherits:
-
Object
- Object
- Workflow::State
- Includes:
- Comparable
- Defined in:
- lib/workflow/state.rb
Overview
Represents one state for the defined workflow, with a list of Events that can transition to other states.
Instance Attribute Summary collapse
-
#events ⇒ Array
readonly
Array of Events defined for this state.
-
#meta ⇒ Hash
readonly
Extra information defined for this state.
-
#name ⇒ Symbol
readonly
The name of the state.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Overloaded comparison operator.
-
#find_event(name) ⇒ Workflow::Event
Returns the event with the given name.
-
#initialize(name, sequence, tags: [], meta: {}) ⇒ State
constructor
private
For creating State objects please see Workflow::Specification#state.
-
#inspect ⇒ String
String representation of object.
-
#on(name, to: nil, tags: [], meta: {}) { ... } ⇒ nil
Define an event on this specification.
Constructor Details
#initialize(name, sequence, tags: [], meta: {}) ⇒ State
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
For creating Workflow::State objects please see Workflow::Specification#state
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/workflow/state.rb', line 24 def initialize(name, sequence, tags: [], meta: {}) @name = name.to_sym @sequence = sequence @events = [] = = [].flatten.uniq unless .reject { |t| t.is_a? Symbol } raise WorkflowDefinitionError, "Tags can only include symbols, state: [#{name}]" end end |
Instance Attribute Details
#events ⇒ Array (readonly)
Returns Array of Events defined for this state.
17 |
# File 'lib/workflow/state.rb', line 17 attr_reader :name, :events, :meta, :tags |
#meta ⇒ Hash (readonly)
Returns Extra information defined for this state.
17 |
# File 'lib/workflow/state.rb', line 17 attr_reader :name, :events, :meta, :tags |
#name ⇒ Symbol (readonly)
Returns The name of the state.
17 18 19 |
# File 'lib/workflow/state.rb', line 17 def name @name end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
17 |
# File 'lib/workflow/state.rb', line 17 attr_reader :name, :events, :meta, :tags |
Instance Method Details
#<=>(other) ⇒ Integer
Overloaded comparison operator. Workflow states are sorted according to the order in which they were defined.
112 113 114 115 |
# File 'lib/workflow/state.rb', line 112 def <=>(other) raise Errors::StateComparisonError, other unless other.is_a?(State) sequence <=> other.send(:sequence) end |
#find_event(name) ⇒ Workflow::Event
Returns the event with the given name.
38 39 40 |
# File 'lib/workflow/state.rb', line 38 def find_event(name) events.find { |t| t.name == name } end |
#inspect ⇒ String
Returns String representation of object.
103 104 105 |
# File 'lib/workflow/state.rb', line 103 def inspect "<State name=#{name.inspect} events(#{events.length})=#{events.inspect}>" end |
#on(name, to: nil, tags: [], meta: {}) { ... } ⇒ nil
Define an event on this specification. Must be called within the scope of the block within a call to Workflow::Specification#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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/workflow/state.rb', line 74 def on(name, to: nil, tags: [], meta: {}, &transitions) check_can_add_transition!(name, to: to, &transitions) event = Workflow::Event.new(name, tags: , meta: ) if to event.to to else event.instance_eval(&transitions) end unless event.valid? raise Errors::NoTransitionsDefinedError.new(self, event) end events << event nil end |