Class: Hash
Direct Known Subclasses
Class Method Summary collapse
-
.from_xml(xml) ⇒ Hash
Converts valid XML into a Ruby Hash structure.
Instance Method Summary collapse
- #add_html_class!(html_class) ⇒ Object
-
#environmentize_keys! ⇒ Hash
Destructively and non-recursively convert each key to an uppercase string, deleting nil values along the way.
-
#except(*rejected) ⇒ Hash
Create a hash with all key/value pairs in receiver except
rejected
. -
#normalize_param(key, value) ⇒ String
Convert a key, value pair into a URL query param string.
-
#only(*allowed) ⇒ Hash
Create a hash with only key/value pairs in receiver and
allowed
. -
#protect_keys! ⇒ Array
Converts all keys into string values.
-
#to_mash ⇒ Mash
Convert to Mash.
-
#to_params ⇒ String
Convert to URL query param string.
-
#to_xml_attributes ⇒ String
(also: #to_html_attributes)
The hash as attributes for an XML tag.
-
#unprotect_keys! ⇒ Object
Attempts to convert all string keys into Class keys.
Class Method Details
.from_xml(xml) ⇒ Hash
Converts valid XML into a Ruby Hash structure.
Mixed content is treated as text and any tags in it are left unparsed
Any attributes other than type on a node containing a text node will be discarded
- Typecasting is performed on elements that have a
type
attribute: - integer
-
Returns an Integer
- boolean
-
Anything other than “true” evaluates to false.
- datetime
-
Returns a Time object. See Time documentation for valid Time strings.
- date
-
Returns a Date object. See Date documentation for valid Date strings.
Keys are automatically converted to snake_case
- Simple
-
<user gender=‘m’>
<age type='integer'>35</age> <name>Home Simpson</name> <dob type='date'>1988-01-01</dob> <joined-at type='datetime'>2000-04-28 23:01</joined-at> <is-cool type='boolean'>true</is-cool>
</user>
Becomes:
{ "user" => {
"gender" => "m",
"age" => 35,
"name" => "Home Simpson",
"dob" => DateObject( 1998-01-01 ),
"joined_at" => TimeObject( 2000-04-28 23:01),
"is_cool" => true
}
}
- Mixed Content
-
<story>
A Quick <em>brown</em> Fox
</story>
Evaluates to:
{ "story" => "A Quick <em>brown</em> Fox" }
- Attributes other than type on a node containing text
-
<story is-good=‘false’>
A Quick <em>brown</em> Fox
</story>
Are ignored:
{ "story" => "A Quick <em>brown</em> Fox" }
- Other attributes in addition to
type
-
<bicep unit=‘inches’ type=‘integer’>60</bicep>
Evaluates with a typecast to an integer. But unit attribute is ignored:
{ "bicep" => 60 }
81 82 83 |
# File 'lib/extlib/hash.rb', line 81 def from_xml( xml ) ToHashParser.from_xml(xml) end |
Instance Method Details
#add_html_class!(html_class) ⇒ Object
209 210 211 212 213 214 215 |
# File 'lib/extlib/hash.rb', line 209 def add_html_class!(html_class) if self[:class] self[:class] = "#{self[:class]} #{html_class}" else self[:class] = html_class.to_s end end |
#environmentize_keys! ⇒ Hash
Destructively and non-recursively convert each key to an uppercase string, deleting nil values along the way.
250 251 252 253 254 255 256 257 |
# File 'lib/extlib/hash.rb', line 250 def environmentize_keys! keys.each do |key| val = delete(key) next if val.nil? self[key.to_s.upcase] = val end self end |
#except(*rejected) ⇒ Hash
Create a hash with all key/value pairs in receiver except rejected
{ :one => 1, :two => 2, :three => 3 }.except(:one)
#=> { :two => 2, :three => 3 }
181 182 183 184 185 |
# File 'lib/extlib/hash.rb', line 181 def except(*rejected) hash = self.dup rejected.each {|k| hash.delete(k) } hash end |
#normalize_param(key, value) ⇒ String
Convert a key, value pair into a URL query param string
normalize_param(:name, "Bob") #=> "name=Bob&"
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/extlib/hash.rb', line 129 def normalize_param(key, value) param = '' stack = [] if value.is_a?(Array) param << value.map { |element| normalize_param("#{key}[]", element) }.join elsif value.is_a?(Hash) stack << [key,value] else param << "#{key}=#{value}&" end stack.each do |parent, hash| hash.each do |k, v| if v.is_a?(Hash) stack << ["#{parent}[#{k}]", v] else param << normalize_param("#{parent}[#{k}]", v) end end end param end |
#only(*allowed) ⇒ Hash
Create a hash with only key/value pairs in receiver and allowed
{ :one => 1, :two => 2, :three => 3 }.only(:one) #=> { :one => 1 }
164 165 166 167 168 |
# File 'lib/extlib/hash.rb', line 164 def only(*allowed) hash = {} allowed.each {|k| hash[k] = self[k] if self.has_key?(k) } hash end |
#protect_keys! ⇒ Array
Converts all keys into string values. This is used during reloading to prevent problems when classes are no longer declared.
225 226 227 |
# File 'lib/extlib/hash.rb', line 225 def protect_keys! keys.each {|key| self[key.to_s] = delete(key) } end |
#to_mash ⇒ Mash
91 92 93 94 95 |
# File 'lib/extlib/hash.rb', line 91 def to_mash hash = Mash.new(self) hash.default = default hash end |
#to_params ⇒ String
Convert to URL query param string
{ :name => "Bob",
:address => {
:street => '111 Ruby Ave.',
:city => 'Ruby Central',
:phones => ['111-111-1111', '222-222-2222']
}
}.to_params
#=> "name=Bob&address[city]=Ruby Central&address[phones][]=111-111-1111&address[phones][]=222-222-2222&address[street]=111 Ruby Ave."
112 113 114 115 116 |
# File 'lib/extlib/hash.rb', line 112 def to_params params = self.map { |k,v| normalize_param(k,v) }.join params.chop! # trailing & params end |
#to_xml_attributes ⇒ String Also known as: to_html_attributes
Returns The hash as attributes for an XML tag.
192 193 194 195 196 |
# File 'lib/extlib/hash.rb', line 192 def to_xml_attributes map do |k,v| %{#{k.to_s.snake_case.sub(/^(.{1,1})/) { |m| m.downcase }}="#{v}"} end.join(' ') end |
#unprotect_keys! ⇒ Object
Attempts to convert all string keys into Class keys. We run this after reloading to convert protected hashes back into usable hashes.
236 237 238 239 240 |
# File 'lib/extlib/hash.rb', line 236 def unprotect_keys! keys.each do |key| (self[Object.full_const_get(key)] = delete(key)) rescue nil end end |