Sha256: c751e25b15e20e6a8814494b3326f5faf6a74beed238ce673c3287e400542b95

Contents?: true

Size: 874 Bytes

Versions: 1

Compression:

Stored size: 874 Bytes

Contents

module Mutator
  class Transition
    attr_reader :to, :from, :machine

    def initialize(opts)
      require_parameters!(opts)
      @to, @from, @machine = opts[:to], opts[:from], opts[:machine]
    end

    def call
      stateholder.state = to if valid?
    end

    def valid?
      transitions.length > 0
    end

    def stateholder
      machine.stateholder
    end

    def ==(other)
      to == other.to && from == other.from && machine == other.machine
    end

    def eql?(other)
      public_send(:==, other)
    end

  protected

    def transitions
      machine.transitions.select do |transition|
        transition[:to] == to && transition[:from].include?(from)
      end
    end

    def require_parameters!(opts)
      [:to, :from, :machine].each do |attr|
        fail ArgumentError, "must provide #{attr}" unless opts[attr]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mutator-0.1.0 lib/mutator/transition.rb