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

- (Set<AbstractOperation, AbstractComparison, Array>) operands (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

- (AbstractOperation) parent

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

+ (Set) descendants

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

+ (undefined) inherited(descendant)

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
111
# File 'lib/dm-core/query/conditions/operation.rb', line 108

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

+ (Symbol) slug(slug = nil)

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



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

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

Instance Method Details

- (self) <<(operand)

Add an operand to the operation

Parameters:

Returns:

  • (self)

    the operation



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

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

- (self) clear

Clear the operands

Returns:

  • (self)

    the operation



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

def clear
  @operands.clear
  self
end

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

Return the difference of the operation and another operand

Parameters:

Returns:

  • (AndOperation)

    the intersection of the operation and operand



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

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

- (self) each {|operand| ... }

Iterate through each operand in the operation

Yields:

  • (operand)

    yields to each operand

Yield Parameters:

Returns:

  • (self)

    returns the operation



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

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

- (Boolean) empty?

Test to see if there are operands

Returns:

  • (Boolean)

    returns true if there are operands



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

def empty?
  @operands.empty?
end

- (AbstractOperation, ...) first

Get the first operand

Returns:



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

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

- (AndOperation) intersection(other) Also known as: &

Return the intersection of the operation and another operand

Parameters:

Returns:

  • (AndOperation)

    the intersection of the operation and operand



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

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

- (self) merge(operands)

Add operands to the operation

Parameters:

  • operands (#each)

    the operands to add

Returns:

  • (self)

    the operation



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

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

- (self) minimize

Minimize the operation

Returns:

  • (self)

    the minimized operation



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

def minimize
  self
end

- (Boolean) negated?

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



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

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

- (Boolean) one?

Test to see if there is one operand

Returns:

  • (Boolean)

    true if there is only one operand



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

def one?
  @operands.size == 1
end

- (Symbol) slug

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



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

def slug
  self.class.slug
end

- (Array<AbstractOperation, AbstractComparison, Array>) sorted_operands

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:



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

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

- (String) to_s

Return the string representation of the operation

Returns:

  • (String)

    the string representation of the operation



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

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

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

Return the union with another operand

Parameters:

Returns:

  • (OrOperation)

    the union of the operation and operand



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

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

- (Boolean) valid?

Test if the operation is valid

Returns:

  • (Boolean)

    true if the operation is valid, false if not



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

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