class DataMapper::Query::Conditions::InclusionComparison

Tests whether the value in the record is contained in the expected set for the Comparison, where expected is an Array, Range, or Set.

Public Instance Methods

valid?() click to toggle source

Checks that the Comparison is valid

@see DataMapper::Query::Conditions::AbstractComparison#valid?

@return [Boolean]

@api semipublic

# File lib/dm-core/query/conditions/comparison.rb, line 583
def valid?
  loaded_value = self.loaded_value
  case loaded_value
    when Collection then valid_collection?(loaded_value)
    when Range      then valid_range?(loaded_value)
    when Enumerable then valid_enumerable?(loaded_value)
    else
      false
  end
end

Private Instance Methods

comparator_string() click to toggle source

@return [String]

@see DataMapper::Query::Conditions::AbstractComparison#to_s

@api private

# File lib/dm-core/query/conditions/comparison.rb, line 713
def comparator_string
  'IN'
end
dump() click to toggle source

Dumps the given val using subject#value

@return [Array<Object>]

@see AbtractComparison#dump

@api private

# File lib/dm-core/query/conditions/comparison.rb, line 697
def dump
  loaded_value = self.loaded_value
  if subject.respond_to?(:dump) && loaded_value.respond_to?(:map) && !loaded_value.kind_of?(Range)
    dumped_value = loaded_value.map { |value| dump_property(value) }
    dumped_value.uniq!
    dumped_value
  else
    super
  end
end
expected() click to toggle source

Overloads AbtractComparison#expected

@return [Array<Object>] @see AbtractComparison#expected

@api private

# File lib/dm-core/query/conditions/comparison.rb, line 607
def expected
  loaded_value = self.loaded_value
  if loaded_value.kind_of?(Range)
    typecast_range(loaded_value)
  elsif loaded_value.respond_to?(:map)
    # FIXME: causes a lazy load when a Collection
    loaded_value.map { |val| super(val) }
  else
    super
  end
end
match_property?(record) click to toggle source

@api private

# File lib/dm-core/query/conditions/comparison.rb, line 597
def match_property?(record)
  super(record, :include?)
end
typecast_collection(collection) click to toggle source

@api private

# File lib/dm-core/query/conditions/comparison.rb, line 672
def typecast_collection(collection)
  collection
end
typecast_enumerable(enumerable) click to toggle source

@api private

# File lib/dm-core/query/conditions/comparison.rb, line 677
def typecast_enumerable(enumerable)
  collection = nil
  enumerable.each do |entry|
    typecasted = typecast_relationship(entry)
    if collection
      collection |= typecasted
    else
      collection = typecasted
    end
  end
  collection
end
typecast_hash(hash) click to toggle source

@api private

# File lib/dm-core/query/conditions/comparison.rb, line 661
def typecast_hash(hash)
  subject = self.subject
  subject.target_model.all(subject.query.merge(hash))
end
typecast_property(value) click to toggle source

@api private

# File lib/dm-core/query/conditions/comparison.rb, line 635
def typecast_property(value)
  if value.kind_of?(Range)
    typecast_range(value)
  elsif value.respond_to?(:map) && !value.kind_of?(String)
    value.map { |entry| super(entry) }
  else
    super
  end
end
typecast_range(range) click to toggle source

@api private

# File lib/dm-core/query/conditions/comparison.rb, line 646
def typecast_range(range)
  range.class.new(typecast_property(range.first), typecast_property(range.last), range.exclude_end?)
end
typecast_relationship(value) click to toggle source

@api private

# File lib/dm-core/query/conditions/comparison.rb, line 651
def typecast_relationship(value)
  case value
    when Hash       then typecast_hash(value)
    when Resource   then typecast_resource(value)
    when Collection then typecast_collection(value)
    when Enumerable then typecast_enumerable(value)
  end
end
typecast_resource(resource) click to toggle source

@api private

# File lib/dm-core/query/conditions/comparison.rb, line 667
def typecast_resource(resource)
  resource.collection_for_self
end
valid_collection?(collection) click to toggle source

@api private

# File lib/dm-core/query/conditions/comparison.rb, line 620
def valid_collection?(collection)
  valid_for_subject?(collection)
end
valid_enumerable?(enumerable) click to toggle source

@api private

# File lib/dm-core/query/conditions/comparison.rb, line 630
def valid_enumerable?(enumerable)
  (!enumerable.empty? || negated?) && enumerable.all? { |entry| valid_for_subject?(entry) }
end
valid_range?(range) click to toggle source

@api private

# File lib/dm-core/query/conditions/comparison.rb, line 625
def valid_range?(range)
  (range.any? || negated?) && valid_for_subject?(range.first) && valid_for_subject?(range.last)
end