123456789_123456789_123456789_123456789_123456789_

Module: ActiveModel::Validations::Clusivity

Do not use. This module is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, ResolveValue
Defined in: activemodel/lib/active_model/validations/clusivity.rb

Constant Summary

Instance Method Summary

ResolveValue - Included

Instance Method Details

#check_validity!

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations/clusivity.rb', line 14

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

#delimiter (private)

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations/clusivity.rb', line 31

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

#include?(record, value) ⇒ Boolean (private)

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations/clusivity.rb', line 21

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

#inclusion_method(enumerable) (private)

After Ruby 2.2, Range#include? on non-number-or-time-ish ranges checks all possible values in the range for equality, which is slower but more accurate. Range#cover? 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.

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations/clusivity.rb', line 40

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