Sha256: bac09292357db67bd18da2b118a6adf7c07ae934d418661a539a20849bb0996a

Contents?: true

Size: 965 Bytes

Versions: 2

Compression:

Stored size: 965 Bytes

Contents

module EdgeStateMachine
  class State
    attr_reader :name, :options

    def initialize(name, &block)
      @name = name
      @options = Hash.new
      instance_eval(&block) if block_given?
    end

    def enter(method = nil, &block)
      @options[:enter] = method.nil? ? block : method
    end

    def exit(method = nil, &block)
      @options[:exit] = method.nil? ? block : method
    end

    def execute_action(action, base)
      action = @options[action.to_sym]
      case action
      when Symbol, String
        base.send(action)
      when Proc
        action.call(base)
      end
    end

    def use_display_name(display_name)
      @display_name = display_name
    end

    def display_name
      @display_name ||= name.to_s.gsub(/_/, ' ').capitalize
    end

    def ==(state)
      if state.is_a? Symbol
        name == state
      elsif state.is_a? String
        name == state
      else
        name == state.name
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
edge-state-machine-0.9.0 lib/edge-state-machine/state.rb
edge-state-machine-0.0.3 lib/edge-state-machine/state.rb