Class: Nagoro::Pipe::Element

Inherits:
Base
  • Object
show all
Defined in:
lib/nagoro/pipe/element.rb

Defined Under Namespace

Classes: ElementStruct

Constant Summary collapse

ELEMENTS =
{}

Constants inherited from Base

Base::EMPTY_TAG

Instance Method Summary collapse

Methods inherited from Base

#doctype, #initialize, #instruction, #result, #tag_with, #text

Constructor Details

This class inherits a constructor from Nagoro::Pipe::Base

Instance Method Details

#append(string) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/nagoro/pipe/element.rb', line 47

def append(string)
  if @stack.empty?
    @body << string
  else
    @stack.last.content << string
  end
end

#tag_end(tag) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nagoro/pipe/element.rb', line 28

def tag_end(tag)
  estruct = @stack.reverse.find{|e| e.tag == tag}
  if estruct and estruct.tag == tag
    attrs, element, content = estruct.values_at(1..3)

    @stack.pop

    if element.respond_to?(:call)
      append element.call(content.join, attrs)
    else
      instance = element.new(content.join)
      instance.params = translate_attrs(instance, estruct.attrs)
      append instance.render
    end
  else
    super
  end
end

#tag_start(tag, original_attrs, value_attrs) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/nagoro/pipe/element.rb', line 17

def tag_start(tag, original_attrs, value_attrs)
  if element = ELEMENTS[tag]
    @stack << ElementStruct.new(tag, value_attrs, element, [])
  elsif tag =~ /^[A-Z]/
    warn "Element: '<#{tag}>' not found."
    super
  else
    super
  end
end

#translate_attrs(instance, attrs) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nagoro/pipe/element.rb', line 55

def translate_attrs(instance, attrs)
  hash = {}
  attrs.each do |key, value|
    case value
    when /^@/
      hash[key] = value
    else
      hash[key] = value
    end
  end

  hash
end