Class: Sash

Inherits:
Hash show all
Defined in:
lib/configliere/core_ext/sash.rb

Overview

Hash with indifferent access

Adapted from extlib/lib/mash.rb

Constant Summary

Constants inherited from Hash

Hash::DEEP_MERGER

Instance Method Summary collapse

Methods inherited from Hash

#compact, #compact!, #deep_delete, #deep_get, #deep_merge, #deep_merge!, #deep_set, #to_sash

Constructor Details

#initialize(constructor = {}) ⇒ Sash

Returns a new instance of Sash.

Parameters:

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

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



16
17
18
19
20
21
22
23
# File 'lib/configliere/core_ext/sash.rb', line 16

def initialize(constructor = {})
  if constructor.is_a?(Hash)
    super()
    update(constructor) unless constructor.empty?
  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:

  • Mash#convert_key
  • Mash#convert_value


34
35
36
# File 'lib/configliere/core_ext/sash.rb', line 34

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

#default(key = nil) ⇒ Object

Parameters:

  • key (Object) (defaults to: nil)

    The default value for the mash. Defaults to nil.



91
92
93
94
95
96
97
# File 'lib/configliere/core_ext/sash.rb', line 91

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

#delete(key) ⇒ Object

Parameters:

  • key (Object)

    The key to delete from the mash.\



77
78
79
# File 'lib/configliere/core_ext/sash.rb', line 77

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

#fetch(key, *extras) ⇒ Object

Returns The value at key or the default value.

Parameters:

  • key (Object)

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

  • *extras (Array)

    Default value.

Returns:

  • (Object)

    The value at key or the default value.



56
57
58
# File 'lib/configliere/core_ext/sash.rb', line 56

def fetch(key, *extras)
  super(convert_key(key), *extras)
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.



43
44
45
# File 'lib/configliere/core_ext/sash.rb', line 43

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

#merge(hash, &block) ⇒ 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.



71
72
73
# File 'lib/configliere/core_ext/sash.rb', line 71

def merge(hash, &block)
  self.dup.update(hash, &block)
end

#merge!Object



38
# File 'lib/configliere/core_ext/sash.rb', line 38

alias_method :merge!, :update

#regular_updateObject



26
# File 'lib/configliere/core_ext/sash.rb', line 26

alias_method :regular_update, :update

#regular_writerObject



25
# File 'lib/configliere/core_ext/sash.rb', line 25

alias_method :regular_writer, :[]=

#stringify_keysHash

Returns The sash as a Hash with stringified keys.

Returns:

  • (Hash)

    The sash as a Hash with stringified keys.



119
120
121
122
123
# File 'lib/configliere/core_ext/sash.rb', line 119

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

#symbolize_keys!Sash

Used to provide the same interface as Hash.

Returns:

  • (Sash)

    This sash unchanged.



116
# File 'lib/configliere/core_ext/sash.rb', line 116

def symbolize_keys!; self end

#to_hashHash

Returns The mash as a Hash with string keys.

Returns:

  • (Hash)

    The mash as a Hash with string keys.



82
83
84
# File 'lib/configliere/core_ext/sash.rb', line 82

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

#update(other_hash, &block) ⇒ Mash

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.



104
105
106
107
108
109
110
111
# File 'lib/configliere/core_ext/sash.rb', line 104

def update(other_hash, &block)
  sash = self.class.new
  other_hash.each_pair do |key, value|
    val = convert_value(value)
    sash[convert_key(key)] = val
  end
  regular_update(sash, &block)
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



64
65
66
# File 'lib/configliere/core_ext/sash.rb', line 64

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