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 =
{}

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



70
71
72
73
74
75
76
# File 'lib/nagoro/pipe/element.rb', line 70

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

#tag(tag, original_attrs, value_attrs) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/nagoro/pipe/element.rb', line 17

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

  if estruct
    attrs, element, content = estruct.values_at(1..3)

    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
  end
end

#tag_end(tag) ⇒ Object



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

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



40
41
42
43
44
45
46
47
48
49
# File 'lib/nagoro/pipe/element.rb', line 40

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



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/nagoro/pipe/element.rb', line 78

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