Class: Spider::TemplateBlocks::Each

Inherits:
Block show all
Defined in:
lib/spiderfw/templates/blocks/each.rb

Instance Attribute Summary

Attributes inherited from Block

#allowed_blocks, #doctype, #el, #template

Instance Method Summary collapse

Methods inherited from Block

#compile_content, #compile_text, #escape_text, #inspect, #parse_content, #var_to_scene, var_to_scene, vars_to_scene, #vars_to_scene

Constructor Details

#initialize(el, template = nil, allowed_blocks = nil) ⇒ Each

Returns a new instance of Each.



7
8
9
10
# File 'lib/spiderfw/templates/blocks/each.rb', line 7

def initialize(el, template=nil, allowed_blocks=nil)
    @repeated = []
    super
end

Instance Method Details

#compile(options = {}) ⇒ Object



12
13
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
# File 'lib/spiderfw/templates/blocks/each.rb', line 12

def compile(options={})
    init = ""
    rep_type = nil
    rep = nil
    ['sp:each', 'sp:each_index', 'sp:each_pair', 'sp:each_with_index'].each do |name|
        if (@el.has_attribute?(name))
            rep_type = name[3..-1]
            rep = @el.get_attribute(name)
            @el.remove_attribute(name)
            break
        end
    end
    return nil unless rep_type
    if (rep =~ /\s*(.+)\s*\|\s*(.+)\s*\|/)
        repeated = $1.strip
        arguments = $2.strip
    end
    c = "#{vars_to_scene(repeated)}.#{rep_type} do |#{arguments}|\n"
    
    content = Spider::TemplateBlocks.parse_element(@el, @allowed_blocks, @template).compile(options)
    content.run_code.each_line do |line|
        c += '  '+line
    end
    c += '   $out << "\n"'
    c += "\n"
    c += "end\n"
    if content.init_code && !content.init_code.strip.empty?
        init = "#{vars_to_scene(repeated, '@scene')}.#{rep_type} do |#{arguments}|\n"
        init += content.init_code 
        init += "end\n"
    end
    return CompiledBlock.new(init, c)
end

#get_following(el) ⇒ Object



46
47
48
49
50
51
# File 'lib/spiderfw/templates/blocks/each.rb', line 46

def get_following(el)
    return false if (el.class == ::Hpricot::Text)
    if (el.name == 'sp:repeated')
        @repeated << el
    end
end