Class: WhinyHash

Inherits:
ActiveSupport::HashWithIndifferentAccess
  • Object
show all
Defined in:
lib/whiny_hash.rb

Overview

We’re extending the already dubious semantics of HashWithIndifferentAccess to raise errors if either an unset key is requested, or if a previously unset key is set by a merge.

This is used to prevent usage of ‘wrong’ options.

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



32
33
34
# File 'lib/whiny_hash.rb', line 32

def [](key)
  key?(key) ? super(key) : raise("Accessing unset key '#{key}'.")
end

#[]=(key, val) ⇒ Object



36
37
38
# File 'lib/whiny_hash.rb', line 36

def []=(key, val)
  key?(key) ? super(key, val) : raise("Trying to set previously unset key '#{key}'.")
end

#merge(other) ⇒ Object



41
42
43
44
45
46
# File 'lib/whiny_hash.rb', line 41

def merge(other)
  other.keys.each do |k| 
    raise "Trying to override unset key '#{k}'." unless key?(k) 
  end
  WhinyHash.new(merge_lidsa(other))
end

#merge_lidsaObject



40
# File 'lib/whiny_hash.rb', line 40

alias_method :merge_lidsa, :merge