Class: Workflow::Event
- Inherits:
-
Object
- Object
- Workflow::Event
- Defined in:
- lib/workflow/event.rb
Defined Under Namespace
Classes: Conditions, Transition
Instance Attribute Summary collapse
-
#meta ⇒ Hash
readonly
Extra information defined for this event.
-
#name ⇒ Symbol
readonly
The name of the event.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#transitions ⇒ Array
readonly
Array of Transitions defined for this event.
Instance Method Summary collapse
-
#evaluate(target) ⇒ Workflow::State
Returns the State that the target object should enter.
- #evaluate!(target) ⇒ Object
-
#initialize(name, tags: [], **meta) ⇒ Event
constructor
private
See State#on for creating objects of this class.
- #inspect ⇒ Object
-
#title ⇒ String
Titleized name of this event.
-
#to(target_state, **conditions_def) { ... } ⇒ nil
Add a Transition to the possible #transitions for this event.
- #valid? ⇒ Boolean
Constructor Details
#initialize(name, tags: [], **meta) ⇒ Event
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.
See State#on for creating objects of this class.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/workflow/event.rb', line 19 def initialize(name, tags: [], **) @name = name.to_sym @transitions = [] @meta = || {} @tags = [].flatten.uniq unless @tags.reject { |t| t.is_a? Symbol } raise WorkflowDefinitionError, "Tags can only include symbols, event: [#{name}]" end .each do |, value| class_eval do attr_accessor end instance_variable_set("@#{}", value) end end |
Instance Attribute Details
#meta ⇒ Hash (readonly)
Returns Extra information defined for this event.
12 |
# File 'lib/workflow/event.rb', line 12 attr_reader :name, :transitions, :meta, :tags |
#name ⇒ Symbol (readonly)
Returns The name of the event.
12 13 14 |
# File 'lib/workflow/event.rb', line 12 def name @name end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
12 |
# File 'lib/workflow/event.rb', line 12 attr_reader :name, :transitions, :meta, :tags |
#transitions ⇒ Array (readonly)
Returns Array of Transitions defined for this event.
12 |
# File 'lib/workflow/event.rb', line 12 attr_reader :name, :transitions, :meta, :tags |
Instance Method Details
#evaluate(target) ⇒ Workflow::State
Returns the State that the target object should enter. This will be the first one in the list of transitions, whose conditions apply to the target object in its present state.
54 55 56 57 58 |
# File 'lib/workflow/event.rb', line 54 def evaluate(target) transitions.find do |transition| transition.matches? target end&.target_state end |
#evaluate!(target) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/workflow/event.rb', line 60 def evaluate!(target) state_name = evaluate(target) unless state_name raise NoMatchingTransitionError, "No matching transition found on #{name} for target #{target}. Consider adding a catchall transition.".squish end state_name end |
#inspect ⇒ Object
45 46 47 |
# File 'lib/workflow/event.rb', line 45 def inspect "<Event name=#{name.inspect} transitions(#{transitions.length})=#{transitions.inspect}>" end |
#title ⇒ String
Returns Titleized name of this event.
37 38 39 |
# File 'lib/workflow/event.rb', line 37 def title name.to_s.titleize end |
#to(target_state, **conditions_def) { ... } ⇒ nil
Add a Transition to the possible #transitions for this event.
82 83 84 |
# File 'lib/workflow/event.rb', line 82 def to(target_state, **conditions_def, &block) transitions << Transition.new(target_state, conditions_def, &block) end |
#valid? ⇒ Boolean
41 42 43 |
# File 'lib/workflow/event.rb', line 41 def valid? transitions.any? end |