Class: Mash

Inherits:
Hash show all
Defined in:
lib/extlib/mash.rb

Overview

This class has dubious semantics and we only have it so that people can write params instead of params.

Constant Summary

Constants inherited from Hash

Hash::URI_ENCODE_PATTERN

Instance Method Summary collapse

Methods inherited from Hash

#add_html_class!, #environmentize_keys!, from_xml, #normalize_param, #only, #protect_keys!, #to_mash, #to_params, #to_xml_attributes, #unprotect_keys!

Constructor Details

#initialize(constructor = {}) ⇒ Mash


13
14
15
16
17
18
19
20
# File 'lib/extlib/mash.rb', line 13

def initialize(constructor = {})
  if constructor.is_a?(Hash)
    super()
    update(constructor)
  else
    super(constructor)
  end
end

Instance Method Details

#[]=(key, value) ⇒ Object

See Also:

  • #convert_key
  • #convert_value

44
45
46
# File 'lib/extlib/mash.rb', line 44

def []=(key, value)
  regular_writer(convert_key(key), convert_value(value))
end

#default(key = nil) ⇒ Object


27
28
29
30
31
32
33
# File 'lib/extlib/mash.rb', line 27

def default(key = nil)
  if key.is_a?(Symbol) && include?(key = key.to_s)
    self[key]
  else
    super
  end
end

#delete(key) ⇒ Object


97
98
99
# File 'lib/extlib/mash.rb', line 97

def delete(key)
  super(convert_key(key))
end

#except(*keys) ⇒ Mash

Returns A new mash without the selected keys.

Examples:

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

108
109
110
# File 'lib/extlib/mash.rb', line 108

def except(*keys)
  super(*keys.map {|k| convert_key(k)})
end

#fetch(key, *extras) ⇒ Object


76
77
78
# File 'lib/extlib/mash.rb', line 76

def fetch(key, *extras)
  super(convert_key(key), *extras)
end

#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?


63
64
65
# File 'lib/extlib/mash.rb', line 63

def key?(key)
  super(convert_key(key))
end

#merge(hash) ⇒ Mash


91
92
93
# File 'lib/extlib/mash.rb', line 91

def merge(hash)
  self.dup.update(hash)
end

#regular_updateObject


36
# File 'lib/extlib/mash.rb', line 36

alias_method :regular_update, :update

#regular_writerObject


35
# File 'lib/extlib/mash.rb', line 35

alias_method :regular_writer, :[]=

#stringify_keys!Mash

Used to provide the same interface as Hash.


115
# File 'lib/extlib/mash.rb', line 115

def stringify_keys!; self end

#symbolize_keysHash


118
119
120
121
122
# File 'lib/extlib/mash.rb', line 118

def symbolize_keys
  h = Hash.new(default)
  each { |key, val| h[key.to_sym] = val }
  h
end

#to_hashHash


125
126
127
# File 'lib/extlib/mash.rb', line 125

def to_hash
  Hash.new(default).merge(self)
end

#update(other_hash) ⇒ Mash Also known as: merge!


53
54
55
56
# File 'lib/extlib/mash.rb', line 53

def update(other_hash)
  other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
  self
end

#values_at(*indices) ⇒ Array


84
85
86
# File 'lib/extlib/mash.rb', line 84

def values_at(*indices)
  indices.collect {|key| self[convert_key(key)]}
end