Sha256: 433122a2eea25d81fb2dbb3ad06002c9b138775e97f550cd94dca284c9f4a165

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 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, :on_unavailable_transition_proc

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

    def state_names
      states.keys
    end

    private

    def state(name, value=nil, options=nil, &events_and_etc)

      if value.is_a? Hash
        value = nil
        options = value
      end

      value ||= name
      options ||= {}
      meta.reverse_merge! meta: {}

      new_state = Workflow::State.new(name, value, self, options[: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

    def on_unavailable_transition(&proc)
      @on_unavailable_transition_proc = proc
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
workflow-orchestrator-1.3.1 lib/workflow/specification.rb
workflow-orchestrator-1.3.0 lib/workflow/specification.rb