Class: DataMapper::Query::Conditions::AbstractOperation

Inherits:
Object
  • Object
show all
Extended by:
Equalizer
Includes:
Assertions, Enumerable
Defined in:
lib/dm-core/query/conditions/operation.rb

Direct Known Subclasses

AndOperation, NotOperation, NullOperation, OrOperation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Equalizer

equalize

Methods included from Assertions

#assert_kind_of

Instance Attribute Details

#operandsSet<AbstractOperation, AbstractComparison, Array> (readonly) Also known as: children

Returns the child operations and comparisons

Returns:

  • the set of operations and comparisons

API:

  • semipublic



84
85
86
# File 'lib/dm-core/query/conditions/operation.rb', line 84

def operands
  @operands
end

#parentAbstractOperation

Returns the parent operation

Returns:

  • the parent operation

API:

  • semipublic



76
77
78
# File 'lib/dm-core/query/conditions/operation.rb', line 76

def parent
  @parent
end

Class Method Details

.descendantsSet

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 the classes that inherit from AbstractComparison

Returns:

  • the descendant classes

API:

  • private



94
95
96
# File 'lib/dm-core/query/conditions/operation.rb', line 94

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

.inherited(descendant) ⇒ undefined

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.

Hook executed when inheriting from AbstractComparison

Returns:

API:

  • private



103
104
105
106
# File 'lib/dm-core/query/conditions/operation.rb', line 103

def self.inherited(descendant)
  descendants << descendant
  super
end

.slug(slug = nil) ⇒ Symbol

Get and set the slug for the operation class

Parameters:

  • (defaults to: nil)

    optionally set the slug for the operation class

Returns:

  • the slug for the operation class

API:

  • semipublic



117
118
119
# File 'lib/dm-core/query/conditions/operation.rb', line 117

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

Instance Method Details

#<<(operand) ⇒ self

Add an operand to the operation

Parameters:

  • the operand to add

Returns:

  • the operation

API:

  • semipublic



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

def <<(operand)
  assert_valid_operand_type(operand)
  @operands << relate_operand(operand)
  self
end

#clearself

Clear the operands

Returns:

  • the operation

API:

  • semipublic



280
281
282
283
# File 'lib/dm-core/query/conditions/operation.rb', line 280

def clear
  @operands.clear
  self
end

#difference(other) ⇒ AndOperation Also known as: -

Return the difference of the operation and another operand

Parameters:

  • the operand to not match

Returns:

  • the intersection of the operation and operand

API:

  • semipublic



258
259
260
# File 'lib/dm-core/query/conditions/operation.rb', line 258

def difference(other)
  Operation.new(:and, dup, Operation.new(:not, other.dup)).minimize
end

#each {|operand| ... } ⇒ self

Iterate through each operand in the operation

Yields:

  • (operand)

    yields to each operand

Yield Parameters:

Returns:

  • returns the operation

API:

  • semipublic



154
155
156
157
# File 'lib/dm-core/query/conditions/operation.rb', line 154

def each(&block)
  @operands.each(&block)
  self
end

#empty?Boolean

Test to see if there are operands

Returns:

  • returns true if there are operands

API:

  • semipublic



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

def empty?
  @operands.empty?
end

#firstAbstractOperation, ...

Get the first operand

Returns:

  • returns the first operand

API:

  • semipublic



137
138
139
140
# File 'lib/dm-core/query/conditions/operation.rb', line 137

def first
  each { |operand| return operand }
  nil
end

#intersection(other) ⇒ AndOperation Also known as: &

Return the intersection of the operation and another operand

Parameters:

  • the operand to intersect with

Returns:

  • the intersection of the operation and operand

API:

  • semipublic



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

def intersection(other)
  Operation.new(:and, dup, other.dup).minimize
end

#merge(operands) ⇒ self

Add operands to the operation

Parameters:

  • the operands to add

Returns:

  • the operation

API:

  • semipublic



213
214
215
216
# File 'lib/dm-core/query/conditions/operation.rb', line 213

def merge(operands)
  operands.each { |op| self << op }
  self
end

#minimizeself

Minimize the operation

Returns:

  • the minimized operation

API:

  • semipublic



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

def minimize
  self
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.

Test if the operation is negated

Defaults to return false.

Returns:

  • true if the operation is negated, false if not

API:

  • private



303
304
305
306
# File 'lib/dm-core/query/conditions/operation.rb', line 303

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

#one?Boolean

Test to see if there is one operand

Returns:

  • true if there is only one operand

API:

  • semipublic



175
176
177
# File 'lib/dm-core/query/conditions/operation.rb', line 175

def one?
  @operands.size == 1
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:

  • the comparison class slug

API:

  • private



127
128
129
# File 'lib/dm-core/query/conditions/operation.rb', line 127

def slug
  self.class.slug
end

#sorted_operandsArray<AbstractOperation, AbstractComparison, Array>

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 a list of operands in predictable order

Returns:

  • list of operands sorted in deterministic order

API:

  • private



314
315
316
# File 'lib/dm-core/query/conditions/operation.rb', line 314

def sorted_operands
  sort_by(&:hash)
end

#to_sString

Return the string representation of the operation

Returns:

  • the string representation of the operation

API:

  • semipublic



291
292
293
# File 'lib/dm-core/query/conditions/operation.rb', line 291

def to_s
  empty? ? '' : "(#{sort_by(&:to_s).map(&:to_s).join(" #{slug.to_s.upcase} ")})"
end

#union(other) ⇒ OrOperation Also known as: |, +

Return the union with another operand

Parameters:

  • the operand to union with

Returns:

  • the union of the operation and operand

API:

  • semipublic



227
228
229
# File 'lib/dm-core/query/conditions/operation.rb', line 227

def union(other)
  Operation.new(:or, dup, other.dup).minimize
end

#valid?Boolean

Test if the operation is valid

Returns:

  • true if the operation is valid, false if not

API:

  • semipublic



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

def valid?
  any? && all? { |op| valid_operand?(op) }
end