Class: Progressive::Specification

Inherits:
Object
  • Object
show all
Defined in:
lib/progressive/specification.rb

Defined Under Namespace

Classes: Event, State

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Specification

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

Raises:

  • (MissingConfiguration)


53
54
55
56
57
58
59
60
# File 'lib/progressive/specification.rb', line 53

def initialize(options = {}, &block)
  raise MissingConfiguration if block.nil?

  @options = options
  @states = {}

  instance_eval(&block)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



42
43
44
# File 'lib/progressive/specification.rb', line 42

def options
  @options
end

#statesObject (readonly)

Returns the value of attribute states.



43
44
45
# File 'lib/progressive/specification.rb', line 43

def states
  @states
end

Instance Method Details

#default_stateObject

Public: Returns the default state for the specification.

Returns symbol.



104
105
106
107
108
109
110
111
# File 'lib/progressive/specification.rb', line 104

def default_state
  @default_state ||=
    if options.key?(:default)
      options[:default]
    elsif states.any?
      states.keys.first
    end
end

#event?(event) ⇒ Boolean

Public: Determine if an event exists within the specification.

event - Event to check for.

Returns true if exists, false if not.

Returns:

  • (Boolean)


67
68
69
# File 'lib/progressive/specification.rb', line 67

def event?(event)
  event_names.include?(event.to_sym)
end

#event_namesObject

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.



76
77
78
79
80
# 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) ⇒ Object

Public: Defines states

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

Returns Progression::Specification::State



88
89
90
# File 'lib/progressive/specification.rb', line 88

def state(state, &block)
  @states[state.to_sym] = State.new(&block)
end

#state?(state) ⇒ Boolean

Public: Determine if a given state has been defined.

state - The state name to check for.

Returns true if defined, false if not.

Returns:

  • (Boolean)


97
98
99
# File 'lib/progressive/specification.rb', line 97

def state?(state)
  @states.key?(state.to_sym)
end