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) ⇒ ASTNode

Returns a new instance of ASTNode.



107
108
109
110
# File 'lib/norikra/query/ast.rb', line 107

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

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



105
106
107
# File 'lib/norikra/query/ast.rb', line 105

def children
  @children
end

#nameObject

Returns the value of attribute name.



105
106
107
# File 'lib/norikra/query/ast.rb', line 105

def name
  @name
end

Instance Method Details

#childObject



120
121
122
# File 'lib/norikra/query/ast.rb', line 120

def child
  @children.first
end

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



146
147
148
# File 'lib/norikra/query/ast.rb', line 146

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



124
125
126
127
128
129
130
131
132
133
# File 'lib/norikra/query/ast.rb', line 124

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’



135
136
137
138
139
140
141
142
143
144
# File 'lib/norikra/query/ast.rb', line 135

def listup(type) # search all nodes that has 'type'
  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:



112
113
114
# File 'lib/norikra/query/ast.rb', line 112

def nodetype?(*sym)
  false
end

#to_aObject



116
117
118
# File 'lib/norikra/query/ast.rb', line 116

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