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

Instance Method Summary collapse

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

Parameters:

  • name (Symbol)

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

  • sequence (Fixnum)

    Sort location among states on this workflow.

  • meta (Hash) (defaults to: {})

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

Instance Attribute Details

#eventsArray (readonly)

Returns Array of Events defined for this state.

Returns:

  • (Array)

    Array of Events defined for this state.



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

attr_reader :name, :events, :meta, :tags

#metaHash (readonly)

Returns Extra information defined for this state.

Returns:

  • (Hash)

    Extra information defined for this state.



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

attr_reader :name, :events, :meta, :tags

#nameSymbol (readonly)

Returns The name of the state.

Returns:

  • (Symbol)

    The name of the state.



17
18
19
# File 'lib/workflow/state.rb', line 17

def name
  @name
end

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

Parameters:

Returns:

  • (Integer)

Raises:



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.

Parameters:

  • name (Symbol)

    name of event to find

Returns:



38
39
40
# File 'lib/workflow/state.rb', line 38

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

#inspectString

Returns String representation of object.

Returns:

  • (String)

    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

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) (defaults to: {})

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

Yields:

  • [] Transitions definition for this event.

Returns:

  • (nil)


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: tags, meta: 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