Module: Predicate::Or
- Includes:
- NadicBool
- Defined in:
- lib/predicate/nodes/or.rb
Constant Summary
Constants included from Expr
Instance Method Summary collapse
- #evaluate(tuple) ⇒ Object
- #operator_symbol ⇒ Object
-
#to_hash ⇒ Object
A disjunction can be represented as a
{ attr => [values] }hash only when every operand constrains the same single attribute to a literal or a set of literals, e.g. - #to_hashes ⇒ Object
Methods included from NadicBool
Methods included from Expr
#!, #&, #and_split, #assert!, #attr_split, #bind, #constant_variables, #constants, #contradiction?, #dyadic_priority, #identifier?, #literal?, #opaque?, #qualify, #rename, #sexpr, #tautology?, #to_postgres, #to_s, #to_sequel, #unqualify, #|
Methods included from Factory
#_factor_predicate, #and, #comp, #contradiction, #empty, #from_hash, #h, #has_size, #identifier, #in, #literal, #match, #native, #not, #opaque, #or, #pg_array_empty, #pg_array_literal, #pg_array_overlaps, #placeholder, #qualified_identifier, #sexpr, #tautology, #var, #vars
Instance Method Details
#evaluate(tuple) ⇒ Object
9 10 11 |
# File 'lib/predicate/nodes/or.rb', line 9 def evaluate(tuple) sexpr_body.any?{|op| op.evaluate(tuple) } end |
#operator_symbol ⇒ Object
5 6 7 |
# File 'lib/predicate/nodes/or.rb', line 5 def operator_symbol :'||' end |
#to_hash ⇒ Object
A disjunction can be represented as a { attr => [values] } hash only
when every operand constrains the same single attribute to a literal
or a set of literals, e.g. x == 1 OR x == 2 OR x IN [3, 4]. Operands
are folded together into an IN-style hash. As soon as an operand touches
another attribute, or cannot itself be represented as a hash, the whole
disjunction is not representable and an ArgumentError is raised (as for
any other non-representable predicate).
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/predicate/nodes/or.rb', line 20 def to_hash merged = sexpr_body.inject(nil) do |acc, term| hash = term.to_hash return super unless hash.size == 1 attr, value = hash.first values = value.is_a?(Array) ? value : [value] if acc.nil? [attr, values.dup] elsif acc.first == attr [attr, acc.last + values] else return super end end return super if merged.nil? { merged.first => merged.last.uniq } end |
#to_hashes ⇒ Object
41 42 43 |
# File 'lib/predicate/nodes/or.rb', line 41 def to_hashes [ to_hash, {} ] end |