Sha256: c0137726b9e8124e4f74fd33632803ff7e0e795a40e51787d63c7fdd9761cdda

Contents?: true

Size: 1008 Bytes

Versions: 7

Compression:

Stored size: 1008 Bytes

Contents

module Workflow
  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

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
workflow-3.0.0 lib/workflow/event.rb
workflow-2.0.2 lib/workflow/event.rb
workflow-2.0.1 lib/workflow/event.rb
workflow-2.0.0 lib/workflow/event.rb
workflow-2.0.0.pre lib/workflow/event.rb
workflow-orchestrator-1.3.1 lib/workflow/event.rb
workflow-orchestrator-1.3.0 lib/workflow/event.rb