Class: XmlFu::Node
- Inherits:
-
Object
- Object
- XmlFu::Node
- Defined in:
- lib/xml-fu/node.rb
Overview
Class to contain logic for converting a key/value pair into an XML node
Defined Under Namespace
Classes: InvalidAttributesException
Constant Summary collapse
- XS_DATETIME_FORMAT =
xs:dateTime format.
"%Y-%m-%dT%H:%M:%SZ"
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#content_type ⇒ Object
Returns the value of attribute content_type.
-
#escape_xml ⇒ Object
Returns the value of attribute escape_xml.
-
#name ⇒ Object
Returns the value of attribute name.
-
#self_closing ⇒ Object
Returns the value of attribute self_closing.
Instance Method Summary collapse
-
#initialize(name, value, attributes = {}) ⇒ Node
constructor
Create XmlFu::Node object.
-
#name_parse_special_characters(val) ⇒ Object
Converts name into proper XML node name.
-
#to_xml ⇒ Object
Create XML String from XmlFu::Node object.
-
#value ⇒ String?
Value can be nil, else it should return a String value.
-
#value=(val) ⇒ Object
Custom Setter for @value instance method.
Constructor Details
#initialize(name, value, attributes = {}) ⇒ Node
Create XmlFu::Node object
26 27 28 29 30 31 32 33 |
# File 'lib/xml-fu/node.rb', line 26 def initialize(name, value, attributes={}) @escape_xml = true @self_closing = false @content_type = "container" self.attributes = attributes self.value = value self.name = name end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
36 37 38 |
# File 'lib/xml-fu/node.rb', line 36 def attributes @attributes end |
#content_type ⇒ Object
Returns the value of attribute content_type.
20 21 22 |
# File 'lib/xml-fu/node.rb', line 20 def content_type @content_type end |
#escape_xml ⇒ Object
Returns the value of attribute escape_xml.
18 19 20 |
# File 'lib/xml-fu/node.rb', line 18 def escape_xml @escape_xml end |
#name ⇒ Object
Returns the value of attribute name.
46 47 48 |
# File 'lib/xml-fu/node.rb', line 46 def name @name end |
#self_closing ⇒ Object
Returns the value of attribute self_closing.
19 20 21 |
# File 'lib/xml-fu/node.rb', line 19 def self_closing @self_closing end |
Instance Method Details
#name_parse_special_characters(val) ⇒ Object
Converts name into proper XML node name
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/xml-fu/node.rb', line 66 def name_parse_special_characters(val) use_this = val.dup # Ensure that we don't have special characters at end of name while ["!","/","*"].include?(use_this.to_s[-1,1]) do # Will this node contain escaped XML? if use_this.to_s[-1,1] == '!' @escape_xml = false use_this.chop! end # Will this be a self closing node? if use_this.to_s[-1,1] == '/' @self_closing = true use_this.chop! end # Will this node contain a collection of sibling nodes? if use_this.to_s[-1,1] == '*' @content_type = "collection" use_this.chop! end end return use_this end |
#to_xml ⇒ Object
Create XML String from XmlFu::Node object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/xml-fu/node.rb', line 129 def to_xml xml = Builder::XmlMarkup.new case when @self_closing && @content_type == 'container' xml.tag!(@name, @attributes) when @value.nil? xml.tag!(@name, @attributes.merge!("xsi:nil" => "true")) when ::Hash === @value xml.tag!(@name, @attributes) { xml << XmlFu::Hash.to_xml(@value) } when ::Array === @value case @content_type when "collection" xml << XmlFu::Array.to_xml(@value.flatten, { :key => (@self_closing ? "#{@name}/" : @name), :attributes => @attributes, :content_type => "collection" }) when "container" xml.tag!(@name, @attributes) { xml << XmlFu::Array.to_xml(@value) } else # Shouldn't be anything else end else xml.tag!(@name, @attributes) { xml << self.value.to_s } end xml.target! end |
#value ⇒ String?
Value can be nil, else it should return a String value.
122 123 124 125 |
# File 'lib/xml-fu/node.rb', line 122 def value return CGI.escapeHTML(@value) if String === @value && @escape_xml return @value end |
#value=(val) ⇒ Object
Custom Setter for @value instance method
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/xml-fu/node.rb', line 95 def value=(val) case val when ::String then @value = val.to_s when ::Hash then @value = val when ::Array then @value = val when ::OpenStruct then @value = val when ::DateTime then @value = val.strftime XS_DATETIME_FORMAT when ::Time then @value = val.strftime XS_DATETIME_FORMAT when ::Date then @value = val.strftime XS_DATETIME_FORMAT else if val.respond_to?(:to_datetime) @value = val.to_datetime elsif val.respond_to?(:call) @value = val.call elsif val.nil? @value = nil else @value = val.to_s end end rescue => e @value = val.to_s end |