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.



887
888
889
890
891
# File 'lib/norikra/query/ast.rb', line 887

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

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



885
886
887
# File 'lib/norikra/query/ast.rb', line 885

def children
  @children
end

#nameObject

Returns the value of attribute name.



885
886
887
# File 'lib/norikra/query/ast.rb', line 885

def name
  @name
end

#treeObject

Returns the value of attribute tree.



885
886
887
# File 'lib/norikra/query/ast.rb', line 885

def tree
  @tree
end

Instance Method Details

#chain(*nodes) ⇒ Object



897
898
899
# File 'lib/norikra/query/ast.rb', line 897

def chain(*nodes)
  nodes.reduce(self){|n, next_node| n && n.has_a?(next_node) ? n.child : nil }
end

#childObject



909
910
911
# File 'lib/norikra/query/ast.rb', line 909

def child
  @children.first
end

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



940
941
942
# File 'lib/norikra/query/ast.rb', line 940

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



913
914
915
916
917
918
919
920
921
922
# File 'lib/norikra/query/ast.rb', line 913

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

#has_a?(name) ⇒ Boolean

Returns:

  • (Boolean)


893
894
895
# File 'lib/norikra/query/ast.rb', line 893

def has_a?(name)
  @children.size == 1 && (@children.first.name == name || @children.first.nodetype?(name))
end

#listup(*type) ⇒ Object

search all nodes that has ‘type’



924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
# File 'lib/norikra/query/ast.rb', line 924

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)


901
902
903
# File 'lib/norikra/query/ast.rb', line 901

def nodetype?(*sym)
  false
end

#to_aObject



905
906
907
# File 'lib/norikra/query/ast.rb', line 905

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