Class: Norikra::Query::ASTNode

Inherits:
Object
  • Object
show all
Defined in:
lib/norikra/query/ast.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, children, tree) ⇒ ASTNode

Returns a new instance of ASTNode.



766
767
768
769
770
# File 'lib/norikra/query/ast.rb', line 766

def initialize(name, children, tree)
  @name = name
  @children = children
  @tree = tree
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



764
765
766
# File 'lib/norikra/query/ast.rb', line 764

def children
  @children
end

#nameObject

Returns the value of attribute name.



764
765
766
# File 'lib/norikra/query/ast.rb', line 764

def name
  @name
end

#treeObject

Returns the value of attribute tree.



764
765
766
# File 'lib/norikra/query/ast.rb', line 764

def tree
  @tree
end

Instance Method Details

#childObject



780
781
782
# File 'lib/norikra/query/ast.rb', line 780

def child
  @children.first
end

#fields(default_target = nil, known_targets_aliases = []) ⇒ Object



811
812
813
# File 'lib/norikra/query/ast.rb', line 811

def fields(default_target=nil, known_targets_aliases=[])
  @children.map{|c| c.nodetype?(:subquery) ? [] : c.fields(default_target, known_targets_aliases)}.reduce(&:+) || []
end

#find(type) ⇒ Object

only one, depth-first search



784
785
786
787
788
789
790
791
792
793
# File 'lib/norikra/query/ast.rb', line 784

def find(type) # only one, depth-first search
  return self if type.is_a?(String) && @name == type || nodetype?(type)

  @children.each do |c|
    next if type != :subquery && c.nodetype?(:subquery)
    r = c.find(type)
    return r if r
  end
  nil
end

#listup(*type) ⇒ Object

search all nodes that has ‘type’



795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
# File 'lib/norikra/query/ast.rb', line 795

def listup(*type) # search all nodes that has 'type'
  if type.size > 1
    return type.map{|t| self.listup(t) }.reduce(&:+)
  end
  type = type.first

  result = []
  result.push(self) if type.is_a?(String) && @name == type || nodetype?(type)

  @children.each do |c|
    next if type != :subquery && c.nodetype?(:subquery)
    result.push(*c.listup(type))
  end
  result
end

#nodetype?(*sym) ⇒ Boolean

Returns:

  • (Boolean)


772
773
774
# File 'lib/norikra/query/ast.rb', line 772

def nodetype?(*sym)
  false
end

#to_aObject



776
777
778
# File 'lib/norikra/query/ast.rb', line 776

def to_a
  [@name] + @children.map{|c| c.children.size > 0 ? c.to_a : c.name}
end