Class: Stretchy::AndCollector

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/stretchy/and_collector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes, context = {}) ⇒ AndCollector

Returns a new instance of AndCollector.



11
12
13
14
# File 'lib/stretchy/and_collector.rb', line 11

def initialize(nodes, context = {})
  @nodes    = nodes
  @context  = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



9
10
11
# File 'lib/stretchy/and_collector.rb', line 9

def context
  @context
end

#nodesObject (readonly)

Returns the value of attribute nodes.



9
10
11
# File 'lib/stretchy/and_collector.rb', line 9

def nodes
  @nodes
end

Instance Method Details

#boost_nodesObject



38
39
40
# File 'lib/stretchy/and_collector.rb', line 38

def boost_nodes
  @boost_nodes ||= nodes.select {|n| n.context? :boost }
end

#context?(*args) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/stretchy/and_collector.rb', line 20

def context?(*args)
  args.all? {|c| !!context[c] }
end

#function_score_node?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/stretchy/and_collector.rb', line 42

def function_score_node?
  boost_nodes.reject { |n| n.empty? }.any?
end

#nodeObject



24
25
26
27
28
29
30
31
32
# File 'lib/stretchy/and_collector.rb', line 24

def node
  @node ||= if function_score_node?
    function_score_node
  elsif query_nodes.any?
    query_node
  else
    Node.new({match_all: {}}, context)
  end
end

#query_nodesObject



34
35
36
# File 'lib/stretchy/and_collector.rb', line 34

def query_nodes
  @query_nodes ||= nodes.reject {|n| n.context? :boost }
end

#with_context(new_context) ⇒ Object



16
17
18
# File 'lib/stretchy/and_collector.rb', line 16

def with_context(new_context)
  self.class.new nodes, new_context
end