Class: Nitro::Elements::Listener
- Inherits:
-
Object
- Object
- Nitro::Elements::Listener
- Includes:
- REXML::StreamListener
- Defined in:
- lib/nitro/compiler/elements.rb
Overview
:nodoc: all
Constant Summary collapse
- PREFIX_RE =
/^#{Element.prefix}:/- CAPITALIZED_RE =
/^[A-Z]/
Instance Attribute Summary collapse
-
#buffer ⇒ Object
Returns the value of attribute buffer.
-
#stack ⇒ Object
Returns the value of attribute stack.
Instance Method Summary collapse
- #comment(c) ⇒ Object
-
#initialize(compiler) ⇒ Listener
constructor
A new instance of Listener.
- #instruction(name, attributes) ⇒ Object
-
#is_element?(name) ⇒ Boolean
Check if a tag is a Nitro::Element.
- #tag_end(name) ⇒ Object
- #tag_start(name, attributes) ⇒ Object
- #text(str) ⇒ Object
Constructor Details
#initialize(compiler) ⇒ Listener
Returns a new instance of Listener.
21 22 23 24 25 26 |
# File 'lib/nitro/compiler/elements.rb', line 21 def initialize(compiler) super() @compiler = compiler @buffer = '' @stack = [] end |
Instance Attribute Details
#buffer ⇒ Object
Returns the value of attribute buffer.
18 19 20 |
# File 'lib/nitro/compiler/elements.rb', line 18 def buffer @buffer end |
#stack ⇒ Object
Returns the value of attribute stack.
19 20 21 |
# File 'lib/nitro/compiler/elements.rb', line 19 def stack @stack end |
Instance Method Details
#comment(c) ⇒ Object
133 134 135 136 137 |
# File 'lib/nitro/compiler/elements.rb', line 133 def comment(c) unless Glue::Template.strip_xml_comments @buffer << "<!--#{c}-->" end end |
#instruction(name, attributes) ⇒ Object
129 130 131 |
# File 'lib/nitro/compiler/elements.rb', line 129 def instruction(name, attributes) @buffer << "<?#{name}#{attributes}?>" end |
#is_element?(name) ⇒ Boolean
Check if a tag is a Nitro::Element. If found, it also tries to auto-extend the klass.
returns the Element class if found
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/nitro/compiler/elements.rb', line 78 def is_element?(name) # Doesn't support modulized classes # name = name.demodulize return false unless name =~ PREFIX_RE or name =~ CAPITALIZED_RE name = name.gsub(PREFIX_RE,'').camelize if name =~ PREFIX_RE # First try to use Nitro::Element::xxx then ::xxx begin klass = Class.by_name("Nitro::Element::#{name}") rescue end # Look into the module the controller's module if any begin namespace = @compiler.controller.name if /::/ =~ namespace namespace = namespace.gsub( /::[a-zA-Z]+$/, "::#{name}" ) klass = Class.by_name(namespace) end rescue end unless klass # Look in the root module begin klass = Class.by_name(name) rescue end unless klass return false unless klass.kind_of?( Class ) # Try to auto-extend unless klass.ancestors.include? Nitro::Element if Element.auto_extend klass.send(:include, Nitro::ElementMixin) else return false end end return klass end |
#tag_end(name) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/nitro/compiler/elements.rb', line 62 def tag_end(name) # check if the name starts with the element prefix, or # is capitalized. if is_element? name obj, @buffer, @parent = @stack.pop @buffer << obj.render else @buffer << "</#{name}>" end end |
#tag_start(name, attributes) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/nitro/compiler/elements.rb', line 31 def tag_start(name, attributes) # check if the name starts with the element prefix, or # is capitalized. if klass = is_element?(name) obj = klass.new attributes.each do | k, v | obj.instance_variable_set("@#{k}", v) end @stack.push [obj, @buffer, @parent] @buffer = obj._text @parent.add_child(obj) if @parent @parent = obj else # This is a static element. attrs = [] attributes.each do | k, v | attrs << %|#{k}="#{v}"| end attrs = attrs.empty? ? nil : " #{attrs.join(' ')}" @buffer << "<#{name}#{attrs}>" end end |
#text(str) ⇒ Object
125 126 127 |
# File 'lib/nitro/compiler/elements.rb', line 125 def text(str) @buffer << str end |