Class: ElasticsearchRecord::Relation::QueryClauseTree

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch_record/relation/query_clause_tree.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(predicates) ⇒ QueryClauseTree

Returns a new instance of QueryClauseTree.



12
13
14
# File 'lib/elasticsearch_record/relation/query_clause_tree.rb', line 12

def initialize(predicates)
  @predicates = predicates
end

Class Method Details

.emptyObject



8
9
10
# File 'lib/elasticsearch_record/relation/query_clause_tree.rb', line 8

def self.empty
  @empty ||= new({}).freeze
end

Instance Method Details

#+(other) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/elasticsearch_record/relation/query_clause_tree.rb', line 48

def +(other)
  dups = dupredicates

  if key?(other.key)
    dups[other.key] += other
  else
    dups[other.key] = other
  end

  QueryClauseTree.new(dups)
end

#-(other) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/elasticsearch_record/relation/query_clause_tree.rb', line 60

def -(other)
  # check for provided :tree
  if other.key == :tree
    scope = self

    (scope.keys & other.keys).each do |key|
      scope -= other[key]
    end

    return scope
  end

  dups = dupredicates

  if key?(other.key)
    dups[other.key] -= other
    dups.delete(other.key) if dups[other.key].blank?
  end

  QueryClauseTree.new(dups)
end

#==(other) ⇒ Object



94
95
96
97
# File 'lib/elasticsearch_record/relation/query_clause_tree.rb', line 94

def ==(other)
  other.is_a?(::ElasticsearchRecord::Relation::QueryClauseTree) &&
    predicates == other.predicates
end

#[](key) ⇒ Object



42
43
44
45
46
# File 'lib/elasticsearch_record/relation/query_clause_tree.rb', line 42

def [](key)
  return nil unless key?(key)

  dupredicates[key]
end

#astObject



24
25
26
# File 'lib/elasticsearch_record/relation/query_clause_tree.rb', line 24

def ast
  predicates.values.map(&:ast)
end

#hashObject



20
21
22
# File 'lib/elasticsearch_record/relation/query_clause_tree.rb', line 20

def hash
  [self.class, predicates].hash
end

#keyObject



16
17
18
# File 'lib/elasticsearch_record/relation/query_clause_tree.rb', line 16

def key
  :tree
end

#merge(other) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/elasticsearch_record/relation/query_clause_tree.rb', line 28

def merge(other)
  dups = dupredicates

  other.each do |key, values|
    if dups.key?(key)
      dups[key] = (dups[key] + values).uniq
    else
      dups[key] = values
    end
  end

  QueryClauseTree.new(dups)
end

#or(other) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/elasticsearch_record/relation/query_clause_tree.rb', line 99

def or(other)
  left = self - other
  common = self - left
  right = other - common

  if left.empty? || right.empty?
    common
  else
    key = other.keys[0]

    left = left[key]
    right = right[key]

    or_clause = Arel::Nodes::Or.new(left, right)

    common.predicates[key] = ElasticsearchRecord::Relation::QueryClause.new(key, [Arel::Nodes::Grouping.new(or_clause)])
    common
  end
end

#|(other) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/elasticsearch_record/relation/query_clause_tree.rb', line 82

def |(other)
  dups = dupredicates

  if key?(other.key)
    dups[other.key] |= other
  else
    dups[other.key] = other
  end

  QueryClauseTree.new(dups)
end