Class: Settingable::Hash

Inherits:
Hash
  • Object
show all
Defined in:
lib/settingable/hash.rb

Overview

A hash that raises an error on an access that fails.

Instance Method Summary collapse

Constructor Details

#initialize(body = {}) ⇒ Hash

Initialize the hash. If a value is passed, the values are set on this hash. Does not modify the value.

Parameters:

  • body (Hash) (defaults to: {})

See Also:

  • #convert


11
12
13
14
15
# File 'lib/settingable/hash.rb', line 11

def initialize(body = {})
  body.each do |key, value|
    self[key] = value
  end
end

Instance Method Details

#[](key) ⇒ Object

Accesses the hash. It raises an error if it can't find the key. It attempts to convert the key into a Symbol.

Parameters:

  • key (String, Symbol, Object)

    The key.

Returns:

  • (Object)

    The value.

Raises:

  • KeyError If the key isn't mapped to a value.



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

def [](key)
  case key
  when String then fetch(key.intern)
  when Symbol then fetch(key)
  else             fetch(key.to_s.intern)
  end
end

#[]=(key, value) ⇒ void

This method returns an undefined value.

Used to set a key to a value. The key is first converted to a Symbol, and the value is coerced into a Settingable::Hash if it is a ::Hash.

Parameters:

  • key (String, Symbol, Object)
  • value (Hash, Object)

See Also:

  • #convert


56
57
58
59
60
61
62
# File 'lib/settingable/hash.rb', line 56

def []=(key, value)
  case key
  when String then super(key.intern, convert(value))
  when Symbol then super(key, convert(value))
  else             super(key.to_s.intern, convert(value))
  end
end

#fetch(key, value = BLANK_OBJECT) {|key| ... } ⇒ Object

Performs a fetch. If the key is in the hash, it returns its value. If the key is not in the hash, and a value was passed, the value is returned. If the key is not in the hash, and a block was passed, it yields. Otherwise, it raises KeyError.

Parameters:

  • key (Object)

    The key.

  • value (Object) (defaults to: BLANK_OBJECT)

    The default object.

Yields:

  • (key)

Returns:

  • (Object)

Raises:

  • KeyError If the key isn't in the hash and no default was given.



82
83
84
85
86
87
88
89
90
# File 'lib/settingable/hash.rb', line 82

def fetch(key, value = BLANK_OBJECT)
  case
  when old_key?(key)         then old_access(key)
  when block_given?          then yield(key)
  when value != BLANK_OBJECT then value
  else
    fail KeyError, "Key not found: #{key.inspect}"
  end
end

#key?(key) ⇒ Boolean

Checks to determine if the given key is set in this hash. It first converts the key to a Symbol, then performs the check.

Parameters:

  • key (String, Symbol, Object)

Returns:

  • (Boolean)


24
25
26
27
28
29
30
# File 'lib/settingable/hash.rb', line 24

def key?(key)
  case key
  when String then super(key.intern)
  when Symbol then super(key)
  else             super(key.to_s.intern)
  end
end

#old_accessObject



32
# File 'lib/settingable/hash.rb', line 32

alias_method :old_access, :[]

#old_fetchObject



70
# File 'lib/settingable/hash.rb', line 70

alias_method :old_fetch, :fetch

#old_key?Object



17
# File 'lib/settingable/hash.rb', line 17

alias_method :old_key?, :key?

#old_setObject



47
# File 'lib/settingable/hash.rb', line 47

alias_method :old_set, :[]=

#to_h::Hash

Converts this hash into a normal hash. Since we recursively converted every hash value into a Settingable::Hash, we have to undo that for the new hash.

Returns:

  • (::Hash)


97
98
99
100
101
102
103
104
# File 'lib/settingable/hash.rb', line 97

def to_h
  out = {}
  each do |key, value|
    out[key] = convert(value, true)
  end

  out
end