diff options
author | Daniel Doubrovkine (dB.) @dblockdotorg <dblock@dblock.org> | 2015-10-23 14:50:44 -0400 |
---|---|---|
committer | Daniel Doubrovkine (dB.) @dblockdotorg <dblock@dblock.org> | 2015-10-23 14:50:44 -0400 |
commit | c652c5b04a51a4de9e62b79fdee0b13687d8f4b4 (patch) | |
tree | 80ef78e8fad669eb652ba3c8026fbdaa3189eae7 /lib | |
parent | f0e5eac1997946975c447b388889dd9675b41ce9 (diff) | |
parent | 61b76fab81f9b876a3bdd7239b8509d2b69dd625 (diff) | |
download | hashie-c652c5b04a51a4de9e62b79fdee0b13687d8f4b4.tar.gz |
Merge pull request #314 from pboling/dictionary
StrictKeyAccess Extension
Diffstat (limited to 'lib')
-rw-r--r-- | lib/hashie.rb | 1 | ||||
-rw-r--r-- | lib/hashie/extensions/strict_key_access.rb | 71 |
2 files changed, 72 insertions, 0 deletions
diff --git a/lib/hashie.rb b/lib/hashie.rb index b0f771c..59a7c25 100644 --- a/lib/hashie.rb +++ b/lib/hashie.rb @@ -26,6 +26,7 @@ module Hashie autoload :PrettyInspect, 'hashie/extensions/pretty_inspect' autoload :KeyConversion, 'hashie/extensions/key_conversion' autoload :MethodAccessWithOverride, 'hashie/extensions/method_access' + autoload :StrictKeyAccess, 'hashie/extensions/strict_key_access' module Parsers autoload :YamlErbParser, 'hashie/extensions/parsers/yaml_erb_parser' diff --git a/lib/hashie/extensions/strict_key_access.rb b/lib/hashie/extensions/strict_key_access.rb new file mode 100644 index 0000000..6972252 --- /dev/null +++ b/lib/hashie/extensions/strict_key_access.rb @@ -0,0 +1,71 @@ +module Hashie + module Extensions + # SRP: This extension will fail an error whenever a key is accessed that does not exist in the hash. + # + # EXAMPLE: + # + # class StrictKeyAccessHash < Hash + # include Hashie::Extensions::StrictKeyAccess + # end + # + # >> hash = StrictKeyAccessHash[foo: "bar"] + # => {:foo=>"bar"} + # >> hash[:foo] + # => "bar" + # >> hash[:cow] + # KeyError: key not found: :cow + # + # NOTE: For googlers coming from Python to Ruby, this extension makes a Hash behave like a "Dictionary". + # + module StrictKeyAccess + class DefaultError < StandardError + def initialize(msg = 'Setting or using a default with Hashie::Extensions::StrictKeyAccess does not make sense', *args) + super + end + end + + # NOTE: This extension would break the default behavior of Hash initialization: + # + # >> a = StrictKeyAccessHash.new(a: :b) + # => {} + # >> a[:a] + # KeyError: key not found: :a + # + # Includes the Hashie::Extensions::MergeInitializer extension to get around that problem. + # Also note that defaults still don't make any sense with a StrictKeyAccess. + def self.included(base) + # Can only include into classes with a hash initializer + base.send(:include, Hashie::Extensions::MergeInitializer) + end + + def [](key) + fetch(key) + end + + def default(_ = nil) + fail DefaultError + end + + def default=(_) + fail DefaultError + end + + def default_proc + fail DefaultError + end + + def default_proc=(_) + fail DefaultError + end + + def key(value) + result = super + if result.nil? && (!key?(result) || self[result] != value) + fail KeyError, "key not found with value of #{value.inspect}" + else + result + end + end + end + end +end |