Sha256: dc35f4fc43fca566b17547901c091ff68bd34073e3424ccd481593cc46b55756

Contents?: true

Size: 843 Bytes

Versions: 2

Compression:

Stored size: 843 Bytes

Contents

module Mutator
  class Machine
    attr_reader :stateholder

    def initialize(stateholder)
      @stateholder = stateholder
    end

    def valid?
      self.class.states.include? current_state
    end

    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)

      if transition.valid?
        stateholder.state = to
        success.call(transition)
        true
      else
        failure.call(transition)
        false
      end
    end

    def self.states
      self.transitions.map do |t|
        [t[:to], t[:from]]
      end.flatten.uniq
    end

    def states
      self.class.states
    end

    def transitions
      self.class.transitions
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mutator-0.0.2 lib/mutator/machine.rb
mutator-0.0.1 lib/mutator/machine.rb