Class: Perpetuity::MongoDB::QueryExpression

Inherits:
Object
  • Object
show all
Defined in:
lib/perpetuity/mongodb/query_expression.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute, comparator, value) ⇒ QueryExpression

Returns a new instance of QueryExpression.



9
10
11
12
13
14
15
16
# File 'lib/perpetuity/mongodb/query_expression.rb', line 9

def initialize attribute, comparator, value
  @attribute = attribute
  @comparator = comparator
  @value = value
  @negated = false

  @attribute = @attribute.to_sym if @attribute.respond_to? :to_sym
end

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



7
8
9
# File 'lib/perpetuity/mongodb/query_expression.rb', line 7

def attribute
  @attribute
end

#comparatorObject

Returns the value of attribute comparator.



7
8
9
# File 'lib/perpetuity/mongodb/query_expression.rb', line 7

def comparator
  @comparator
end

#negatedObject

Returns the value of attribute negated.



7
8
9
# File 'lib/perpetuity/mongodb/query_expression.rb', line 7

def negated
  @negated
end

#valueObject

Returns the value of attribute value.



7
8
9
# File 'lib/perpetuity/mongodb/query_expression.rb', line 7

def value
  @value
end

Instance Method Details

#&(other) ⇒ Object



76
77
78
# File 'lib/perpetuity/mongodb/query_expression.rb', line 76

def & other
  QueryIntersection.new(self, other)
end

#==(other) ⇒ Object



86
87
88
89
90
91
# File 'lib/perpetuity/mongodb/query_expression.rb', line 86

def == other
  attribute == other.attribute &&
  comparator == other.comparator &&
  value == other.value &&
  negated == other.negated
end

#equalsObject



22
23
24
25
26
27
28
# File 'lib/perpetuity/mongodb/query_expression.rb', line 22

def equals
  if @negated
    { @attribute => { '$ne' => @value } }
  else
    { @attribute => @value }
  end
end

#function(func) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/perpetuity/mongodb/query_expression.rb', line 30

def function func
  f = { func => @value }

  if @negated
    { @attribute => { '$not' => f } }
  else
    { @attribute => f }
  end
end

#greater_thanObject



48
49
50
# File 'lib/perpetuity/mongodb/query_expression.rb', line 48

def greater_than
  function '$gt'
end

#gteObject



52
53
54
# File 'lib/perpetuity/mongodb/query_expression.rb', line 52

def gte
  function '$gte'
end

#inObject



60
61
62
# File 'lib/perpetuity/mongodb/query_expression.rb', line 60

def in
  function '$in'
end

#less_thanObject



40
41
42
# File 'lib/perpetuity/mongodb/query_expression.rb', line 40

def less_than
  function '$lt'
end

#lteObject



44
45
46
# File 'lib/perpetuity/mongodb/query_expression.rb', line 44

def lte
  function '$lte'
end

#matchesObject



64
65
66
67
68
69
70
# File 'lib/perpetuity/mongodb/query_expression.rb', line 64

def matches
  if @negated
    { @attribute => { '$not' => @value } }
  else
    { @attribute => @value }
  end
end

#negateObject



80
81
82
83
84
# File 'lib/perpetuity/mongodb/query_expression.rb', line 80

def negate
  expr = dup
  expr.negated = true
  expr
end

#not_equalObject



56
57
58
# File 'lib/perpetuity/mongodb/query_expression.rb', line 56

def not_equal
  function '$ne'
end

#to_dbObject



18
19
20
# File 'lib/perpetuity/mongodb/query_expression.rb', line 18

def to_db
  public_send @comparator
end

#|(other) ⇒ Object



72
73
74
# File 'lib/perpetuity/mongodb/query_expression.rb', line 72

def | other
  QueryUnion.new(self, other)
end