Class: Progressive::Specification
- Inherits:
-
Object
- Object
- Progressive::Specification
- Defined in:
- lib/progressive/specification.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#states ⇒ Object
readonly
Returns the value of attribute states.
Instance Method Summary collapse
-
#default_state ⇒ Object
Public: Returns the default state for the specification.
-
#event?(event) ⇒ Boolean
Public: Determine if an event exists within the specification.
-
#event_names ⇒ Object
Public: All possible events that can be applied to the subject.
-
#initialize(options = {}, &block) ⇒ Specification
constructor
Public: Define the different states and events the subject can go through.
-
#state(state, &block) ⇒ Object
Public: Defines states.
-
#state?(state) ⇒ Boolean
Public: Determine if a given state has been defined.
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
53 54 55 56 57 58 59 60 |
# File 'lib/progressive/specification.rb', line 53 def initialize( = {}, &block) raise MissingConfiguration if block.nil? = @states = {} instance_eval(&block) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
42 43 44 |
# File 'lib/progressive/specification.rb', line 42 def end |
#states ⇒ Object (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_state ⇒ Object
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 .key?(:default) [: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.
67 68 69 |
# File 'lib/progressive/specification.rb', line 67 def event?(event) event_names.include?(event.to_sym) end |
#event_names ⇒ Object
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.
97 98 99 |
# File 'lib/progressive/specification.rb', line 97 def state?(state) @states.key?(state.to_sym) end |