Sha256: 247f6f34f4063df4213c74cf156e078d0de8eb0c7f2af76cf90843e08202ca63

Contents?: true

Size: 1.62 KB

Versions: 34

Compression:

Stored size: 1.62 KB

Contents

require 'active_support/core_ext/range'

module ActiveModel
  module Validations
    module Clusivity #:nodoc:
      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 = if delimiter.respond_to?(:call)
                    delimiter.call(record)
                  elsif delimiter.respond_to?(:to_sym)
                    record.send(delimiter)
                  else
                    delimiter
                  end

        members.send(inclusion_method(members), value)
      end

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

      # In 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.first
          when Numeric, Time, DateTime, Date
            :cover?
          else
            :include?
          end
        else
          :include?
        end
      end
    end
  end
end

Version data entries

34 entries across 33 versions & 5 rubygems

Version Path
activemodel-5.0.7.2 lib/active_model/validations/clusivity.rb
activemodel-5.0.7.1 lib/active_model/validations/clusivity.rb
activemodel-5.0.7 lib/active_model/validations/clusivity.rb
activemodel-5.0.6 lib/active_model/validations/clusivity.rb
activemodel-5.0.6.rc1 lib/active_model/validations/clusivity.rb
activemodel-5.0.5 lib/active_model/validations/clusivity.rb
activemodel-5.0.5.rc2 lib/active_model/validations/clusivity.rb
activemodel-5.0.5.rc1 lib/active_model/validations/clusivity.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/activemodel-5.0.2/lib/active_model/validations/clusivity.rb
tdiary-5.0.5 vendor/bundle/gems/activemodel-5.0.2/lib/active_model/validations/clusivity.rb
activemodel-5.0.4 lib/active_model/validations/clusivity.rb
activemodel-5.0.4.rc1 lib/active_model/validations/clusivity.rb
activemodel-5.0.3 lib/active_model/validations/clusivity.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activemodel-5.0.2/lib/active_model/validations/clusivity.rb
tdiary-5.0.4 vendor/bundle/gems/activemodel-5.0.2/lib/active_model/validations/clusivity.rb
activemodel-5.0.2 lib/active_model/validations/clusivity.rb
activemodel-5.0.2.rc1 lib/active_model/validations/clusivity.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activemodel-5.0.1/lib/active_model/validations/clusivity.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activemodel-5.0.1/lib/active_model/validations/clusivity.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activemodel-5.0.1/lib/active_model/validations/clusivity.rb