Parent

Hash

Public Class Methods

from_xml( xml ) click to toggle source

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 }

@param [String] xml A string representation of valid XML.

@return [Hash] A hash created by parsing xml

    # File lib/extlib/hash.rb, line 81
81:     def from_xml( xml )
82:       ToHashParser.from_xml(xml)
83:     end

Public Instance Methods

add_html_class!(html_class) click to toggle source

@param html_class<#>

  The HTML class to add to the :class key. The html_class will be
  concatenated to any existing classes.

@example hash[:class] #=> nil @example hash.add_html_class!(:selected) @example hash[:class] #=> “selected” @example hash.add_html_class!(“class1 class2”) @example hash[:class] #=> “selected class1 class2“

     # File lib/extlib/hash.rb, line 209
209:   def add_html_class!(html_class)
210:     if self[:class]
211:       self[:class] = "#{self[:class]} #{html_class}"
212:     else
213:       self[:class] = html_class.to_s
214:     end
215:   end
environmentize_keys!() click to toggle source

Destructively and non-recursively convert each key to an uppercase string, deleting nil values along the way.

@return [Hash] The newly environmentized hash.

@example

  { :name => "Bob", :contact => { :email => "bob@bob.com" } }.environmentize_keys!
    #=> { "NAME" => "Bob", "CONTACT" => { :email => "bob@bob.com" } }
     # File lib/extlib/hash.rb, line 250
250:   def environmentize_keys!
251:     keys.each do |key|
252:       val = delete(key)
253:       next if val.nil?
254:       self[key.to_s.upcase] = val
255:     end
256:     self
257:   end
except(*rejected) click to toggle source

Create a hash with all key/value pairs in receiver except rejected

   { :one => 1, :two => 2, :three => 3 }.except(:one)
    #=> { :two => 2, :three => 3 }

@param [Array[String, Symbol]] *rejected The hash keys to exclude.

@return [Hash] A new hash without the selected keys.

@api public

     # File lib/extlib/hash.rb, line 181
181:   def except(*rejected)
182:     hash = self.dup
183:     rejected.each {|k| hash.delete(k) }
184:     hash
185:   end
normalize_param(key, value) click to toggle source

Convert a key, value pair into a URL query param string

  normalize_param(:name, "Bob")   #=> "name=Bob&"

@param [Object] key The key for the param. @param [Object] value The value for the param.

@return [String] This key value pair as a param

@api public

     # File lib/extlib/hash.rb, line 129
129:   def normalize_param(key, value)
130:     param = ''
131:     stack = []
132: 
133:     if value.is_a?(Array)
134:       param << value.map { |element| normalize_param("#{key}[]", element) }.join
135:     elsif value.is_a?(Hash)
136:       stack << [key,value]
137:     else
138:       param << "#{key}=#{value}&"
139:     end
140: 
141:     stack.each do |parent, hash|
142:       hash.each do |k, v|
143:         if v.is_a?(Hash)
144:           stack << ["#{parent}[#{k}]", v]
145:         else
146:           param << normalize_param("#{parent}[#{k}]", v)
147:         end
148:       end
149:     end
150: 
151:     param
152:   end
only(*allowed) click to toggle source

Create a hash with only key/value pairs in receiver and allowed

  { :one => 1, :two => 2, :three => 3 }.only(:one)    #=> { :one => 1 }

@param [Array[String, Symbol]] *allowed The hash keys to include.

@return [Hash] A new hash with only the selected keys.

@api public

     # File lib/extlib/hash.rb, line 164
164:   def only(*allowed)
165:     hash = {}
166:     allowed.each {|k| hash[k] = self[k] if self.has_key?(k) }
167:     hash
168:   end
protect_keys!() click to toggle source

Converts all keys into string values. This is used during reloading to prevent problems when classes are no longer declared.

@return [Array] An array of they hash’s keys

@example

  hash = { One => 1, Two => 2 }.proctect_keys!
  hash # => { "One" => 1, "Two" => 2 }
     # File lib/extlib/hash.rb, line 225
225:   def protect_keys!
226:     keys.each {|key| self[key.to_s] = delete(key) }
227:   end
to_html_attributes() click to toggle source
Alias for: to_xml_attributes
to_mash() click to toggle source

Convert to Mash. This class has semantics of ActiveSupport’s HashWithIndifferentAccess and we only have it so that people can write params[:key] instead of params[‘key’].

@return [Mash] This hash as a Mash for string or symbol key access.

    # File lib/extlib/hash.rb, line 91
91:   def to_mash
92:     hash = Mash.new(self)
93:     hash.default = default
94:     hash
95:   end
to_params() click to toggle source

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."

@return [String] This hash as a query string

@api public

     # File lib/extlib/hash.rb, line 112
112:   def to_params
113:     params = self.map { |k,v| normalize_param(k,v) }.join
114:     params.chop! # trailing &
115:     params
116:   end
to_xml_attributes() click to toggle source

@return [String] The hash as attributes for an XML tag.

@example

  { :one => 1, "two"=>"TWO" }.to_xml_attributes
    #=> 'one="1" two="TWO"'
     # File lib/extlib/hash.rb, line 192
192:   def to_xml_attributes
193:     map do |k,v|
194:       %{#{k.to_s.snake_case.sub(/^(.{1,1})/) { |m| m.downcase }}="#{v}"}
195:     end.join(' ')
196:   end
Also aliased as: to_html_attributes
unprotect_keys!() click to toggle source

Attempts to convert all string keys into Class keys. We run this after reloading to convert protected hashes back into usable hashes.

@example

  # Provided that classes One and Two are declared in this scope:
  hash = { "One" => 1, "Two" => 2 }.unproctect_keys!
  hash # => { One => 1, Two => 2 }
     # File lib/extlib/hash.rb, line 236
236:   def unprotect_keys!
237:     keys.each do |key|
238:       (self[Object.full_const_get(key)] = delete(key)) rescue nil
239:     end
240:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.