Class: Hash
Direct Known Subclasses
Class Method Summary collapse
-
.from_xml(xml) ⇒ Object
Converts valid XML into a Ruby Hash structure.
Instance Method Summary collapse
-
#add_html_class!(html_class) ⇒ Object
Parameters html_class<~to_s>:: The HTML class to add to the :class key.
-
#environmentize_keys! ⇒ Object
Destructively and non-recursively convert each key to an uppercase string, deleting nil values along the way.
-
#except(*rejected) ⇒ Object
Parameters *rejected:: The hash keys to exclude.
-
#only(*allowed) ⇒ Object
Parameters *allowed:: The hash keys to include.
-
#protect_keys! ⇒ Object
Converts all keys into string values.
-
#to_mash ⇒ Object
Returns Mash:: This hash as a Mash for string or symbol key access.
-
#to_params ⇒ Object
Returns String:: This hash as a query string.
-
#to_xml_attributes ⇒ Object
(also: #to_html_attributes)
Returns String:: 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) ⇒ Object
Converts valid XML into a Ruby Hash structure.
Paramters
- xml<String>
-
A string representation of valid XML.
Notes
-
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
Typecasting is performed on elements that have a type
attribute:
- 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
Examples
Standard
<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>
evaluates to
{ "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>
evaluates to
{ "story" => "A Quick <em>brown</em> Fox" }
<bicep unit='inches' type='integer'>60</bicep>
evaluates with a typecast to an integer. But unit attribute is ignored.
{ "bicep" => 60 }
72 73 74 |
# File 'lib/merb-core/core_ext/hash.rb', line 72 def from_xml( xml ) ToHashParser.from_xml(xml) end |
Instance Method Details
#add_html_class!(html_class) ⇒ Object
Parameters
- html_class<~to_s>
-
The HTML class to add to the :class key. The html_class will be concatenated to any existing classes.
Examples
hash[:class] #=> nil
hash.add_html_class!(:selected)
hash[:class] #=> "selected"
hash.add_html_class!("class1 class2")
hash[:class] #=> "selected class1 class2"
174 175 176 177 178 179 180 |
# File 'lib/merb-core/core_ext/hash.rb', line 174 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! ⇒ Object
Destructively and non-recursively convert each key to an uppercase string, deleting nil values along the way.
Returns
- Hash
-
The newly environmentized hash.
Examples
{ :name => "Bob", :contact => { :email => "[email protected]" } }.environmentize_keys!
#=> { "NAME" => "Bob", "CONTACT" => { :email => "[email protected]" } }
214 215 216 217 218 219 220 221 |
# File 'lib/merb-core/core_ext/hash.rb', line 214 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) ⇒ Object
Parameters
- *rejected
-
The hash keys to exclude.
Returns
- Hash
-
A new hash without the selected keys.
Examples
{ :one => 1, :two => 2, :three => 3 }.except(:one)
#=> { :two => 2, :three => 3 }
145 146 147 |
# File 'lib/merb-core/core_ext/hash.rb', line 145 def except(*rejected) reject { |k,v| rejected.include?(k) } end |
#only(*allowed) ⇒ Object
Parameters
- *allowed
-
The hash keys to include.
Returns
- Hash
-
A new hash with only the selected keys.
Examples
{ :one => 1, :two => 2, :three => 3 }.only(:one)
#=> { :one => 1 }
132 133 134 |
# File 'lib/merb-core/core_ext/hash.rb', line 132 def only(*allowed) reject { |k,v| !allowed.include?(k) } end |
#protect_keys! ⇒ Object
Converts all keys into string values. This is used during reloading to prevent problems when classes are no longer declared.
Examples
hash = { One => 1, Two => 2 }.proctect_keys!
hash # => { "One" => 1, "Two" => 2 }
188 189 190 |
# File 'lib/merb-core/core_ext/hash.rb', line 188 def protect_keys! keys.each {|key| self[key.to_s] = delete(key) } end |
#to_mash ⇒ Object
Returns
- Mash
-
This hash as a Mash for string or symbol key access.
79 80 81 82 83 |
# File 'lib/merb-core/core_ext/hash.rb', line 79 def to_mash hash = Mash.new(self) hash.default = default hash end |
#to_params ⇒ Object
Returns
- String
-
This hash as a query string
Examples
{ :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-1111222-222-2222&address[street]=111 Ruby Ave."
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 |
# File 'lib/merb-core/core_ext/hash.rb', line 97 def to_params params = '' stack = [] each do |k, v| if v.is_a?(Hash) stack << [k,v] else params << "#{k}=#{v}&" end end stack.each do |parent, hash| hash.each do |k, v| if v.is_a?(Hash) stack << ["#{parent}[#{k}]", v] else params << "#{parent}[#{k}]=#{v}&" end end end params.chop! # trailing & params end |
#to_xml_attributes ⇒ Object Also known as: to_html_attributes
Returns
- String
-
The hash as attributes for an XML tag.
Examples
{ :one => 1, "two"=>"TWO" }.to_xml_attributes
#=> 'one="1" two="TWO"'
155 156 157 158 159 |
# File 'lib/merb-core/core_ext/hash.rb', line 155 def to_xml_attributes map do |k,v| %{#{k.to_s.camel_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.
Examples
# Provided that classes One and Two are declared in this scope:
hash = { "One" => 1, "Two" => 2 }.unproctect_keys!
hash # => { One => 1, Two => 2 }
199 200 201 202 203 |
# File 'lib/merb-core/core_ext/hash.rb', line 199 def unprotect_keys! keys.each do |key| (self[Object.full_const_get(key)] = delete(key)) rescue nil end end |