Class: Fabulator::Expr::Step

Inherits:
Object
  • Object
show all
Defined in:
lib/fabulator/expr/step.rb

Instance Method Summary collapse

Constructor Details

#initialize(a, n) ⇒ Step

Returns a new instance of Step.



4
5
6
7
# File 'lib/fabulator/expr/step.rb', line 4

def initialize(a,n)
  @axis = a
  @node_test = n
end

Instance Method Details

#create_node(context) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/fabulator/expr/step.rb', line 33

def create_node(context)
  return nil if node_text == '*'

  c = Fabulator::Expr::Node.new(context.root.axis, context.root.roots, nil, [])
  c.name = @node_test
  context.root.add_child(c)
  c
end

#run(context, autovivify = false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fabulator/expr/step.rb', line 9

def run(context, autovivify = false)
  c = context.root
  if !@axis.nil? && @axis != '' && context.root.roots.has_key?(@axis) &&
      @axis != context.root.axis
    c = context.root.roots[@axis]
  end
  if @node_test.is_a?(String)
    n = @node_test
  else
    n = (@node_test.run(context).last.value rescue nil)
  end 
  return [ ] if n.nil?
  if n == '*'
    possible = c.children
  else
    possible = c.children.select{ |cc| cc.name == n }
    if possible.empty? && autovivify
      #Rails.logger.info("Autovivifying #{n}")
      possible = context.with_root(c).traverse_path([ n ], true)
    end
  end
  return possible
end