class DataMapper::Query::Conditions::AbstractComparison
A base class for the various comparison classes.
Attributes
@api private
The loaded/typecast value
In the case of primitive types, this will be the same as
value
, however when using primitive property this stores the
loaded value.
If writing an adapter, you should use value
, while plugin
authors should refer to loaded_value
.
@return [Object]
@api semipublic
@api semipublic
The property or relationship which is being matched against
@return [Property, Associations::Relationship]
@api semipublic
Public Class Methods
Keeps track of AbstractComparison subclasses (used in Comparison)
@return [Set<AbstractComparison>] @api private
# File lib/dm-core/query/conditions/comparison.rb, line 159 def self.descendants @descendants ||= DescendantSet.new end
Registers AbstractComparison subclasses (used in Comparison)
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 166 def self.inherited(descendant) descendants << descendant end
Creates a new AbstractComparison
instance with subject
and value
@param [Property, Associations::Relationship] subject
The subject of the comparison - the value of the subject will be matched against the given value parameter.
@param [Object] value
The value for the comparison.
@api semipublic
# File lib/dm-core/query/conditions/comparison.rb, line 289 def initialize(subject, value) @subject = subject @loaded_value = typecast(value) @dumped_value = dump end
Setter/getter: allows subclasses to easily set their slug
@param [Symbol] slug
The slug to be set for this class. Passing nil returns the current value instead.
@return [Symbol]
The current slug set for the Comparison.
@example Creating a MyComparison compairson with slug :exact.
class MyComparison < AbstractComparison slug :exact end
@api semipublic
# File lib/dm-core/query/conditions/comparison.rb, line 185 def self.slug(slug = nil) slug ? @slug = slug : @slug end
Public Instance Methods
Returns a human-readable representation of this object
@return [String]
@api semipublic
# File lib/dm-core/query/conditions/comparison.rb, line 251 def inspect "#<#{self.class} @subject=#{@subject.inspect} " "@dumped_value=#{@dumped_value.inspect} @loaded_value=#{@loaded_value.inspect}>" end
Test that the record value matches the comparison
@param [Resource, Hash] record
The record containing the value to be matched
@return [Boolean]
@api semipublic
# File lib/dm-core/query/conditions/comparison.rb, line 207 def matches?(record) match_property?(record) end
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 270 def negated? parent = self.parent parent ? parent.negated? : false end
Returns whether the subject is a Property
@return [Boolean]
@api semipublic
# File lib/dm-core/query/conditions/comparison.rb, line 242 def property? subject.kind_of?(Property) end
Returns whether the subject is a Relationship
@return [Boolean]
@api semipublic
# File lib/dm-core/query/conditions/comparison.rb, line 233 def relationship? false end
Return the comparison class slug
@return [Symbol]
the comparison class slug
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 195 def slug self.class.slug end
Returns a string version of this Comparison object
@example
Comparison.new(:==, MyClass.my_property, "value") # => "my_property == value"
@return [String]
@api semipublic
# File lib/dm-core/query/conditions/comparison.rb, line 265 def to_s "#{subject.name} #{comparator_string} #{dumped_value.inspect}" end
Tests that the Comparison is valid
Subclasses can overload this to customise the means by which they determine the validity of the comparison. valid? is called prior to performing a query on the repository: each Comparison within a Query must be valid otherwise the query will not be performed.
@see DataMapper::Property#valid? @see DataMapper::Associations::Relationship#valid?
@return [Boolean]
@api semipublic
# File lib/dm-core/query/conditions/comparison.rb, line 224 def valid? valid_for_subject?(loaded_value) end
Value to be compared with the subject
This value is compared against that contained in the subject when filtering collections, or the value in the repository when performing queries.
In the case of primitive property, this is the value as it is stored in the repository.
@return [Object]
@api semipublic
# File lib/dm-core/query/conditions/comparison.rb, line 129 def value dumped_value end
Private Instance Methods
Dumps the given #loaded_value using subject#value
This converts property values to the primitive as stored in the repository.
@return [Object]
The raw (dumped) object.
@see Property#value
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 334 def dump dump_property(loaded_value) end
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 339 def dump_property(value) subject.dump(value) end
Retrieves the value of the subject
@return [Object]
@api semipublic
# File lib/dm-core/query/conditions/comparison.rb, line 417 def expected(value = @loaded_value) expected = record_value(value, :target_key) if @subject.respond_to?(:source_key) @subject.source_key.typecast(expected) else expected end end
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 296 def match_property?(record, operator = :===) expected.send(operator, record_value(record)) end
Returns a value for the comparison subject
Extracts value for the subject
property or relationship from
the given record
, where record
is a Resource instance or a Hash.
@param [DataMapper::Resource, Hash] record
The resource or hash from which to retrieve the value.
@param [Property, Associations::Relationship]
The subject of the comparison. For example, if this is a property, the value for the resources +subject+ property is retrieved.
@param [Symbol] key_type
In the event that +subject+ is a relationship, key_type indicated which key should be used to retrieve the value from the resource.
@return [Object]
@api semipublic
# File lib/dm-core/query/conditions/comparison.rb, line 360 def record_value(record, key_type = :source_key) subject = self.subject case record when Hash record_value_from_hash(record, subject, key_type) when Resource record_value_from_resource(record, subject, key_type) else record end end
Returns a value from a record hash
Retrieves value for the subject
property or relationship from
the given hash
.
@return [Object]
@see #record_value
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 382 def record_value_from_hash(hash, subject, key_type) hash.fetch subject, case subject when Property subject.load(hash[subject.field]) when Associations::Relationship subject.send(key_type).map { |property| record_value_from_hash(hash, property, key_type) } end end
Returns a value from a resource
Extracts value for the subject
property or relationship from
the given resource
.
@return [Object]
@see #record_value
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 403 def record_value_from_resource(resource, subject, key_type) case subject when Property subject.get!(resource) when Associations::Relationship subject.send(key_type).get!(resource) end end
Typecasts the given val
using subject#typecast
If the subject has no typecast method the value is returned without any changes.
@param [Object] val
The object to attempt to typecast.
@return [Object]
The typecasted object.
@see DataMapper::Property#typecast
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 314 def typecast(value) typecast_property(value) end
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 319 def typecast_property(value) subject.typecast(value) end
Test the value to see if it is valid
@return [Boolean] true if the value is valid
@api semipublic
# File lib/dm-core/query/conditions/comparison.rb, line 432 def valid_for_subject?(loaded_value) subject.valid?(loaded_value, negated?) end