Class: Workflow::Specification

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/workflow/specification.rb

Overview

Metadata object describing available states and state transitions.

Defined Under Namespace

Modules: StateTagHelpers

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

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

    Metadata

Yields:

  • [] Block for workflow definition



59
60
61
62
63
64
65
# File 'lib/workflow/specification.rb', line 59

def initialize(meta = {}, &specification)
  @states = []
  @meta = meta
  run_callbacks :spec_definition do
    instance_eval(&specification)
  end
end

Instance Attribute Details

#initial_stateState (readonly)

State object to be given to newly created objects under this workflow.

Returns:



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

def initial_state
  @initial_state
end

#metaHash (readonly)

Optional metadata stored with this workflow specification

Returns:

  • (Hash)


21
22
23
# File 'lib/workflow/specification.rb', line 21

def meta
  @meta
end

#named_argumentsArray (readonly)

List of symbols, for attribute accessors to be added to TransitionContext object

Returns:

  • (Array)


25
26
27
# File 'lib/workflow/specification.rb', line 25

def named_arguments
  @named_arguments
end

#statesHash (readonly)

The state objects defined for this specification, keyed by name

Returns:

  • (Hash)


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

Returns:

  • (nil)


116
117
118
# File 'lib/workflow/specification.rb', line 116

def define_revert_events!
  @define_revert_events = true
end

#define_revert_events?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/workflow/specification.rb', line 124

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.

Parameters:

  • names (Array)

    A list of symbols

Returns:

  • (nil)


89
90
91
# File 'lib/workflow/specification.rb', line 89

def event_args(*names)
  @named_arguments = names
end

#find_state(name) ⇒ Workflow::State

Find the state with the given name.

Parameters:

  • name (Symbol)

    Name of state to find.

Returns:



50
51
52
# File 'lib/workflow/specification.rb', line 50

def find_state(name)
  states.find { |t| t.name == name.to_sym }
end

#state(name, tags: [], meta: {}) { ... } ⇒ nil

Define a new state named [name]

Parameters:

  • name (Symbol)

    name of state

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

    Metadata to be stored with the state within the Workflow::Specification object

  • tags (Array) (defaults to: [])

    Tags to apply to the Workflow::State object

Yields:

  • [] block defining events for this state.

Returns:

  • (nil)


74
75
76
77
78
79
80
# File 'lib/workflow/specification.rb', line 74

def state(name, tags: [], meta: {}, &events)
  name = name.to_sym
  new_state = Workflow::State.new(name, @states.length, tags: tags, meta: meta)
  @initial_state ||= new_state
  @states << new_state
  new_state.instance_eval(&events) if block_given?
end

#unique_event_namesObject



120
121
122
# File 'lib/workflow/specification.rb', line 120

def unique_event_names
  states.collect(&:events).flatten.collect(&:name).flatten.uniq
end