lib/mutator/machine.rb in mutator-0.0.2 vs lib/mutator/machine.rb in mutator-0.1.0
- old
+ new
@@ -12,33 +12,51 @@
def current_state
stateholder.state
end
- def transition(to:, success: lambda { |_transition| }, failure: lambda { |_transition| })
- transition = Transition.new(to: to, from: current_state, machine: self)
+ def transition(options)
+ options = extract(options)
+ success, failure, transition = options.values
- if transition.valid?
- stateholder.state = to
+ if transition.call
success.call(transition)
true
else
failure.call(transition)
false
end
end
def self.states
- self.transitions.map do |t|
- [t[:to], t[:from]]
+ self.transitions.map do |transition|
+ to, from = transition[:to], transition[:from]
+ [to, from]
end.flatten.uniq
end
def states
self.class.states
end
def transitions
self.class.transitions
+ end
+
+ protected
+
+ def extract(options)
+ to = options[:to]
+ fail ArgumentError, 'must provide state to transition to' unless to
+
+ {
+ success: lambda { |_| },
+ failure: lambda { |_| },
+ transition: Transition.new(
+ to: to,
+ from: current_state,
+ machine: self
+ )
+ }.merge(options)
end
end
end