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
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
84 85 86 |
# File 'lib/dm-core/query/conditions/operation.rb', line 84 def operands @operands end |
#parent ⇒ AbstractOperation
Returns the parent operation
76 77 78 |
# File 'lib/dm-core/query/conditions/operation.rb', line 76 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
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
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
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
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 |
#clear ⇒ self
Clear the operands
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
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
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
165 166 167 |
# File 'lib/dm-core/query/conditions/operation.rb', line 165 def empty? @operands.empty? end |
#first ⇒ AbstractOperation, ...
Get the first operand
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
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
213 214 215 216 |
# File 'lib/dm-core/query/conditions/operation.rb', line 213 def merge(operands) operands.each { |op| self << op } self end |
#minimize ⇒ self
Minimize the 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.
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
175 176 177 |
# File 'lib/dm-core/query/conditions/operation.rb', line 175 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
127 128 129 |
# File 'lib/dm-core/query/conditions/operation.rb', line 127 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
314 315 316 |
# File 'lib/dm-core/query/conditions/operation.rb', line 314 def sorted_operands sort_by(&:hash) end |
#to_s ⇒ String
Return 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
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
185 186 187 |
# File 'lib/dm-core/query/conditions/operation.rb', line 185 def valid? any? && all? { |op| valid_operand?(op) } end |