Class: Mash

Inherits:
Hash
  • Object
show all
Defined in:
lib/ohai/mash.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


59
60
61
62
63
64
65
66
# File 'lib/ohai/mash.rb', line 59

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


177
178
179
180
181
# File 'lib/ohai/mash.rb', line 177

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

90
91
92
# File 'lib/ohai/mash.rb', line 90

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

#default(key = nil) ⇒ Object


73
74
75
76
77
78
79
# File 'lib/ohai/mash.rb', line 73

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

#delete(key) ⇒ Object


143
144
145
# File 'lib/ohai/mash.rb', line 143

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 }

154
155
156
# File 'lib/ohai/mash.rb', line 154

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

#fetch(key, *extras) ⇒ Object


122
123
124
# File 'lib/ohai/mash.rb', line 122

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

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


109
110
111
# File 'lib/ohai/mash.rb', line 109

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

#merge(hash) ⇒ Mash


137
138
139
# File 'lib/ohai/mash.rb', line 137

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

#regular_updateObject


82
# File 'lib/ohai/mash.rb', line 82

alias_method :regular_update, :update

#regular_writerObject


81
# File 'lib/ohai/mash.rb', line 81

alias_method :regular_writer, :[]=

#stringify_keys!Mash

Used to provide the same interface as Hash.


161
# File 'lib/ohai/mash.rb', line 161

def stringify_keys!; self end

#symbolize_keysHash


164
165
166
167
168
# File 'lib/ohai/mash.rb', line 164

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

#to_hashHash


171
172
173
# File 'lib/ohai/mash.rb', line 171

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

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


99
100
101
102
# File 'lib/ohai/mash.rb', line 99

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

#values_at(*indices) ⇒ Array


130
131
132
# File 'lib/ohai/mash.rb', line 130

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