Sha256: 39e599bc32f0b85180d3ef82bf6d9f1d64b3fee7f76ede1f814eefc26075b690

Contents?: true

Size: 1.31 KB

Versions: 6

Compression:

Stored size: 1.31 KB

Contents

require 'active_support/core_ext/array/extract_options'

module StateMachinesRspec
  module Matchers
    module States
      class Matcher
        attr_reader :failure_message

        def initialize(states)
          @options = states.extract_options!
          @states = states
        end

        def description
          @states.map{ |event| event.inspect }.join(', ')
        end

        def matches?(subject)
          raise_if_multiple_values

          @subject = subject
          @introspector = StateMachinesIntrospector.new(@subject,
                                                       state_machine_scope)

          return false unless matches_states?(@states)
          @failure_message.nil?
        end

        def matches_states?(states)
          raise NotImplementedError,
            "subclasses of #{self.class} must override matches_states?"
        end

        protected

        def state_machine_scope
          @options.fetch(:on, nil)
        end

        def state_value
          @options.fetch(:value, nil)
        end

        private

        def raise_if_multiple_values
          if @states.count > 1 && state_value
            raise ArgumentError, 'cannot make value assertions on ' +
                                 'multiple states at once'
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
state_machines-rspec-0.6.0 lib/matchers/states/matcher.rb
state_machines-rspec-0.5.0 lib/matchers/states/matcher.rb
state_machines-rspec-0.4.0 lib/matchers/states/matcher.rb
state_machines_rspec-0.3.2 lib/matchers/states/matcher.rb
state_machines_rspec-0.3.1 lib/matchers/states/matcher.rb
state_machines_rspec-0.3.0 lib/matchers/states/matcher.rb