Class: Fabulator::Expr::PathExpr

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

Instance Method Summary collapse

Constructor Details

#initialize(pe, predicates, segment) ⇒ PathExpr

Returns a new instance of PathExpr.



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

def initialize(pe, predicates, segment)
  @primary_expr = pe
  @predicates = predicates
  @segment = (segment.is_a?(Array) ? segment : [ segment ]) - [nil]
end

Instance Method Details

#expr_type(context) ⇒ Object



10
11
12
# File 'lib/fabulator/expr/path_expr.rb', line 10

def expr_type(context)
  nil
end

#run(context, autovivify = false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fabulator/expr/path_expr.rb', line 14

def run(context, autovivify = false)
  if @primary_expr.nil?
    possible = [ context.root ]
  else
    possible = @primary_expr.run(context,autovivify).uniq
  end

  final = [ ]

  @segment = [ @segment ] unless @segment.is_a?(Array)

  possible.each do |e|
    next if e.nil?
    not_pass = false
    @predicates.each do |p|
      if !p.test(context.with_root(e))
        not_pass = true
        break
      end
    end
    next if not_pass
    pos = [ e ]
    @segment.each do |s|
      pos = pos.collect{ |p| 
        s.run(context.with_root(p), autovivify) 
      }.flatten - [ nil ]
    end
      
    final = final + pos
  end

  return final
end