Class: Deplate::Regions::Foreach

Inherits:
Deplate::Region show all
Defined in:
lib/deplate/regions.rb

Instance Method Summary collapse

Methods inherited from Deplate::Region

check_file, clean_strings, #deprecated_regnote, deprecated_regnote, #finish_accum, #format_compound, regions, register_as, set_line_cont, #setup

Methods inherited from Element

#join_lines, #join_lines_re_zh_cn

Instance Method Details

#finishObject



684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
# File 'lib/deplate/regions.rb', line 684

def finish
    finish_accum
    @id = Deplate::Core.split_list(@args['@id'], ',', '; ', @source)
    unless @id
        log('No ID given', :error)
        return nil
    end
    doc = @args['var'] || @args['doc']
    if doc
        @list = @deplate.variables[doc]
    else
        @list = deprecated_regnote('each')
    end
    case @list
    when nil
        log('Missing arguments', :error)
        return nil
    when Array
        @list = @list.flatten
    else
        rx = @args['rx']
        if rx
            @list = @list.split(Regexp.new(rx))
        else
            sep = @args['sep']
            if sep
                sep = Regexp.new(Regexp.escape(sep))
                @list = @list.split(sep)
            else
                @list = [@list]
            end
        end
    end
    use_template = !(@args['noTemplate'] || deplate.variables['legacyFor1'])
    if use_template
        tmpl = Deplate::Template.new(:master    => @deplate,
                                     :template  => @accum,
                                     :source    => @source,
                                     :container => self)
    end
    @foreach = []
    while !@list.empty?
        ids = {}
        for i in @id
            ids[i] = @list.shift
        end
        Deplate::Define.let_variables(@deplate, ids) do
            if use_template
                body = tmpl.fill_in(deplate, :source => @source)
            else
                body = @accum
            end
            acc  = @deplate.parsed_array_from_strings(body, 1 + @source.begin, @source.file)
            @prototype ||= acc[0]
            @foreach << [ids, acc]
        end
    end
    return self
end

#process_particles(&block) ⇒ Object



744
745
746
747
748
749
750
751
752
# File 'lib/deplate/regions.rb', line 744

def process_particles(&block)
    @elt = []
    for ids, acc in @foreach
        Deplate::Define.let_variables(@deplate, ids) do
            @elt << acc.collect {|e| e.process}
        end
    end
    @elt = @elt.flatten.compact
end