Sha256: 243c503b9c464131bc16b6cf35dbdd93ec6d78fcd7d2670f09c30b8e30966549

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

module Finite

  # The event class. Represents an event in the state machine
  class Event

    attr_reader :name, :transitions, :callbacks

    # Create an event object
    #
    # @param name [Symbol] the name of the event
    # @param block [Block] the block of code in the event
    def initialize(name, &block)
      @name = name
      @transitions = Hash.new
      @callbacks = {before: Array.new, after: Array.new}
      instance_eval &block
    end

    # Are two events equal
    #
    # @param event [Object] the object you are comparing it to
    # @return true if they are equal false if not
    def ==(event)
      if event.is_a? Event
        @name == event.name
      elsif event.is_a? Symbol
        @name == event
      else
        false
      end
    end

    # overrriden for puts and print
    def to_s
      @name.to_s
    end

    # Overridden for p
    def inspect
      @name
    end

    private
    # The transition method for the dsl
    #
    # @param opts [Hash] the options for a transition
    def go(opts)
      options = []
      if opts[:from].is_a? Array
        opts[:from].each do |from|
          options << {from: from, to: opts[:to]}
        end
      else
        options << opts
      end
      options.each do |opt|
        @transitions[opt[:from]] = Transition.new(opt)
      end
    end

    # Create the callback methods
    [:after, :before].each do |callback|
      define_method callback do |*args, &block|
        @callbacks[callback] << block
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
finite-1.0.0 lib/finite/event.rb