Class: Mash

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

Overview

This class has dubious semantics and we only have it so that people can write params[:key] instead of params['key'].

Instance Method Summary collapse

Methods inherited from Hash

#extractable_options?, #to_mash, zip

Methods included from Gorillib::Hashlike::Serialization

#to_wire

Methods included from Gorillib::Hashlike::ReverseMerge

#reverse_merge, #reverse_merge!

Methods included from Gorillib::Hashlike::DeepCompact

#deep_compact!

Methods included from Gorillib::Hashlike::DeepMerge

#deep_merge, #deep_merge!

Methods included from Gorillib::Hashlike::DeepDup

#deep_dup

Methods included from Gorillib::Hashlike::Compact

#compact, #compact!, #compact_blank, #compact_blank!

Methods included from Gorillib::Hashlike::Slice

#except, #except!, #extract!, #only, #only!, #slice, #slice!

Methods included from Gorillib::Hashlike::Keys

#assert_valid_keys, #stringify_keys, #symbolize_keys!

Constructor Details

#Mash

Returns a new instance of Mash.

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.

Parameters:

  • constructor (Object) (defaults to: {})

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



25
26
27
28
29
30
31
32
# File 'lib/gorillib/hash/mash.rb', line 25

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.

  • value (Object)

    The value to set the key to.

See Also:

  • #convert_key
  • #convert_value


56
57
58
# File 'lib/gorillib/hash/mash.rb', line 56

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

#Object

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.

Parameters:

  • key (Object) (defaults to: nil)

    The default value for the mash. Defaults to nil.



39
40
41
42
43
44
45
# File 'lib/gorillib/hash/mash.rb', line 39

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.\



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

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

#fetch(key, *args) { ... } ⇒ Object

Returns The value at key or the default value.

Parameters:

  • key (Object)

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

  • args (Array)

    Default value.

Yields:

  • Default block

Returns:

  • (Object)

    The value at key or the default value.



88
89
90
# File 'lib/gorillib/hash/mash.rb', line 88

def fetch(key, *args, &block)
  super(convert_key(key), *args, &block)
end

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

Returns True if the key exists in the mash.

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.



75
76
77
# File 'lib/gorillib/hash/mash.rb', line 75

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

#merge(hash) ⇒ Mash

Returns A new mash with the hash values merged in.

Parameters:

  • hash (Hash)

    The hash to merge with the mash.

Returns:

  • (Mash)

    A new mash with the hash values merged in.



103
104
105
# File 'lib/gorillib/hash/mash.rb', line 103

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

#regular_updateObject



48
# File 'lib/gorillib/hash/mash.rb', line 48

alias_method :regular_update, :update

#regular_writerObject



47
# File 'lib/gorillib/hash/mash.rb', line 47

alias_method :regular_writer, :[]=

#stringify_keys!Mash

Used to provide the same interface as Hash.

Returns:

  • (Mash)

    This mash unchanged.



116
# File 'lib/gorillib/hash/mash.rb', line 116

def stringify_keys!; self end

#symbolize_keysHash

Returns The mash as a Hash with symbolized keys.

Returns:

  • (Hash)

    The mash as a Hash with symbolized keys.



119
120
121
122
123
# File 'lib/gorillib/hash/mash.rb', line 119

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

#to_hashHash

Returns The mash as a Hash with string keys.

Returns:

  • (Hash)

    The mash as a Hash with string keys.



126
127
128
# File 'lib/gorillib/hash/mash.rb', line 126

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

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

Returns The updated mash.

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.



65
66
67
68
# File 'lib/gorillib/hash/mash.rb', line 65

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

#values_at(*indices) ⇒ Array

Returns The values at each of the provided keys.

Parameters:

  • indices (Array)

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

Returns:

  • (Array)

    The values at each of the provided keys



96
97
98
# File 'lib/gorillib/hash/mash.rb', line 96

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