Class: Mutator::Machine
- Inherits:
-
Object
- Object
- Mutator::Machine
- Defined in:
- lib/mutator/machine.rb
Instance Attribute Summary collapse
-
#stateholder ⇒ Object
readonly
Returns the value of attribute stateholder.
Class Method Summary collapse
Instance Method Summary collapse
- #current_state ⇒ Object
-
#initialize(stateholder) ⇒ Machine
constructor
A new instance of Machine.
- #states ⇒ Object
- #transition(to:, success: lambda { |_transition| }, failure: lambda { |_transition| }) ⇒ Object
- #transitions ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(stateholder) ⇒ Machine
Returns a new instance of Machine.
5 6 7 |
# File 'lib/mutator/machine.rb', line 5 def initialize(stateholder) @stateholder = stateholder end |
Instance Attribute Details
#stateholder ⇒ Object (readonly)
Returns the value of attribute stateholder.
3 4 5 |
# File 'lib/mutator/machine.rb', line 3 def stateholder @stateholder end |
Class Method Details
.states ⇒ Object
30 31 32 33 34 |
# File 'lib/mutator/machine.rb', line 30 def self.states self.transitions.map do |t| [t[:to], t[:from]] end.flatten.uniq end |
Instance Method Details
#current_state ⇒ Object
13 14 15 |
# File 'lib/mutator/machine.rb', line 13 def current_state stateholder.state end |
#states ⇒ Object
36 37 38 |
# File 'lib/mutator/machine.rb', line 36 def states self.class.states end |
#transition(to:, success: lambda { |_transition| }, failure: lambda { |_transition| }) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/mutator/machine.rb', line 17 def transition(to:, success: lambda { |_transition| }, failure: lambda { |_transition| }) transition = Transition.new(to: to, from: current_state, machine: self) if transition.valid? stateholder.state = to success.call(transition) true else failure.call(transition) false end end |
#transitions ⇒ Object
40 41 42 |
# File 'lib/mutator/machine.rb', line 40 def transitions self.class.transitions end |
#valid? ⇒ Boolean
9 10 11 |
# File 'lib/mutator/machine.rb', line 9 def valid? self.class.states.include? current_state end |