Sha256: b248b6b48300eeb297f85efcb42677fc50767177249f5c070db1b97730841888

Contents?: true

Size: 1.52 KB

Versions: 10

Compression:

Stored size: 1.52 KB

Contents

require 'active_support/all'

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.to_s.classify
        klass ||= if const_defined?(const_name, false)
          self.const_get(const_name)
        else
          Class.new(StateManager::State)
        end
        klass = Class.new(klass, &block) if block_given?

        remove_const const_name if const_defined?(const_name, false)
        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

10 entries across 10 versions & 1 rubygems

Version Path
state_manager-0.4.0 lib/state_manager/dsl.rb
state_manager-0.3.5 lib/state_manager/dsl.rb
state_manager-0.3.4 lib/state_manager/dsl.rb
state_manager-0.3.3 lib/state_manager/dsl.rb
state_manager-0.3.2 lib/state_manager/dsl.rb
state_manager-0.3.1 lib/state_manager/dsl.rb
state_manager-0.3.0 lib/state_manager/dsl.rb
state_manager-0.2.13 lib/state_manager/dsl.rb
state_manager-0.2.12 lib/state_manager/dsl.rb
state_manager-0.2.11 lib/state_manager/dsl.rb