Class: DataMapper::Query::Conditions::AbstractOperation
- Inherits:
-
Object
- Object
- DataMapper::Query::Conditions::AbstractOperation
- Extended by:
- Equalizer
- Includes:
- Assertions, Enumerable
- Defined in:
- lib/dm-core/query/conditions/operation.rb
Overview
class Operation
Direct Known Subclasses
Instance Attribute Summary collapse
-
#operands ⇒ Set<AbstractOperation, AbstractComparison, Array>
(also: #children)
readonly
Returns the child operations and comparisons.
-
#parent ⇒ AbstractOperation
Returns the parent operation.
Class Method Summary collapse
-
.descendants ⇒ Set
private
Returns the classes that inherit from AbstractComparison.
-
.inherited(descendant) ⇒ undefined
private
Hook executed when inheriting from AbstractComparison.
-
.slug(slug = nil) ⇒ Symbol
Get and set the slug for the operation class.
Instance Method Summary collapse
-
#<<(operand) ⇒ self
Add an operand to the operation.
-
#clear ⇒ self
Clear the operands.
-
#difference(other) ⇒ AndOperation
(also: #-)
Return the difference of the operation and another operand.
-
#each {|operand| ... } ⇒ self
Iterate through each operand in the operation.
-
#empty? ⇒ Boolean
Test to see if there are operands.
-
#first ⇒ AbstractOperation, ...
Get the first operand.
-
#intersection(other) ⇒ AndOperation
(also: #&)
Return the intersection of the operation and another operand.
-
#merge(operands) ⇒ self
Add operands to the operation.
-
#minimize ⇒ self
Minimize the operation.
-
#negated? ⇒ Boolean
private
Test if the operation is negated.
-
#one? ⇒ Boolean
Test to see if there is one operand.
-
#slug ⇒ Symbol
private
Return the comparison class slug.
-
#sorted_operands ⇒ Array<AbstractOperation, AbstractComparison, Array>
private
Return a list of operands in predictable order.
-
#to_s ⇒ String
Return the string representation of the operation.
-
#union(other) ⇒ OrOperation
(also: #|, #+)
Return the union with another operand.
-
#valid? ⇒ Boolean
Test if the operation is valid.
Methods included from Equalizer
Methods included from Assertions
Instance Attribute Details
#operands ⇒ Set<AbstractOperation, AbstractComparison, Array> (readonly) Also known as: children
Returns the child operations and comparisons
89 90 91 |
# File 'lib/dm-core/query/conditions/operation.rb', line 89 def operands @operands end |
#parent ⇒ AbstractOperation
Returns the parent operation
81 82 83 |
# File 'lib/dm-core/query/conditions/operation.rb', line 81 def parent @parent end |
Class Method Details
.descendants ⇒ Set
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
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
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
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
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 |
#clear ⇒ self
Clear the operands
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
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
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
169 170 171 |
# File 'lib/dm-core/query/conditions/operation.rb', line 169 def empty? @operands.empty? end |
#first ⇒ AbstractOperation, ...
Get the first operand
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
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
217 218 219 220 |
# File 'lib/dm-core/query/conditions/operation.rb', line 217 def merge(operands) operands.each { |op| self << op } self end |
#minimize ⇒ self
Minimize the 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.
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
179 180 181 |
# File 'lib/dm-core/query/conditions/operation.rb', line 179 def one? @operands.size == 1 end |
#slug ⇒ Symbol
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
131 132 133 |
# File 'lib/dm-core/query/conditions/operation.rb', line 131 def slug self.class.slug end |
#sorted_operands ⇒ Array<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
318 319 320 |
# File 'lib/dm-core/query/conditions/operation.rb', line 318 def sorted_operands sort_by { |op| op.hash } end |
#to_s ⇒ String
Return 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
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
189 190 191 |
# File 'lib/dm-core/query/conditions/operation.rb', line 189 def valid? any? && all? { |op| valid_operand?(op) } end |