lib/mutator/machine.rb in mutator-0.1.0 vs lib/mutator/machine.rb in mutator-0.2.0

- old
+ new

@@ -1,10 +1,10 @@ module Mutator class Machine attr_reader :stateholder - def initialize(stateholder) + def initialize stateholder @stateholder = stateholder end def valid? self.class.states.include? current_state @@ -12,25 +12,25 @@ def current_state stateholder.state end - def transition(options) - options = extract(options) - success, failure, transition = options.values + def transition options + options = extract options + transition, success, failure = options.values if transition.call - success.call(transition) + success.call transition true else - failure.call(transition) + failure.call transition false end end def self.states - self.transitions.map do |transition| + transitions.map do |transition| to, from = transition[:to], transition[:from] [to, from] end.flatten.uniq end @@ -42,21 +42,16 @@ self.class.transitions end protected - def extract(options) - to = options[:to] - fail ArgumentError, 'must provide state to transition to' unless to + def extract options + to = options.fetch(:to) { + transition: Transition.new(to: to, from: current_state, machine: self), success: lambda { |_| }, - failure: lambda { |_| }, - transition: Transition.new( - to: to, - from: current_state, - machine: self - ) - }.merge(options) + failure: lambda { |_| } + }.merge options end end end