Class: AttributeStruct::Mash

Inherits:
Hash
  • Object
show all
Defined in:
lib/attribute_struct/attribute_hash.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constructor = {}) ⇒ Mash



63
64
65
66
67
68
69
70
# File 'lib/attribute_struct/attribute_hash.rb', line 63

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

Class Method Details

.from_hash(hash) ⇒ Mash

The input Hash's default value is maintained



202
203
204
205
206
# File 'lib/attribute_struct/attribute_hash.rb', line 202

def self.from_hash(hash)
  mash = Mash.new(hash)
  mash.default = hash.default
  mash
end

Instance Method Details

#[]=(key, value) ⇒ Object

See Also:

  • #convert_key
  • #convert_value


112
113
114
# File 'lib/attribute_struct/attribute_hash.rb', line 112

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

#deep_merge(hash) ⇒ AttributeStruct::Mash

Perform deep merge



245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/attribute_struct/attribute_hash.rb', line 245

def deep_merge(hash)
  unless (hash.is_a?(Hash))
    raise ArgumentError.new "Expecting `Hash` type. Received: `#{hash.class}`"
  end
  new_self = self.dup
  hash.each do |k, v|
    if (new_self[k].is_a?(Hash) && v.is_a?(Hash))
      new_self[k] = new_self[k].deep_merge(v)
    else
      new_self[k] = v
    end
  end
  new_self
end

#deep_merge!(hash) ⇒ self

Perform deep merge and replace contents of self



263
264
265
266
# File 'lib/attribute_struct/attribute_hash.rb', line 263

def deep_merge!(hash)
  self.replace(self.deep_merge(hash))
  self
end

#default(key = nil) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/attribute_struct/attribute_hash.rb', line 91

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

#delete(key) ⇒ Object



165
166
167
# File 'lib/attribute_struct/attribute_hash.rb', line 165

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 }


176
177
178
# File 'lib/attribute_struct/attribute_hash.rb', line 176

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

#fetch(key, *extras) ⇒ Object



144
145
146
# File 'lib/attribute_struct/attribute_hash.rb', line 144

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

#initialize_copy(orig) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/attribute_struct/attribute_hash.rb', line 75

def initialize_copy(orig)
  super
  # Handle nested values
  each do |k, v|
    if v.kind_of?(Mash) || v.is_a?(Array)
      self[k] = v.dup
    end
  end
  self
end

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



131
132
133
# File 'lib/attribute_struct/attribute_hash.rb', line 131

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

#merge(hash) ⇒ Mash



159
160
161
# File 'lib/attribute_struct/attribute_hash.rb', line 159

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

#regular_updateObject



103
# File 'lib/attribute_struct/attribute_hash.rb', line 103

alias_method :regular_update, :update

#regular_writerObject



100
# File 'lib/attribute_struct/attribute_hash.rb', line 100

alias_method :regular_writer, :[]=

#stringify_keys!Mash

Used to provide the same interface as Hash.



183
# File 'lib/attribute_struct/attribute_hash.rb', line 183

def stringify_keys!; self end

#symbolize_keysHash



186
187
188
189
190
191
192
193
# File 'lib/attribute_struct/attribute_hash.rb', line 186

def symbolize_keys
  h = Hash.new(default)
  each do |key, val|
    key = key.to_sym if key.is_a?(String) || key.is_a?(Symbol)
    h[key] = val
  end
  h
end

#to_hashHash



196
197
198
# File 'lib/attribute_struct/attribute_hash.rb', line 196

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

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



121
122
123
124
# File 'lib/attribute_struct/attribute_hash.rb', line 121

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

#values_at(*indices) ⇒ Array



152
153
154
# File 'lib/attribute_struct/attribute_hash.rb', line 152

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