Sha256: ae88a1496f4886e14bb24083fb01b553b11c5b56a78483bfadd76b7705915b22

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

require 'active_support/core_ext'

module StateManager
  module DSL

    module State
      # Specifies a state that is a child of the current state
      def state(name, klass=nil, &block)
        # If no base class is specified we look for a class inside the current
        # state's class which has the same name as the state
        const_name = name.capitalize
        klass ||= if const_defined?(const_name)
          self.const_get(name.capitalize)
        else
          Class.new(StateManager::State)
        end
        klass = Class.new(klass, &block) if block_given?

        remove_const const_name if const_defined?(const_name)
        const_set(const_name, klass)

        specification.states[name.to_sym] = klass
      end

      # Specifies an event on the current state
      def event(name, options={}, &block)
        name = name.to_sym
        event = options.dup
        event[:name] = name
        specification.events[name] = event
        define_method name, &block if block_given?
      end

      # The initial state
      def initial_state(value)
        specification.initial_state = value
      end
    end

    module Base
      def resource_class(value)
        self._resource_class = value
      end

      def resource_name(value)
        self._resource_name = value
        create_resource_accessor!(_resource_name)
      end

      def state_property(value)
        self._state_property = value
      end
    end
    
  end

  class State
    extend DSL::State
  end

  class Base
    extend DSL::Base
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
state_manager-0.2.4 lib/state_manager/dsl.rb