Class: MSFLVisitors::Visitor

Inherits:
Object
  • Object
show all
Defined in:
lib/msfl_visitors/visitor.rb

Defined Under Namespace

Classes: AggregationsVisitor, ESTermFilterVisitor, TermFilterVisitor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVisitor

Returns a new instance of Visitor.



29
30
31
32
33
# File 'lib/msfl_visitors/visitor.rb', line 29

def initialize
  self.mode = :term # or :aggregations
  self.clauses = Array.new

end

Instance Attribute Details

#clausesObject

Returns the value of attribute clauses.



26
27
28
# File 'lib/msfl_visitors/visitor.rb', line 26

def clauses
  @clauses
end

#current_clauseObject

Returns the value of attribute current_clause.



26
27
28
# File 'lib/msfl_visitors/visitor.rb', line 26

def current_clause
  @current_clause
end

#mode=(value) ⇒ Object

Sets the attribute mode

Parameters:

  • value

    the value to set the attribute mode to.



27
28
29
# File 'lib/msfl_visitors/visitor.rb', line 27

def mode=(value)
  @mode = value
end

Instance Method Details

#get_visitorObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/msfl_visitors/visitor.rb', line 51

def get_visitor
  case mode
    when :term
      TermFilterVisitor.new(self)
    when :es_term
      ESTermFilterVisitor.new(self)
    else
      AggregationsVisitor.new(self)
  end
end

#in_aggregation_modeObject



62
63
64
65
66
67
# File 'lib/msfl_visitors/visitor.rb', line 62

def in_aggregation_mode
  self.mode = :aggregations
  result = yield if block_given?
  self.mode = :term
  result
end

#visit(node) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/msfl_visitors/visitor.rb', line 35

def visit(node)
  if mode == :es_term
    get_visitor.visit(node)
  else
    case node
      when Nodes::Partial
        in_aggregation_mode do
          clauses.concat get_visitor.visit(node)
          ""
        end
      else
        get_visitor.visit(node)
    end
  end
end

#visit_tree(root) ⇒ Object



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

def visit_tree(root)
  [{clause: root.accept(self)}].concat(clauses).reject { |c| c[:clause] == "" }
end