Sha256: e36ba7de1db8b3aba029aabfa1b7531022d3e53ec5b04e29bc8e1f36affb003f

Contents?: true

Size: 1.11 KB

Versions: 8

Compression:

Stored size: 1.11 KB

Contents

# coding: utf-8
# frozen_string_literal: true

module Stealth
  module Flow
    class Event

      attr_accessor :name, :transitions_to, :meta, :action, :condition

      def initialize(name, transitions_to, condition = nil, meta = {}, &action)
        @name = name
        @transitions_to = transitions_to.to_sym
        @meta = meta
        @action = action
        @condition = if condition.nil? || condition.is_a?(Symbol) || condition.respond_to?(:call)
                       condition
                     else
                       raise TypeError, 'condition must be nil, an instance method name symbol or a callable (eg. a proc or lambda)'
                     end
      end

      def condition_applicable?(object)
        if condition
          if condition.is_a?(Symbol)
            object.send(condition)
          else
            condition.call(object)
          end
        else
          true
        end
      end

      def draw(graph, from_state)
        graph.add_edges(from_state.name.to_s, transitions_to.to_s, meta.merge(:label => to_s))
      end

      def to_s
        @name.to_s
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

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