Class: REXMLUtilityNode
- Inherits:
-
Object
- Object
- REXMLUtilityNode
- Defined in:
- lib/crack/xml.rb
Overview
This is a slighly modified version of the XMLUtilityNode from merb.devjavu.com/projects/merb/ticket/95 (has.sox@gmail.com) 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)
-
- (Object) attributes
:nodoc:.
-
- (Object) children
:nodoc:.
-
- (Object) name
:nodoc:.
-
- (Object) type
:nodoc:.
Class Method Summary (collapse)
- + (Object) available_typecasts
- + (Object) available_typecasts=(obj)
- + (Object) typecasts
- + (Object) typecasts=(obj)
Instance Method Summary (collapse)
- - (Object) add_node(node)
-
- (REXMLUtilityNode) initialize(name, normalized_attributes = {})
constructor
A new instance of REXMLUtilityNode.
-
- (Object) inner_html
Get the inner_html of the REXML node.
- - (Object) to_hash
-
- (String) to_html
Converts the node into a readable HTML node.
- - (Object) to_s
-
- (Integer, ...) typecast_value(value)
Typecasts a value based upon its type.
-
- (Object) undasherize_keys(params)
Take keys of the form foo-bar and convert them to foo_bar.
Constructor Details
- (REXMLUtilityNode) initialize(name, normalized_attributes = {})
A new instance of REXMLUtilityNode
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/crack/xml.rb', line 48 def initialize(name, normalized_attributes = {}) # unnormalize attribute values attributes = Hash[* normalized_attributes.map { |key, value| [ key, unnormalize_xml_entities(value) ] }.flatten] @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
- (Object) attributes
:nodoc:
16 17 18 |
# File 'lib/crack/xml.rb', line 16 def attributes @attributes end |
- (Object) children
:nodoc:
16 17 18 |
# File 'lib/crack/xml.rb', line 16 def children @children end |
- (Object) name
:nodoc:
16 17 18 |
# File 'lib/crack/xml.rb', line 16 def name @name end |
- (Object) type
:nodoc:
16 17 18 |
# File 'lib/crack/xml.rb', line 16 def type @type end |
Class Method Details
+ (Object) available_typecasts
26 27 28 |
# File 'lib/crack/xml.rb', line 26 def self.available_typecasts @@available_typecasts end |
+ (Object) available_typecasts=(obj)
30 31 32 |
# File 'lib/crack/xml.rb', line 30 def self.available_typecasts=(obj) @@available_typecasts = obj end |
+ (Object) typecasts
18 19 20 |
# File 'lib/crack/xml.rb', line 18 def self.typecasts @@typecasts end |
+ (Object) typecasts=(obj)
22 23 24 |
# File 'lib/crack/xml.rb', line 22 def self.typecasts=(obj) @@typecasts = obj end |
Instance Method Details
- (Object) add_node(node)
65 66 67 68 |
# File 'lib/crack/xml.rb', line 65 def add_node(node) @text = true if node.is_a? String @children << node end |
- (Object) inner_html
Get the inner_html of the REXML node.
165 166 167 |
# File 'lib/crack/xml.rb', line 165 def inner_html @children.join end |
- (Object) to_hash
70 71 72 73 74 75 76 77 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 124 125 |
# File 'lib/crack/xml.rb', line 70 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 t = typecast_value( unnormalize_xml_entities( inner_html ) ) if t.is_a?(String) class << t attr_accessor :attributes end t.attributes = attributes end return { name => t } 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 |
- (String) to_html
Converts the node into a readable HTML node.
172 173 174 175 |
# File 'lib/crack/xml.rb', line 172 def to_html attributes.merge!(:type => @type ) if @type "<#{name}#{Crack::Util.to_xml_attributes(attributes)}>#{@nil_element ? '' : inner_html}</#{name}>" end |
- (Object) to_s
178 179 180 |
# File 'lib/crack/xml.rb', line 178 def to_s to_html end |
- (Integer, ...) typecast_value(value)
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]}
150 151 152 153 154 |
# File 'lib/crack/xml.rb', line 150 def typecast_value(value) return value unless @type proc = self.class.typecasts[@type] proc.nil? ? value : proc.call(value) end |
- (Object) undasherize_keys(params)
Take keys of the form foo-bar and convert them to foo_bar
157 158 159 160 161 162 |
# File 'lib/crack/xml.rb', line 157 def undasherize_keys(params) params.keys.each do |key, value| params[key.tr("-", "_")] = params.delete(key) end params end |