A base class for the various comparison classes.
The property or relationship which is being matched against
@return [Property, Associations::Relationship]
@api semipublic
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
Keeps track of AbstractComparison subclasses (used in Comparison)
@return [Set
# File lib/dm-core/query/conditions/comparison.rb, line 159 159: def self.descendants 160: @descendants ||= DescendantSet.new 161: end
Registers AbstractComparison subclasses (used in Comparison)
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 166 166: def self.inherited(descendant) 167: descendants << descendant 168: 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 289: def initialize(subject, value) 290: @subject = subject 291: @loaded_value = typecast(value) 292: @dumped_value = dump 293: 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 185: def self.slug(slug = nil) 186: slug ? @slug = slug : @slug 187: end
Returns a human-readable representation of this object
@return [String]
@api semipublic
# File lib/dm-core/query/conditions/comparison.rb, line 251 251: def inspect 252: "#<#{self.class} @subject=#{@subject.inspect} " "@dumped_value=#{@dumped_value.inspect} @loaded_value=#{@loaded_value.inspect}>" 253: 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 207: def matches?(record) 208: match_property?(record) 209: end
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 270 270: def negated? 271: parent = self.parent 272: parent ? parent.negated? : false 273: end
Returns whether the subject is a Property
@return [Boolean]
@api semipublic
# File lib/dm-core/query/conditions/comparison.rb, line 242 242: def property? 243: subject.kind_of?(Property) 244: end
Returns whether the subject is a Relationship
@return [Boolean]
@api semipublic
# File lib/dm-core/query/conditions/comparison.rb, line 233 233: def relationship? 234: false 235: end
Return the comparison class slug
@return [Symbol]
the comparison class slug
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 195 195: def slug 196: self.class.slug 197: 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 265: def to_s 266: "#{subject.name} #{comparator_string} #{dumped_value.inspect}" 267: end
Tests that the Comparison is valid
Subclasses can overload this to customise the means by which they determine the validity of the comparison. # 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 224: def valid? 225: valid_for_subject?(loaded_value) 226: 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 129: def value 130: dumped_value 131: end
Dumps the given loaded_value using subject#
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 334: def dump 335: dump_property(loaded_value) 336: end
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 339 339: def dump_property(value) 340: subject.dump(value) 341: end
Retrieves the value of the subject
@return [Object]
@api semipublic
# File lib/dm-core/query/conditions/comparison.rb, line 417 417: def expected(value = @loaded_value) 418: expected = record_value(value, :target_key) 419: 420: if @subject.respond_to?(:source_key) 421: @subject.source_key.typecast(expected) 422: else 423: expected 424: end 425: end
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 296 296: def match_property?(record, operator = :===) 297: expected.send(operator, record_value(record)) 298: 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 360: def record_value(record, key_type = :source_key) 361: subject = self.subject 362: case record 363: when Hash 364: record_value_from_hash(record, subject, key_type) 365: when Resource 366: record_value_from_resource(record, subject, key_type) 367: else 368: record 369: end 370: end
Returns a value from a record hash
Retrieves value for the subject property or relationship from the given hash.
@return [Object]
@see AbstractComparison#record_value
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 382 382: def record_value_from_hash(hash, subject, key_type) 383: hash.fetch subject, case subject 384: when Property 385: subject.load(hash[subject.field]) 386: when Associations::Relationship 387: subject.send(key_type).map { |property| 388: record_value_from_hash(hash, property, key_type) 389: } 390: end 391: end
Returns a value from a resource
Extracts value for the subject property or relationship from the given resource.
@return [Object]
@see AbstractComparison#record_value
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 403 403: def record_value_from_resource(resource, subject, key_type) 404: case subject 405: when Property 406: subject.get!(resource) 407: when Associations::Relationship 408: subject.send(key_type).get!(resource) 409: end 410: end
Typecasts the given val using subject#
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 Property#typecast
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 314 314: def typecast(value) 315: typecast_property(value) 316: end
@api private
# File lib/dm-core/query/conditions/comparison.rb, line 319 319: def typecast_property(value) 320: subject.typecast(value) 321: 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 432: def valid_for_subject?(loaded_value) 433: subject.valid?(loaded_value, negated?) 434: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.