Class Hash
In: lib/merb-core/core_ext/hash.rb
Parent: Object

Methods

Public Class methods

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 }

Public Instance methods

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"

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 => "bob@bob.com" } }.environmentize_keys!
    #=> { "NAME" => "Bob", "CONTACT" => { :email => "bob@bob.com" } }

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 }

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 }

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 }
to_html_attributes()

Alias for to_xml_attributes

Returns

Mash:This hash as a Mash for string or symbol key access.

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

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

Returns

String:The hash as attributes for an XML tag.

Examples

  { :one => 1, "two"=>"TWO" }.to_xml_attributes
    #=> 'one="1" two="TWO"'

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 }

[Validate]