Class: REXMLUtilityNode
Overview
This is a slighly modified version of the XMLUtilityNode from merb.devjavu.com/projects/merb/ticket/95 ([email protected]) It’s mainly just adding vowels, as I ht cd wth n vwls :) This represents the hard part of the work, all I did was change the underlying parser.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#children ⇒ Object
Returns the value of attribute children.
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
- .available_typecasts ⇒ Object
- .available_typecasts=(obj) ⇒ Object
- .typecasts ⇒ Object
- .typecasts=(obj) ⇒ Object
Instance Method Summary collapse
- #add_node(node) ⇒ Object
-
#initialize(name, attributes = {}) ⇒ REXMLUtilityNode
constructor
A new instance of REXMLUtilityNode.
-
#inner_html ⇒ Object
Get the inner_html of the REXML node.
- #to_hash ⇒ Object
-
#to_html ⇒ String
Converts the node into a readable HTML node.
- #to_s ⇒ Object
-
#translate_xml_entities(value) ⇒ #gsub
Convert basic XML entities into their literal values.
-
#typecast_value(value) ⇒ Integer, ...
Typecasts a value based upon its type.
-
#undasherize_keys(params) ⇒ Object
Take keys of the form foo-bar and convert them to foo_bar.
Constructor Details
#initialize(name, attributes = {}) ⇒ REXMLUtilityNode
Returns a new instance of REXMLUtilityNode.
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/core_extensions.rb', line 126 def initialize(name, attributes = {}) @name = name.tr("-", "_") # leave the type alone if we don't know what it is @type = self.class.available_typecasts.include?(attributes["type"]) ? attributes.delete("type") : attributes["type"] @nil_element = attributes.delete("nil") == "true" @attributes = undasherize_keys(attributes) @children = [] @text = false end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
92 93 94 |
# File 'lib/core_extensions.rb', line 92 def attributes @attributes end |
#children ⇒ Object
Returns the value of attribute children.
92 93 94 |
# File 'lib/core_extensions.rb', line 92 def children @children end |
#name ⇒ Object
Returns the value of attribute name.
92 93 94 |
# File 'lib/core_extensions.rb', line 92 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
92 93 94 |
# File 'lib/core_extensions.rb', line 92 def type @type end |
Class Method Details
.available_typecasts ⇒ Object
102 103 104 |
# File 'lib/core_extensions.rb', line 102 def self.available_typecasts @@available_typecasts end |
.available_typecasts=(obj) ⇒ Object
106 107 108 |
# File 'lib/core_extensions.rb', line 106 def self.available_typecasts=(obj) @@available_typecasts = obj end |
.typecasts ⇒ Object
94 95 96 |
# File 'lib/core_extensions.rb', line 94 def self.typecasts @@typecasts end |
.typecasts=(obj) ⇒ Object
98 99 100 |
# File 'lib/core_extensions.rb', line 98 def self.typecasts=(obj) @@typecasts = obj end |
Instance Method Details
#add_node(node) ⇒ Object
137 138 139 140 |
# File 'lib/core_extensions.rb', line 137 def add_node(node) @text = true if node.is_a? String @children << node end |
#inner_html ⇒ Object
Get the inner_html of the REXML node.
243 244 245 |
# File 'lib/core_extensions.rb', line 243 def inner_html @children.join end |
#to_hash ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/core_extensions.rb', line 142 def to_hash if @type == "file" f = StringIO.new((@children.first || '').unpack('m').first) class << f attr_accessor :original_filename, :content_type end f.original_filename = attributes['name'] || 'untitled' f.content_type = attributes['content_type'] || 'application/octet-stream' return {name => f} end if @text return { name => typecast_value( translate_xml_entities( inner_html ) ) } else #change repeating groups into an array groups = @children.inject({}) { |s,e| (s[e.name] ||= []) << e; s } out = nil if @type == "array" out = [] groups.each do |k, v| if v.size == 1 out << v.first.to_hash.entries.first.last else out << v.map{|e| e.to_hash[k]} end end out = out.flatten else # If Hash out = {} groups.each do |k,v| if v.size == 1 out.merge!(v.first) else out.merge!( k => v.map{|e| e.to_hash[k]}) end end out.merge! attributes unless attributes.empty? out = out.empty? ? nil : out end if @type && out.nil? { name => typecast_value(out) } else { name => out } end end end |
#to_html ⇒ String
Converts the node into a readable HTML node.
250 251 252 253 |
# File 'lib/core_extensions.rb', line 250 def to_html attributes.merge!(:type => @type ) if @type "<#{name}#{attributes.to_xml_attributes}>#{@nil_element ? '' : inner_html}</#{name}>" end |
#to_s ⇒ Object
256 257 258 |
# File 'lib/core_extensions.rb', line 256 def to_s to_html end |
#translate_xml_entities(value) ⇒ #gsub
Convert basic XML entities into their literal values.
226 227 228 229 230 231 232 |
# File 'lib/core_extensions.rb', line 226 def translate_xml_entities(value) value.gsub(/</, "<"). gsub(/>/, ">"). gsub(/"/, '"'). gsub(/'/, "'"). gsub(/&/, "&") end |
#typecast_value(value) ⇒ Integer, ...
If self
does not have a “type” key, or if it’s not one of the options specified above, the raw value
will be returned.
Typecasts a value based upon its type. For instance, if node
has #type == “integer”, #=> 12]}
215 216 217 218 219 |
# File 'lib/core_extensions.rb', line 215 def typecast_value(value) return value unless @type proc = self.class.typecasts[@type] proc.nil? ? value : proc.call(value) end |
#undasherize_keys(params) ⇒ Object
Take keys of the form foo-bar and convert them to foo_bar
235 236 237 238 239 240 |
# File 'lib/core_extensions.rb', line 235 def undasherize_keys(params) params.keys.each do |key, value| params[key.tr("-", "_")] = params.delete(key) end params end |