class DataMapper::Query::Conditions::AbstractOperation
Attributes
Returns the child operations and comparisons
@return [Set<AbstractOperation, AbstractComparison, Array>]
the set of operations and comparisons
@api semipublic
Returns the child operations and comparisons
@return [Set<AbstractOperation, AbstractComparison, Array>]
the set of operations and comparisons
@api semipublic
Returns the parent operation
@return [AbstractOperation]
the parent operation
@api semipublic
Public Class Methods
Returns the classes that inherit from AbstractComparison
@return [Set]
the descendant classes
@api private
# File lib/dm-core/query/conditions/operation.rb, line 99 def self.descendants @descendants ||= DescendantSet.new end
Hook executed when inheriting from AbstractComparison
@return [undefined]
@api private
# File lib/dm-core/query/conditions/operation.rb, line 108 def self.inherited(descendant) descendants << descendant end
Initialize an operation
@param [Array<AbstractOperation, AbstractComparison, Array>] *operands
the operands to include in the operation
@return [AbstractOperation]
the operation
@api semipublic
# File lib/dm-core/query/conditions/operation.rb, line 333 def initialize(*operands) @operands = Set.new merge(operands) end
Get and set the slug for the operation class
@param [Symbol] slug
optionally set the slug for the operation class
@return [Symbol]
the slug for the operation class
@api semipublic
# File lib/dm-core/query/conditions/operation.rb, line 121 def self.slug(slug = nil) slug ? @slug = slug : @slug end
Public Instance Methods
Add an operand to the operation
@param [AbstractOperation, AbstractComparison, Array] operand
the operand to add
@return [self]
the operation
@api semipublic
# File lib/dm-core/query/conditions/operation.rb, line 202 def <<(operand) assert_valid_operand_type(operand) @operands << relate_operand(operand) self end
Clear the operands
@return [self]
the operation
@api semipublic
# File lib/dm-core/query/conditions/operation.rb, line 284 def clear @operands.clear self end
Return the difference of the operation and another operand
@param [AbstractOperation] other
the operand to not match
@return [AndOperation]
the intersection of the operation and operand
@api semipublic
# File lib/dm-core/query/conditions/operation.rb, line 262 def difference(other) Operation.new(:and, dup, Operation.new(:not, other.dup)).minimize end
Iterate through each operand in the operation
@yield [operand]
yields to each operand
@yieldparam [AbstractOperation, AbstractComparison, Array] operand
each operand
@return [self]
returns the operation
@api semipublic
# File lib/dm-core/query/conditions/operation.rb, line 158 def each @operands.each { |op| yield op } self end
Test to see if there are operands
@return [Boolean]
returns true if there are operands
@api semipublic
# File lib/dm-core/query/conditions/operation.rb, line 169 def empty? @operands.empty? end
Get the first operand
@return [AbstractOperation, AbstractComparison, Array]
returns the first operand
@api semipublic
# File lib/dm-core/query/conditions/operation.rb, line 141 def first each { |operand| return operand } nil end
Return the intersection of the operation and another operand
@param [AbstractOperation] other
the operand to intersect with
@return [AndOperation]
the intersection of the operation and operand
@api semipublic
# File lib/dm-core/query/conditions/operation.rb, line 247 def intersection(other) Operation.new(:and, dup, other.dup).minimize end
Add operands to the operation
@param [#each] operands
the operands to add
@return [self]
the operation
@api semipublic
# File lib/dm-core/query/conditions/operation.rb, line 217 def merge(operands) operands.each { |op| self << op } self end
Minimize the operation
@return [self]
the minimized operation
@api semipublic
# File lib/dm-core/query/conditions/operation.rb, line 274 def minimize self end
Test if the operation is negated
Defaults to return false.
@return [Boolean]
true if the operation is negated, false if not
@api private
# File lib/dm-core/query/conditions/operation.rb, line 307 def negated? parent = self.parent parent ? parent.negated? : false end
Test to see if there is one operand
@return [Boolean]
true if there is only one operand
@api semipublic
# File lib/dm-core/query/conditions/operation.rb, line 179 def one? @operands.size == 1 end
Return the comparison class slug
@return [Symbol]
the comparison class slug
@api private
# File lib/dm-core/query/conditions/operation.rb, line 131 def slug self.class.slug end
Return a list of operands in predictable order
@return [Array<AbstractOperation, AbstractComparison, Array>]
list of operands sorted in deterministic order
@api private
# File lib/dm-core/query/conditions/operation.rb, line 318 def sorted_operands sort_by { |op| op.hash } end
Return the string representation of the operation
@return [String]
the string representation of the operation
@api semipublic
# 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
Return the union with another operand
@param [AbstractOperation] other
the operand to union with
@return [OrOperation]
the union of the operation and operand
@api semipublic
# File lib/dm-core/query/conditions/operation.rb, line 231 def union(other) Operation.new(:or, dup, other.dup).minimize end
Test if the operation is valid
@return [Boolean]
true if the operation is valid, false if not
@api semipublic
# File lib/dm-core/query/conditions/operation.rb, line 189 def valid? any? && all? { |op| valid_operand?(op) } end
Private Instance Methods
Assert that the operand is a valid type
@param [AbstractOperation, AbstractComparison, Array] operand
the operand to test
@return [undefined]
@raise [ArgumentError]
raised if the operand is not a valid type
@api private
# File lib/dm-core/query/conditions/operation.rb, line 410 def assert_valid_operand_type(operand) assert_kind_of 'operand', operand, AbstractOperation, AbstractComparison, Array end
Copy an operation
@param [AbstractOperation] original
the original operation
@return [undefined]
@api semipublic
# File lib/dm-core/query/conditions/operation.rb, line 346 def initialize_copy(*) @operands = map { |op| op.dup }.to_set end
Minimize the operands recursively
@return [undefined]
@api private
# File lib/dm-core/query/conditions/operation.rb, line 355 def minimize_operands # FIXME: why does Set#map! not work here? @operands = map do |op| relate_operand(op.respond_to?(:minimize) ? op.minimize : op) end.to_set end
Prune empty operands recursively
@return [undefined]
@api private
# File lib/dm-core/query/conditions/operation.rb, line 367 def prune_operands @operands.delete_if { |op| op.respond_to?(:empty?) ? op.empty? : false } end
Set self to be the operand's parent
@return [AbstractOperation, AbstractComparison, Array]
the operand that was related to self
@api private
# File lib/dm-core/query/conditions/operation.rb, line 394 def relate_operand(operand) operand.parent = self if operand.respond_to?(:parent=) operand end
Test if the operand is valid
@param [AbstractOperation, AbstractComparison, Array] operand
the operand to test
@return [Boolean]
true if the operand is valid
@api private
# File lib/dm-core/query/conditions/operation.rb, line 380 def valid_operand?(operand) if operand.respond_to?(:valid?) operand.valid? else true end end