Class: DataMapper::Query::Conditions::AbstractComparison

Inherits:
Object
  • Object
show all
Extended by:
Deprecate, Equalizer
Defined in:
lib/dm-core/query/conditions/comparison.rb

Overview

A base class for the various comparison classes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Deprecate

deprecate

Methods included from Equalizer

equalize

Instance Attribute Details

#loaded_valueObject (readonly)

The loaded/typecast value

In the case of primitive types, this will be the same as value, however when using custom types this stores the loaded value.

If writing an adapter, you should use value, while plugin authors should refer to loaded_value.

– As an example, you might use symbols with the Enum type in dm-types

property :myprop, Enum[:open, :closed]

These are stored in repositories as 1 and 2, respectively. value returns the 1 or 2, while loaded_value returns the symbol. ++

Returns:

  • (Object)


158
159
160
# File 'lib/dm-core/query/conditions/comparison.rb', line 158

def loaded_value
  @loaded_value
end

#subjectProperty, Associations::Relationship (readonly)

The property or relationship which is being matched against



122
123
124
# File 'lib/dm-core/query/conditions/comparison.rb', line 122

def subject
  @subject
end

#valueObject (readonly)

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 custom types, this is the value as it is stored in the repository.

Returns:

  • (Object)


136
137
138
# File 'lib/dm-core/query/conditions/comparison.rb', line 136

def value
  @value
end

Class Method Details

.descendantsSet<AbstractComparison>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Keeps track of AbstractComparison subclasses (used in Comparison)

Returns:



164
165
166
# File 'lib/dm-core/query/conditions/comparison.rb', line 164

def self.descendants
  @descendants ||= Set.new
end

.inherited(comparison_class) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Registers AbstractComparison subclasses (used in Comparison)



171
172
173
# File 'lib/dm-core/query/conditions/comparison.rb', line 171

def self.inherited(comparison_class)
  descendants << comparison_class
end

.slug(slug = nil) ⇒ Symbol

Setter/getter: allows subclasses to easily set their slug

Examples:

Creating a MyComparison compairson with slug :exact.

class MyComparison < AbstractComparison
  slug :exact
end

Parameters:

  • slug (Symbol) (defaults to: nil)

    The slug to be set for this class. Passing nil returns the current value instead.

Returns:

  • (Symbol)

    The current slug set for the Comparison.



190
191
192
# File 'lib/dm-core/query/conditions/comparison.rb', line 190

def self.slug(slug = nil)
  slug ? @slug = slug : @slug
end

Instance Method Details

#inspectString

Returns a human-readable representation of this object

Returns:

  • (String)


248
249
250
251
# File 'lib/dm-core/query/conditions/comparison.rb', line 248

def inspect
  "#<#{self.class} @subject=#{@subject.inspect} " \
    "@value=#{@value.inspect} @loaded_value=#{@loaded_value.inspect}>"
end

#property?Boolean

Returns whether the subject is a Property

Returns:

  • (Boolean)


239
240
241
# File 'lib/dm-core/query/conditions/comparison.rb', line 239

def property?
  subject.kind_of?(Property)
end

#relationship?Boolean

Returns whether the subject is a Relationship

Returns:

  • (Boolean)


230
231
232
# File 'lib/dm-core/query/conditions/comparison.rb', line 230

def relationship?
  false
end

#slugSymbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the comparison class slug

Returns:

  • (Symbol)

    the comparison class slug



200
201
202
# File 'lib/dm-core/query/conditions/comparison.rb', line 200

def slug
  self.class.slug
end

#to_sString

Returns a string version of this Comparison object

Examples:

Comparison.new(:==, MyClass.my_property, "value")
# => "my_property == value"

Returns:

  • (String)


262
263
264
# File 'lib/dm-core/query/conditions/comparison.rb', line 262

def to_s
  "#{@subject} #{comparator_string} #{@value}"
end

#valid?Boolean

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.

Returns:

  • (Boolean)

See Also:



217
218
219
220
221
222
223
# File 'lib/dm-core/query/conditions/comparison.rb', line 217

def valid?
  # This needs to be deferred until the last moment because the value
  # could be a reference to a Resource, that when the comparison was
  # created was invalid, but has since been saved and has it's key
  # set.
  subject.valid?(loaded_value)
end