Class: Fabulator::Core::Actions::ForEach

Inherits:
Structural
  • Object
show all
Defined in:
lib/fabulator/core/actions/for_each.rb

Instance Method Summary collapse

Methods inherited from Structural

#accepts_structural?, accepts_structural?, #compile_xml, contained_in, contains, element, #initialize, structurals

Constructor Details

This class inherits a constructor from Fabulator::Structural

Instance Method Details

#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
47
48
49
50
51
# File 'lib/fabulator/core/actions/for_each.rb', line 14

def run(context, autovivify = false)
  @context.with(context) do |ctx|
    items = self.select(ctx)
    res = nil
    ctx.in_context do |c|
      if !@sorts.empty?
        if self.as.nil?
          items = items.sort_by{ |i| 
            @sorts.collect{|s| s.run(c.with_root(i)) }.join("\0") 
          }
        else
          items = items.sort_by{ |i| 
            r = nil
            c.in_context do |cc|
              cc.set_var(self.as, i)
              r = @sorts.collect{|s| s.run(cc.with_root(i)) }.join("\0") 
            end
            r
          }
        end
      end
      res = [ ]
      if self.as.nil?
        items.each do |i|
          res = res + self.run_actions(c.with_root(i))
        end
      else
        items.each do |i|
          c.in_context do |cc|
            cc.set_var(self.as, i) unless self.as.nil?
            res = res + self.run_actions(cc.with_root(i))
          end
        end
      end
    end
    return res
  end
end