wycats / merb-core

Merb Core: All you need. None you don't.

This URL has Read+Write access

merb-core / lib / merb-core / core_ext / mash.rb
100644 154 lines (137 sloc) 3.961 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# This class has dubious semantics and we only have it so that people can write
# params[:key] instead of params['key'].
class Mash < Hash
 
  # ==== 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.
  def initialize(constructor = {})
    if constructor.is_a?(Hash)
      super()
      update(constructor)
    else
      super(constructor)
    end
  end
 
  # ==== 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.
  def default(key = nil)
    if key.is_a?(Symbol) && include?(key = key.to_s)
      self[key]
    else
      super
    end
  end
 
  alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
  alias_method :regular_update, :update unless method_defined?(:regular_update)
 
  # ==== 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.
  def []=(key, value)
    regular_writer(convert_key(key), convert_value(value))
  end
 
  # ==== 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.
  def update(other_hash)
    other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
    self
  end
 
  alias_method :merge!, :update
 
  # ==== 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.
  def key?(key)
    super(convert_key(key))
  end
 
  # def include? def has_key? def member?
  alias_method :include?, :key?
  alias_method :has_key?, :key?
  alias_method :member?, :key?
 
  # ==== Parameters
  # key<Object>:: The key to fetch. This will be run through convert_key.
  # extras:: Default value.
  #
  # ==== Returns
  # Object:: The value at key or the default value.
  def fetch(key, *extras)
    super(convert_key(key), *extras)
  end
 
  # ==== Parameters
  # indices<Array>::
  # The keys to retrieve values for. These will be run through +convert_key+.
  def values_at(*indices)
    indices.collect {|key| self[convert_key(key)]}
  end
 
  # ==== Returns
  # Mash:: A duplicate of this mash.
  def dup
    Mash.new(self)
  end
 
  # ==== Parameters
  # hash<Hash>:: The hash to merge with the mash.
  #
  # ==== Returns
  # Mash:: A new mash with the hash values merged in.
  def merge(hash)
    self.dup.update(hash)
  end
 
  # ==== Parameters
  # key<Object>::
  # The key to delete from the mash. This will be run through convert_key.
  def delete(key)
    super(convert_key(key))
  end
 
  # Used to provide the same interface as Hash.
  #
  # ==== Returns
  # Mash:: This mash unchanged.
  def stringify_keys!; self end
 
  # ==== Returns
  # Hash:: The mash as a Hash with string keys.
  def to_hash
    Hash.new(default).merge(self)
  end
 
  protected
  # ==== Parameters
  # key<Object>:: The key to convert.
  #
  # ==== Returns
  # Object::
  # The converted key. If the key was a symbol, it will be converted to a
  # string.
  def convert_key(key)
    key.kind_of?(Symbol) ? key.to_s : key
  end
 
  # ==== Parameters
  # value<Object>:: The value to convert.
  #
  # ==== Returns
  # Object::
  # The converted value. A Hash or an Array of hashes, will be converted to
  # their Mash equivalents.
  def convert_value(value)
    case value
    when Hash
      value.to_mash
    when Array
      value.collect { |e| convert_value(e) }
    else
      value
    end
  end
end