class Progressive::Specification

Attributes

options[R]
states[R]

Public Class Methods

new(options = {}, &block) click to toggle source

Public: Define the different states and events the subject can go through.

options - The Hash options used to build the specification (default: {}):

:default - The default state the subject is instantiated at (optional).

block - A required block that is used to define states and events for

the subject.

Returns Progression::Specification

# File lib/progressive/specification.rb, line 53
def initialize(options = {}, &block)
  raise MissingConfiguration if block.nil?

  @options = options
  @states = ActiveSupport::HashWithIndifferentAccess.new

  instance_eval(&block)
end

Public Instance Methods

default_state() click to toggle source

Public: Returns the default state for the specification.

Returns symbol.

# File lib/progressive/specification.rb, line 104
def default_state
  @default_state ||=
    if options.key?(:default)
      options[:default].to_s
    elsif states.any?
      states.keys.first
    end
end
event?(event) click to toggle source

Public: Determine if an event exists within the specification.

event - Event to check for.

Returns true if exists, false if not.

# File lib/progressive/specification.rb, line 67
def event?(event)
  event_names.include?(event.to_s)
end
event_names() click to toggle source

Public: All possible events that can be applied to the subject. Doesn’t gaurantee it can progress to said states, but that they exist for the subject.

Returns Array of Symbols.

# File lib/progressive/specification.rb, line 76
def event_names
  @event_names ||= @states.collect do |_, state|
    state.events.keys
  end.flatten.uniq
end
state(state, &block) click to toggle source

Public: Defines states

state - The name of the state block - block that is used to define events for the state.

Returns Progression::Specification::State

# File lib/progressive/specification.rb, line 88
def state(state, &block)
  @states[state.to_sym] = State.new(&block)
end
state?(state) click to toggle source

Public: Determine if a given state has been defined.

state - The state name to check for.

Returns true if defined, false if not.

# File lib/progressive/specification.rb, line 97
def state?(state)
  @states.key?(state)
end