Sha256: fa541ede7b44151185f7029fa9e97a57090c6f22acaf996e9faa7f6a607ea51f

Contents?: true

Size: 1.67 KB

Versions: 42

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

require "active_model/validations/resolve_value"
require "active_support/core_ext/range"

module ActiveModel
  module Validations
    module Clusivity # :nodoc:
      include ResolveValue

      ERROR_MESSAGE = "An object with the method #include? or a proc, lambda or symbol is required, " \
                      "and must be supplied as the :in (or :within) option of the configuration hash"

      def check_validity!
        unless delimiter.respond_to?(:include?) || delimiter.respond_to?(:call) || delimiter.respond_to?(:to_sym)
          raise ArgumentError, ERROR_MESSAGE
        end
      end

    private
      def include?(record, value)
        members = resolve_value(record, delimiter)

        if value.is_a?(Array)
          value.all? { |v| members.public_send(inclusion_method(members), v) }
        else
          members.public_send(inclusion_method(members), value)
        end
      end

      def delimiter
        @delimiter ||= options[:in] || options[:within]
      end

      # After Ruby 2.2, <tt>Range#include?</tt> on non-number-or-time-ish ranges checks all
      # possible values in the range for equality, which is slower but more accurate.
      # <tt>Range#cover?</tt> uses the previous logic of comparing a value with the range
      # endpoints, which is fast but is only accurate on Numeric, Time, Date,
      # or DateTime ranges.
      def inclusion_method(enumerable)
        if enumerable.is_a? Range
          case enumerable.begin || enumerable.end
          when Numeric, Time, DateTime, Date
            :cover?
          else
            :include?
          end
        else
          :include?
        end
      end
    end
  end
end

Version data entries

42 entries across 42 versions & 6 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activemodel-7.1.3.4/lib/active_model/validations/clusivity.rb
activemodel-8.0.1 lib/active_model/validations/clusivity.rb
activemodel-8.0.0.1 lib/active_model/validations/clusivity.rb
activemodel-7.2.2.1 lib/active_model/validations/clusivity.rb
activemodel-7.1.5.1 lib/active_model/validations/clusivity.rb
activemodel-8.0.0 lib/active_model/validations/clusivity.rb
activemodel-7.2.2 lib/active_model/validations/clusivity.rb
activemodel-7.1.5 lib/active_model/validations/clusivity.rb
activemodel-8.0.0.rc2 lib/active_model/validations/clusivity.rb
activemodel-7.2.1.2 lib/active_model/validations/clusivity.rb
activemodel-7.1.4.2 lib/active_model/validations/clusivity.rb
activemodel-8.0.0.rc1 lib/active_model/validations/clusivity.rb
activemodel-7.2.1.1 lib/active_model/validations/clusivity.rb
activemodel-7.1.4.1 lib/active_model/validations/clusivity.rb
activemodel-8.0.0.beta1 lib/active_model/validations/clusivity.rb
omg-activemodel-8.0.0.alpha9 lib/active_model/validations/clusivity.rb
omg-activemodel-8.0.0.alpha8 lib/active_model/validations/clusivity.rb
omg-activemodel-8.0.0.alpha7 lib/active_model/validations/clusivity.rb
omg-activemodel-8.0.0.alpha4 lib/active_model/validations/clusivity.rb
omg-activemodel-8.0.0.alpha3 lib/active_model/validations/clusivity.rb