Sha256: cba4de86f226dba32b605e1d551bef1636979a9dd18813a3549ec6ece40aa7ab

Contents?: true

Size: 1.72 KB

Versions: 8

Compression:

Stored size: 1.72 KB

Contents

# coding: utf-8
# frozen_string_literal: true

module Stealth
  module Flow
    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 = Stealth::Flow::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 StealthFlowDefinitionError.new(
          "missing ':transitions_to' in workflow event definition for '#{name}'") \
          if target.nil?
        @scoped_state.events.push(
          name, Stealth::Flow::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
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
stealth-0.9.8 lib/stealth/flow/specification.rb
stealth-0.9.7 lib/stealth/flow/specification.rb
stealth-0.9.6 lib/stealth/flow/specification.rb
stealth-0.9.5 lib/stealth/flow/specification.rb
stealth-0.9.4 lib/stealth/flow/specification.rb
stealth-0.9.3 lib/stealth/flow/specification.rb
stealth-0.9.2 lib/stealth/flow/specification.rb
stealth-0.9.1 lib/stealth/flow/specification.rb