Class: Workflow::State

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:

  • name (Symbol)

    Name of the state being created. Must be unique within its workflow.

  • all_states (Array)

    All states defined for this workflow, used for sorting.

  • meta (Hash)

    Optional metadata for this 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: [], **meta)
  @name = name.to_sym
  @all_states = all_states
  @events = []
  @tags = [tags].flatten.uniq
  unless @tags.reject { |t| t.is_a? Symbol }
    raise WorkflowDefinitionError, "Tags can only include symbols, state: [#{name}]"
  end

  meta.each do |meta_name, value|
    class_eval do
      attr_accessor meta_name
    end
    instance_variable_set("@#{meta_name}", value)
  end
end

Instance Attribute Details

#eventsArray (readonly)

Returns Array of Events defined for this state.

Returns:

  • (Array)

    Array of Events defined for this state.



15
# File 'lib/workflow/state.rb', line 15

attr_reader :name, :events, :tags

#nameSymbol (readonly)

Returns The name of the state.

Returns:

  • (Symbol)

    The name of the state.



15
16
17
# File 'lib/workflow/state.rb', line 15

def name
  @name
end

#tagsObject (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

Returns:

  • (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.

Parameters:

Returns:

  • (Integer)


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.

Parameters:

  • name (Symbol)

    name of event to find

Returns:



42
43
44
# File 'lib/workflow/state.rb', line 42

def find_event(name)
  events.find { |t| t.name == name }
end

#inspectString

Returns String representation of object.

Returns:

  • (String)

    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

Parameters:

  • name (Symbol)

    The name of the event

  • to (Symbol) (defaults to: nil)

    Optional name of Workflow::State this event will transition to. Must be omitted if a block is provided.

  • meta (Hash)

    Optional hash of metadata to be stored on the event object.

Yields:

  • [] Transitions definition for this event.

Returns:

  • (nil)


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: [], **meta, &transitions)
  check_can_add_transition!(name, to: to, &transitions)
  event = Workflow::Event.new(name, tags: 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