Sha256: 6a0d9ca6c7dc85d664f6094628ba72758e4273196db0da3db4e537c38b7fedd2

Contents?: true

Size: 1.87 KB

Versions: 6

Compression:

Stored size: 1.87 KB

Contents

require 'active_support/core_ext/module/delegation'

module Shoulda
  module Matchers
    module ActiveModel
      module NumericalityMatchers
        # @private
        class RangeMatcher < ValidationMatcher
          OPERATORS = [:>=, :<=].freeze

          delegate :failure_message, to: :submatchers

          def initialize(numericality_matcher, attribute, range)
            super(attribute)
            unless numericality_matcher.respond_to? :diff_to_compare
              raise ArgumentError, 'numericality_matcher is invalid'
            end

            @numericality_matcher = numericality_matcher
            @range = range
            @attribute = attribute
          end

          def matches?(subject)
            @subject = subject
            submatchers.matches?(subject)
          end

          def simple_description
            description = ''

            if expects_strict?
              description << ' strictly'
            end

            description +
              "disallow :#{attribute} from being a number that is not " +
              range_description
          end

          def range_description
            "from #{Shoulda::Matchers::Util.inspect_range(@range)}"
          end

          def submatchers
            @_submatchers ||= NumericalityMatchers::Submatchers.new(build_submatchers)
          end

          private

          def build_submatchers
            submatcher_combos.map do |value, operator|
              build_comparison_submatcher(value, operator)
            end
          end

          def submatcher_combos
            @range.minmax.zip(OPERATORS)
          end

          def build_comparison_submatcher(value, operator)
            ComparisonMatcher.new(@numericality_matcher, value, operator).
              for(@attribute).
              with_message(@message).
              on(@context)
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
shoulda-matchers-6.4.0 lib/shoulda/matchers/active_model/numericality_matchers/range_matcher.rb
shoulda-matchers-6.3.1 lib/shoulda/matchers/active_model/numericality_matchers/range_matcher.rb
shoulda-matchers-6.3.0 lib/shoulda/matchers/active_model/numericality_matchers/range_matcher.rb
shoulda-matchers-6.2.0 lib/shoulda/matchers/active_model/numericality_matchers/range_matcher.rb
shoulda-matchers-6.1.0 lib/shoulda/matchers/active_model/numericality_matchers/range_matcher.rb
shoulda-matchers-6.0.0 lib/shoulda/matchers/active_model/numericality_matchers/range_matcher.rb