Sha256: 628eb6ff9dbc97c0a4e8a64b336a4bbfce818ba1b7f6f9bdbc63d4a95fc64180

Contents?: true

Size: 1.66 KB

Versions: 6

Compression:

Stored size: 1.66 KB

Contents

require 'workflow/state'
require 'workflow/event'
require 'workflow/event_collection'
require 'workflow/errors'

module Workflow
  class Specification
    attr_accessor :states, :initial_state, :meta,
      :on_transition_proc, :before_transition_proc, :after_transition_proc, :on_error_proc

    def initialize(meta = {}, &specification)
      @states = Hash.new
      @meta = meta
      instance_eval(&specification)
    end

    def state_names
      states.keys
    end

    private

    def state(name, meta = {:meta => {}}, &events_and_etc)
      # meta[:meta] to keep the API consistent..., gah
      new_state = Workflow::State.new(name, self, meta[:meta])
      @initial_state = new_state if @states.empty?
      @states[name.to_sym] = new_state
      @scoped_state = new_state
      instance_eval(&events_and_etc) if events_and_etc
    end

    def event(name, args = {}, &action)
      target = args[:transitions_to] || args[:transition_to]
      condition = args[:if]
      raise WorkflowDefinitionError.new(
        "missing ':transitions_to' in workflow event definition for '#{name}'") \
        if target.nil?
      @scoped_state.events.push(
        name, Workflow::Event.new(name, target, condition, (args[:meta] or {}), &action)
      )
    end

    def on_entry(&proc)
      @scoped_state.on_entry = proc
    end

    def on_exit(&proc)
      @scoped_state.on_exit = proc
    end

    def after_transition(&proc)
      @after_transition_proc = proc
    end

    def before_transition(&proc)
      @before_transition_proc = proc
    end

    def on_transition(&proc)
      @on_transition_proc = proc
    end

    def on_error(&proc)
      @on_error_proc = proc
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
workflow-3.0.0 lib/workflow/specification.rb
workflow-2.0.2 lib/workflow/specification.rb
workflow-2.0.1 lib/workflow/specification.rb
workflow-2.0.0 lib/workflow/specification.rb
workflow-2.0.0.pre lib/workflow/specification.rb
workflow-1.2.0 lib/workflow/specification.rb