Class: Predicate

Inherits:
Object
  • Object
show all
Extended by:
Factory, Sugar
Defined in:
lib/predicate.rb,
lib/predicate/dsl.rb,
lib/predicate/sugar.rb,
lib/predicate/sequel.rb,
lib/predicate/factory.rb,
lib/predicate/grammar.rb,
lib/predicate/version.rb,
lib/predicate/asserter.rb,
lib/predicate/nodes/eq.rb,
lib/predicate/nodes/gt.rb,
lib/predicate/nodes/in.rb,
lib/predicate/nodes/lt.rb,
lib/predicate/nodes/or.rb,
lib/predicate/nodes/and.rb,
lib/predicate/nodes/gte.rb,
lib/predicate/nodes/lte.rb,
lib/predicate/nodes/neq.rb,
lib/predicate/nodes/not.rb,
lib/predicate/nodes/var.rb,
lib/predicate/nodes/expr.rb,
lib/predicate/nodes/empty.rb,
lib/predicate/nodes/match.rb,
lib/predicate/placeholder.rb,
lib/predicate/nodes/exists.rb,
lib/predicate/nodes/native.rb,
lib/predicate/nodes/opaque.rb,
lib/predicate/nodes/set_op.rb,
lib/predicate/nodes/subset.rb,
lib/predicate/nodes/literal.rb,
lib/predicate/nodes/has_size.rb,
lib/predicate/nodes/superset.rb,
lib/predicate/nodes/intersect.rb,
lib/predicate/nodes/tautology.rb,
lib/predicate/processors/to_s.rb,
lib/predicate/nodes/identifier.rb,
lib/predicate/nodes/nadic_bool.rb,
lib/predicate/nodes/unary_func.rb,
lib/predicate/sequel/to_sequel.rb,
lib/predicate/nodes/binary_func.rb,
lib/predicate/nodes/dyadic_comp.rb,
lib/predicate/postgres/pg_array.rb,
lib/predicate/postgres/rewriter.rb,
lib/predicate/processors/binder.rb,
lib/predicate/processors/renamer.rb,
lib/predicate/nodes/contradiction.rb,
lib/predicate/postgres/ext/factory.rb,
lib/predicate/processors/qualifier.rb,
lib/predicate/postgres/ext/to_sequel.rb,
lib/predicate/processors/unqualifier.rb,
lib/predicate/postgres/pg_array/empty.rb,
lib/predicate/postgres/pg_array/literal.rb,
lib/predicate/nodes/qualified_identifier.rb,
lib/predicate/postgres/pg_array/overlaps.rb

Defined Under Namespace

Modules: And, BinaryFunc, Contradiction, DyadicComp, Empty, Eq, Exists, Expr, Factory, Grammar, Gt, Gte, HasSize, Identifier, In, Intersect, Literal, Lt, Lte, Match, NadicBool, Native, Neq, Not, Opaque, Or, Postgres, QualifiedIdentifier, SetOp, Subset, Sugar, Superset, Tautology, UnaryFunc, Var, Version Classes: Asserter, Binder, Dsl, Error, NotSupportedError, Placeholder, Qualifier, Renamer, ToS, ToSequel, TypeError, UnboundError, Unqualifier

Constant Summary collapse

TupleLike =
->(t){ t.is_a?(Hash) }
SexprLike =
->(x) { x.is_a?(Array) && x.first.is_a?(Symbol) }
VERSION =
"#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sugar

between, is_null, match, max_size, min_size

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, tautology, var, vars

Constructor Details

#initialize(sexpr) ⇒ Predicate

Returns a new instance of Predicate.



21
22
23
# File 'lib/predicate.rb', line 21

def initialize(sexpr)
  @sexpr = sexpr
end

Instance Attribute Details

#sexprObject (readonly) Also known as: expr

Returns the value of attribute sexpr.



24
25
26
# File 'lib/predicate.rb', line 24

def sexpr
  @sexpr
end

Class Method Details

.coerce(arg) ⇒ Object Also known as: parse



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/predicate.rb', line 31

def coerce(arg)
  case arg
  when Predicate   then arg
  when TrueClass   then tautology
  when FalseClass  then contradiction
  when Symbol      then identifier(arg)
  when Proc        then native(arg)
  when Hash        then from_hash(arg)
  else
    raise ArgumentError, "Unable to coerce `#{arg}` to a predicate"
  end
end

.currying(var = var(".", :dig), &bl) ⇒ Object



49
50
51
# File 'lib/predicate.rb', line 49

def currying(var = var(".", :dig), &bl)
  Predicate::Dsl.new(var, true).instance_eval(&bl)
end

.dsl(var = var(".", :dig), &bl) ⇒ Object



45
46
47
# File 'lib/predicate.rb', line 45

def dsl(var = var(".", :dig), &bl)
  Predicate::Dsl.new(var, false).instance_eval(&bl)
end

Instance Method Details

#!Object



101
102
103
# File 'lib/predicate.rb', line 101

def !
  Predicate.new(!expr)
end

#&(other) ⇒ Object



89
90
91
92
93
# File 'lib/predicate.rb', line 89

def &(other)
  return self  if other.tautology? or other==self
  return other if tautology?
  Predicate.new(expr & other.expr)
end

#==(other) ⇒ Object Also known as: eql?



154
155
156
# File 'lib/predicate.rb', line 154

def ==(other)
  other.is_a?(Predicate) && (other.expr==expr)
end

#and_split(attr_list) ⇒ Object

Splits this predicate, say P, as too predicates P1 & P2 such that ‘P <=> P1 & P2` and P2 makes no reference to any attribute in `attr_list`.



136
137
138
# File 'lib/predicate.rb', line 136

def and_split(attr_list)
  expr.and_split(attr_list).map{|e| Predicate.new(e)}
end

#assert!(tuple = {}) ⇒ Object



129
130
131
# File 'lib/predicate.rb', line 129

def assert!(tuple = {})
  expr.assert!(tuple)
end

#attr_splitObject

Returns a hash ‘(attr -> Pattr)` associating attribute names to predicates, so that each predicate `Pattr` only makes reference to the corresponding attribute name `attr`, while the conjunction of `Pattr`s is still equivalent to the original predicate.

A ‘nil` key may map a predicate that still makes references to more than one attribute.



148
149
150
151
152
# File 'lib/predicate.rb', line 148

def attr_split
  expr.attr_split.each_pair.each_with_object({}) do |(k,v),h|
    h[k] = Predicate.new(v)
  end
end

#bind(binding) ⇒ Object



117
118
119
# File 'lib/predicate.rb', line 117

def bind(binding)
  Predicate.new(expr.bind(binding))
end

#call(tuple) ⇒ Object



125
126
127
# File 'lib/predicate.rb', line 125

def call(tuple)
  expr.evaluate(tuple)
end

#constant_variablesObject



81
82
83
# File 'lib/predicate.rb', line 81

def constant_variables
  expr.constant_variables
end

#constantsObject



85
86
87
# File 'lib/predicate.rb', line 85

def constants
  expr.constants
end

#contradiction?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/predicate.rb', line 73

def contradiction?
  expr.contradiction?
end

#evaluate(tuple) ⇒ Object



121
122
123
# File 'lib/predicate.rb', line 121

def evaluate(tuple)
  expr.evaluate(tuple)
end

#free_variablesObject



77
78
79
# File 'lib/predicate.rb', line 77

def free_variables
  expr.free_variables
end

#hashObject



159
160
161
# File 'lib/predicate.rb', line 159

def hash
  expr.hash
end

#native?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/predicate.rb', line 65

def native?
  Native===expr
end

#qualify(qualifier) ⇒ Object



105
106
107
# File 'lib/predicate.rb', line 105

def qualify(qualifier)
  Predicate.new(expr.qualify(qualifier))
end

#rename(renaming) ⇒ Object



113
114
115
# File 'lib/predicate.rb', line 113

def rename(renaming)
  Predicate.new(expr.rename(renaming))
end

#tautology?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/predicate.rb', line 69

def tautology?
  expr.tautology?
end

#to_hashObject

If possible, converts this predicate back to a ‘{ attr: value, … }` hash.

Raises an ArgumentError if the predicate cannot be represented that way.



171
172
173
# File 'lib/predicate.rb', line 171

def to_hash
  expr.to_hash
end

#to_hashesObject

If possible, converts this predicate back to two ‘{ attr: value, … }` hashes, where the first one is positive (e.g. x IN …) and the second one is negative (e.g. x NOT IN …)

The result is such that the original predicate is equivalent to a AND of the two hashes, where the first includes all its values and the second excludes all its values.

Raises an ArgumentError if the predicate cannot be represented that way.



184
185
186
# File 'lib/predicate.rb', line 184

def to_hashes
  expr.to_hashes
end

#to_postgres(*args) ⇒ Object



46
47
48
# File 'lib/predicate/postgres/rewriter.rb', line 46

def to_postgres(*args)
  Predicate.new(expr.to_postgres(*args))
end

#to_s(scope = nil) ⇒ Object



163
164
165
# File 'lib/predicate.rb', line 163

def to_s(scope = nil)
  expr.to_s(scope)
end

#to_sequelObject

module Expr



11
12
13
# File 'lib/predicate/sequel.rb', line 11

def to_sequel
  expr.to_sequel
end

#unqualifyObject



109
110
111
# File 'lib/predicate.rb', line 109

def unqualify
  Predicate.new(expr.unqualify)
end

#|(other) ⇒ Object



95
96
97
98
99
# File 'lib/predicate.rb', line 95

def |(other)
  return self  if other.contradiction? or other==self
  return other if contradiction?
  Predicate.new(expr | other.expr)
end