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

Overview

class Operation

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:



89
90
91
# File 'lib/dm-core/query/conditions/operation.rb', line 89

def operands
  @operands
end

#parentAbstractOperation

Returns the parent operation

Returns:



81
82
83
# File 'lib/dm-core/query/conditions/operation.rb', line 81

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:

  • (Set)

    the descendant classes



99
100
101
# File 'lib/dm-core/query/conditions/operation.rb', line 99

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:

  • (undefined)


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

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

.slug(slug = nil) ⇒ Symbol

Get and set the slug for the operation class

Parameters:

  • slug (Symbol) (defaults to: nil)

    optionally set the slug for the operation class

Returns:

  • (Symbol)

    the slug for the operation class



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

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

Instance Method Details

#<<(operand) ⇒ self

Add an operand to the operation

Parameters:

Returns:

  • (self)

    the operation



202
203
204
205
206
# File 'lib/dm-core/query/conditions/operation.rb', line 202

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

#clearself

Clear the operands

Returns:

  • (self)

    the operation



284
285
286
287
# File 'lib/dm-core/query/conditions/operation.rb', line 284

def clear
  @operands.clear
  self
end

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

Return the difference of the operation and another operand

Parameters:

Returns:

  • (AndOperation)

    the intersection of the operation and operand



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

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:

  • (self)

    returns the operation



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

def each
  @operands.each { |op| yield op }
  self
end

#empty?Boolean

Test to see if there are operands

Returns:

  • (Boolean)

    returns true if there are operands



169
170
171
# File 'lib/dm-core/query/conditions/operation.rb', line 169

def empty?
  @operands.empty?
end

#firstAbstractOperation, ...

Get the first operand

Returns:



141
142
143
144
# File 'lib/dm-core/query/conditions/operation.rb', line 141

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

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

Return the intersection of the operation and another operand

Parameters:

Returns:

  • (AndOperation)

    the intersection of the operation and operand



247
248
249
# File 'lib/dm-core/query/conditions/operation.rb', line 247

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

#merge(operands) ⇒ self

Add operands to the operation

Parameters:

  • operands (#each)

    the operands to add

Returns:

  • (self)

    the operation



217
218
219
220
# File 'lib/dm-core/query/conditions/operation.rb', line 217

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

#minimizeself

Minimize the operation

Returns:

  • (self)

    the minimized operation



274
275
276
# File 'lib/dm-core/query/conditions/operation.rb', line 274

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:

  • (Boolean)

    true if the operation is negated, false if not



307
308
309
310
# File 'lib/dm-core/query/conditions/operation.rb', line 307

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

#one?Boolean

Test to see if there is one operand

Returns:

  • (Boolean)

    true if there is only one operand



179
180
181
# File 'lib/dm-core/query/conditions/operation.rb', line 179

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:

  • (Symbol)

    the comparison class slug



131
132
133
# File 'lib/dm-core/query/conditions/operation.rb', line 131

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:



318
319
320
# File 'lib/dm-core/query/conditions/operation.rb', line 318

def sorted_operands
  sort_by { |op| op.hash }
end

#to_sString

Return the string representation of the operation

Returns:

  • (String)

    the string representation of the operation



295
296
297
# File 'lib/dm-core/query/conditions/operation.rb', line 295

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

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

Return the union with another operand

Parameters:

Returns:

  • (OrOperation)

    the union of the operation and operand



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

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

#valid?Boolean

Test if the operation is valid

Returns:

  • (Boolean)

    true if the operation is valid, false if not



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

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