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.
-
#name ⇒ Symbol
readonly
The name of the state.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Class Method Summary collapse
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Overloaded comparison operator.
-
#find_event(name) ⇒ Workflow::Event
Returns the event with the given name.
-
#initialize(name, all_states, 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, all_states, 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
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/workflow/state.rb', line 22 def initialize(name, all_states, tags: [], **) @name = name.to_sym @all_states = all_states @events = [] @tags = [].flatten.uniq unless @tags.reject { |t| t.is_a? Symbol } raise WorkflowDefinitionError, "Tags can only include symbols, state: [#{name}]" end .each do |, value| class_eval do attr_accessor end instance_variable_set("@#{}", value) end end |
Instance Attribute Details
#events ⇒ Array (readonly)
Returns Array of Events defined for this state.
15 |
# File 'lib/workflow/state.rb', line 15 attr_reader :name, :events, :tags |
#name ⇒ Symbol (readonly)
Returns The name of the state.
15 16 17 |
# File 'lib/workflow/state.rb', line 15 def name @name end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
15 |
# File 'lib/workflow/state.rb', line 15 attr_reader :name, :events, :tags |
Class Method Details
.beyond?(other) ⇒ Boolean
97 98 99 |
# File 'lib/workflow/state.rb', line 97 def beyond?(other) -> { current_state > other } end |
Instance Method Details
#<=>(other) ⇒ Integer
Overloaded comparison operator. Workflow states are sorted according to the order in which they were defined.
122 123 124 125 126 127 |
# File 'lib/workflow/state.rb', line 122 def <=>(other) if other.is_a?(Symbol) other = all_states.find { |state| state.name == other } end all_states.index(self) <=> all_states.index(other) end |
#find_event(name) ⇒ Workflow::Event
Returns the event with the given name.
42 43 44 |
# File 'lib/workflow/state.rb', line 42 def find_event(name) events.find { |t| t.name == name } end |
#inspect ⇒ String
Returns String representation of object.
113 114 115 |
# File 'lib/workflow/state.rb', line 113 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
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/workflow/state.rb', line 78 def on(name, to: nil, tags: [], **, &transitions) check_can_add_transition!(name, to: to, &transitions) event = Workflow::Event.new(name, tags: , **) 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 |