Sha256: 94bab961a6929a7e1fe2e66efa687a9a0e320aa8e675322004bcfaf0dbbb16b3

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

class StateMachinesIntrospector
  def initialize(subject, state_machine_name=nil)
    @subject = subject
    @state_machine_name = state_machine_name
  end

  def state_machine_attribute
    state_machine.attribute
  end

  def current_state_value
    @subject.send(state_machine_attribute)
  end

  def state(name)
    state = state_machine.states.find { |s| s.name == name }
  end

  def undefined_states(states)
    states.reject { |s| state_defined? s }
  end

  def defined_states(states)
    states.select { |s| state_defined? s }
  end

  def undefined_events(events)
    events.reject { |e| event_defined? e }
  end

  def valid_events(events)
    events.select { |e| valid_event? e }
  end

  def invalid_events(events)
    events.reject { |e| valid_event? e }
  end

  private

  def state_machine
    if @state_machine_name
      unless machine = @subject.class.state_machines[@state_machine_name]
        raise StateMachinesIntrospectorError,
          "#{@subject.class} does not have a state machine defined " +
          "on #{@state_machine_name}"
      end
    else
      machine = @subject.class.state_machine
    end

    machine
  end

  def state_defined?(state_name)
    state(state_name)
  end

  def event_defined?(event)
    @subject.respond_to? "can_#{event}?"
  end

  def valid_event?(event)
    @subject.send("can_#{event}?")
  end

end

class StateMachinesIntrospectorError < StandardError
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
state_machines_rspec-0.3.2 lib/state_machines_rspec/state_machines_introspector.rb
state_machines_rspec-0.3.1 lib/state_machines_rspec/state_machines_introspector.rb
state_machines_rspec-0.3.0 lib/state_machines_rspec/state_machines_introspector.rb