Class: AlpacaBuildTool::XmlNode
- Inherits:
-
Object
- Object
- AlpacaBuildTool::XmlNode
- Defined in:
- lib/alpacabuildtool/entities/xml_node.rb
Overview
XmlNode represents simple xml node that can contain attributes and other nodes or string as a content
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#attribute(name, value) ⇒ Object
Adds attribute to node.
-
#initialize(name, content = nil, &block) ⇒ XmlNode
constructor
Creates an instance.
-
#node(name, content = nil, &block) ⇒ Object
Adds new node into content.
-
#to_s ⇒ Object
Overrides string representation to populate xml pretty formatted content.
Constructor Details
#initialize(name, content = nil, &block) ⇒ XmlNode
Creates an instance
name
-
name of node
content
-
content of node (if set, block is ignored)
accepts &block to set content to array of nodes
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/alpacabuildtool/entities/xml_node.rb', line 16 def initialize(name, content = nil, &block) @name = name return @content = content unless content.nil? return unless block_given? @arity = block.arity if @arity <= 0 @context = eval('self', block.binding) instance_eval(&block) else yield self end end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
8 9 10 |
# File 'lib/alpacabuildtool/entities/xml_node.rb', line 8 def content @content end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/alpacabuildtool/entities/xml_node.rb', line 8 def name @name end |
Instance Method Details
#attribute(name, value) ⇒ Object
Adds attribute to node
name
-
attribute name
value
-
attribute value
52 53 54 |
# File 'lib/alpacabuildtool/entities/xml_node.rb', line 52 def attribute(name, value) (@attributes ||= {})[name] = value end |
#node(name, content = nil, &block) ⇒ Object
Adds new node into content
name
-
node name
content
-
node content
accepts &block for this new node
XmlNode.new 'a' do
node 'b' do
node 'c' do
...
end
end
end
43 44 45 |
# File 'lib/alpacabuildtool/entities/xml_node.rb', line 43 def node(name, content = nil, &block) (@content ||= []) << XmlNode.new(name, content, &block) end |
#to_s ⇒ Object
Overrides string representation to populate xml pretty formatted content
58 59 60 61 62 |
# File 'lib/alpacabuildtool/entities/xml_node.rb', line 58 def to_s return "<#{@name}#{attributes_to_s}/>" if @content.nil? content = @content.is_a?(Array) ? array_to_s : CGI.escape_html(@content) "<#{@name}#{attributes_to_s}>#{content}</#{@name}>" end |