Class: Mash

Inherits:
Hash show all
Defined in:
lib/merb-core/core_ext/mash.rb

Overview

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

Instance Method Summary collapse

Methods inherited from Hash

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

Constructor Details

#initialize(constructor = {}) ⇒ Mash

Parameters

constructor<Object>

The default value for the mash. Defaults to an empty hash.

Alternatives

If constructor is a Hash, a new mash will be created based on the keys of the hash and no default value will be set.



12
13
14
15
16
17
18
19
# File 'lib/merb-core/core_ext/mash.rb', line 12

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

Instance Method Details

#[]=(key, value) ⇒ Object

Parameters

key<Object>

The key to set. This will be run through convert_key.

value<Object>

The value to set the key to. This will be run through convert_value.



42
43
44
# File 'lib/merb-core/core_ext/mash.rb', line 42

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

#default(key = nil) ⇒ Object

Parameters

key<Object>

The default value for the mash. Defaults to nil.

Alternatives

If key is a Symbol and it is a key in the mash, then the default value will be set to the value matching the key.



27
28
29
30
31
32
33
# File 'lib/merb-core/core_ext/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

Parameters

key<Object>

The key to delete from the mash. This will be run through convert_key.



109
110
111
# File 'lib/merb-core/core_ext/mash.rb', line 109

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

#dupObject

Returns

Mash

A duplicate of this mash.



93
94
95
# File 'lib/merb-core/core_ext/mash.rb', line 93

def dup 
  Mash.new(self) 
end

#fetch(key, *extras) ⇒ Object

Parameters

key<Object>

The key to fetch. This willbe run through convert_key.

extras

Default value.

Returns

Object

The value at key or the default value.



80
81
82
# File 'lib/merb-core/core_ext/mash.rb', line 80

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

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

Parameters

key<Object>

The key to check for. This will be run through convert_key.

Returns

Boolean

True if the key exists in the mash.

Returns:

  • (Boolean)


65
66
67
# File 'lib/merb-core/core_ext/mash.rb', line 65

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

#merge(hash) ⇒ Object

Parameters

hash<Hash>

The hash to merge with the mash.

Returns

Mash

A new mash with the hash values merged in.



102
103
104
# File 'lib/merb-core/core_ext/mash.rb', line 102

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

#regular_updateObject



36
# File 'lib/merb-core/core_ext/mash.rb', line 36

alias_method :regular_update, :update

#regular_writerObject



35
# File 'lib/merb-core/core_ext/mash.rb', line 35

alias_method :regular_writer, :[]=

#stringify_keys!Object

Used to provide the same interface as Hash.

Returns

Mash

This mash unchanged.



117
# File 'lib/merb-core/core_ext/mash.rb', line 117

def stringify_keys!; self end

#to_hashObject

Returns

Hash

The mash as a Hash with string keys.



121
122
123
# File 'lib/merb-core/core_ext/mash.rb', line 121

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

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

Parameters

other_hash<Hash>

A hash to update values in the mash with. The keys and the values will be converted to Mash format.

Returns

Mash

The updated mash.



53
54
55
56
# File 'lib/merb-core/core_ext/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) ⇒ Object

Parameters

indices<Array>

The keys to retrieve values for. These will be run through convert_key.



87
88
89
# File 'lib/merb-core/core_ext/mash.rb', line 87

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