Sha256: 706735e11a259406ce511831a2caa4f751cbdc740a34664168dfcd67328863b4

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 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

    # Overridden 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.1.0 lib/finite/event.rb