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

Inherits:
Object
  • Object
show all
Extended by:
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 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 primitive property 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)


153
154
155
# File 'lib/dm-core/query/conditions/comparison.rb', line 153

def loaded_value
  @loaded_value
end

#parentObject



108
109
110
# File 'lib/dm-core/query/conditions/comparison.rb', line 108

def parent
  @parent
end

#subjectProperty, Associations::Relationship (readonly)

The property or relationship which is being matched against



115
116
117
# File 'lib/dm-core/query/conditions/comparison.rb', line 115

def subject
  @subject
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:



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

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

.inherited(descendant) ⇒ 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)



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

def self.inherited(descendant)
  descendants << descendant
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.



185
186
187
# File 'lib/dm-core/query/conditions/comparison.rb', line 185

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

Instance Method Details

#inspectString

Returns a human-readable representation of this object

Returns:

  • (String)


251
252
253
254
# 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

#matches?(record) ⇒ Boolean

Test that the record value matches the comparison

Parameters:

  • record (Resource, Hash)

    The record containing the value to be matched

Returns:

  • (Boolean)


207
208
209
# File 'lib/dm-core/query/conditions/comparison.rb', line 207

def matches?(record)
  match_property?(record)
end

#negated?Boolean

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.

Returns:

  • (Boolean)


270
271
272
273
# File 'lib/dm-core/query/conditions/comparison.rb', line 270

def negated?
  parent = self.parent
  parent ? parent.negated? : false
end

#property?Boolean

Returns whether the subject is a Property

Returns:

  • (Boolean)


242
243
244
# File 'lib/dm-core/query/conditions/comparison.rb', line 242

def property?
  subject.kind_of?(Property)
end

#relationship?Boolean

Returns whether the subject is a Relationship

Returns:

  • (Boolean)


233
234
235
# File 'lib/dm-core/query/conditions/comparison.rb', line 233

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



195
196
197
# File 'lib/dm-core/query/conditions/comparison.rb', line 195

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)


265
266
267
# File 'lib/dm-core/query/conditions/comparison.rb', line 265

def to_s
  "#{subject.name} #{comparator_string} #{dumped_value.inspect}"
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:



224
225
226
# File 'lib/dm-core/query/conditions/comparison.rb', line 224

def valid?
  valid_for_subject?(loaded_value)
end

#valueObject

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.

Returns:

  • (Object)


129
130
131
# File 'lib/dm-core/query/conditions/comparison.rb', line 129

def value
  dumped_value
end