Class: Workflow::Specification
- Inherits:
-
Object
- Object
- Workflow::Specification
- Includes:
- ActiveSupport::Callbacks
- Defined in:
- lib/workflow/specification.rb
Overview
Metadata object describing available states and state transitions.
Defined Under Namespace
Modules: StateTagHelpers, TaggedWith
Instance Attribute Summary collapse
-
#events ⇒ Array
readonly
The event objects defined for this specification.
-
#initial_state ⇒ State
readonly
State object to be given to newly created objects under this workflow.
-
#meta ⇒ Hash
readonly
Optional metadata stored with this workflow specification.
-
#named_arguments ⇒ Array
readonly
List of symbols, for attribute accessors to be added to TransitionContext object.
-
#states ⇒ Array
readonly
The state objects defined for this specification.
Instance Method Summary collapse
-
#define_revert_events! ⇒ nil
Also create additional event transitions that will move each configured transition in the reverse direction.
- #define_revert_events? ⇒ Boolean
-
#event_args(*names) ⇒ nil
Specify attributes to make available on the TransitionContext object during transitions taking place in this specification.
-
#event_names ⇒ Array
Array of unique event names defined for this workflow spec.
-
#find_state(name) ⇒ Workflow::State
Find the state with the given name.
- #initialize(meta = {}) { ... } ⇒ Specification constructor private
-
#state(name, tags: [], **meta) { ... } ⇒ nil
Define a new state named [name].
-
#state_names ⇒ Array
Names of states defined on this workflow spec.
- #unique_event_names ⇒ Object
Constructor Details
#initialize(meta = {}) { ... } ⇒ Specification
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.
75 76 77 78 79 80 81 |
# File 'lib/workflow/specification.rb', line 75 def initialize( = {}, &specification) @states = [] = run_callbacks :spec_definition do instance_eval(&specification) end end |
Instance Attribute Details
#events ⇒ Array (readonly)
The event objects defined for this specification
17 18 19 |
# File 'lib/workflow/specification.rb', line 17 def events @events end |
#initial_state ⇒ State (readonly)
State object to be given to newly created objects under this workflow.
21 22 23 |
# File 'lib/workflow/specification.rb', line 21 def initial_state @initial_state end |
#meta ⇒ Hash (readonly)
Optional metadata stored with this workflow specification
25 26 27 |
# File 'lib/workflow/specification.rb', line 25 def end |
#named_arguments ⇒ Array (readonly)
List of symbols, for attribute accessors to be added to TransitionContext object
29 30 31 |
# File 'lib/workflow/specification.rb', line 29 def named_arguments @named_arguments end |
#states ⇒ Array (readonly)
The state objects defined for this specification
13 14 15 |
# File 'lib/workflow/specification.rb', line 13 def states @states end |
Instance Method Details
#define_revert_events! ⇒ nil
Also create additional event transitions that will move each configured transition in the reverse direction.
class Article
include Workflow
workflow do
define_revert_events!
state :foo do
on :bar, to: :bax
end
state :bax
end
end
a = Article.new
a.transition! :foo
a.current_state.name # => :bax
a.transition! :revert_bar
a.current_state.name # => :foo
132 133 134 |
# File 'lib/workflow/specification.rb', line 132 def define_revert_events! @define_revert_events = true end |
#define_revert_events? ⇒ Boolean
140 141 142 |
# File 'lib/workflow/specification.rb', line 140 def define_revert_events? @define_revert_events end |
#event_args(*names) ⇒ nil
Specify attributes to make available on the TransitionContext object during transitions taking place in this specification. The attributes' values will be taken in order from the arguments passed to the event transit method call.
105 106 107 |
# File 'lib/workflow/specification.rb', line 105 def event_args(*names) @named_arguments = names end |
#event_names ⇒ Array
Returns Array of unique event names defined for this workflow spec.
61 62 63 |
# File 'lib/workflow/specification.rb', line 61 def event_names events.map(&:name).uniq end |
#find_state(name) ⇒ Workflow::State
Find the state with the given name.
56 57 58 |
# File 'lib/workflow/specification.rb', line 56 def find_state(name) states.find { |t| t.name == name.to_sym } end |
#state(name, tags: [], **meta) { ... } ⇒ nil
Define a new state named [name]
90 91 92 93 94 95 96 |
# File 'lib/workflow/specification.rb', line 90 def state(name, tags: [], **, &events) name = name.to_sym new_state = Workflow::State.new(name, @states, tags: , **) @initial_state ||= new_state @states << new_state new_state.instance_eval(&events) if block_given? end |
#state_names ⇒ Array
Returns Names of states defined on this workflow spec.
66 67 68 |
# File 'lib/workflow/specification.rb', line 66 def state_names states.map(&:name) end |
#unique_event_names ⇒ Object
136 137 138 |
# File 'lib/workflow/specification.rb', line 136 def unique_event_names states.collect(&:events).flatten.collect(&:name).flatten.uniq end |