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:



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

def operands
  @operands
end

#parentAbstractOperation

Returns the parent operation

Returns:



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:

  • (Set)

    the descendant classes



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:

  • (undefined)


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:

  • slug (Symbol) (defaults to: nil)

    optionally set the slug for the operation class

Returns:

  • (Symbol)

    the slug for the operation class



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:

Returns:

  • (self)

    the operation



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:

  • (self)

    the operation



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:

Returns:

  • (AndOperation)

    the intersection of the operation and operand



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:

  • (self)

    returns the operation



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:

  • (Boolean)

    returns true if there are operands



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

def empty?
  @operands.empty?
end

#firstAbstractOperation, ...

Get the first operand

Returns:



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:

Returns:

  • (AndOperation)

    the intersection of the operation and operand



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:

  • operands (#each)

    the operands to add

Returns:

  • (self)

    the operation



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:

  • (self)

    the minimized operation



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:

  • (Boolean)

    true if the operation is negated, false if not



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:

  • (Boolean)

    true if there is only one operand



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:

  • (Symbol)

    the comparison class slug



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:



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:

  • (String)

    the string representation of the operation



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:

Returns:

  • (OrOperation)

    the union of the operation and operand



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:

  • (Boolean)

    true if the operation is valid, false if not



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

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