diff options
author | Bobby McDonald <BobbyMcWho@users.noreply.github.com> | 2019-10-14 14:14:47 -0400 |
---|---|---|
committer | Daniel Doubrovkine (dB.) @dblockdotorg <dblock@dblock.org> | 2019-10-14 14:14:47 -0400 |
commit | 1a30427c9db1bdf974530aeddf90b305a3a621a5 (patch) | |
tree | 7dde72202b0fbfdd250113aa119239c322bac107 /lib | |
parent | ca3604516b5725b30a290482c219ca55fca5de49 (diff) | |
download | hashie-1a30427c9db1bdf974530aeddf90b305a3a621a5.tar.gz |
Allow mash error silencing (#488)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/hashie.rb | 38 | ||||
-rw-r--r-- | lib/hashie/extensions/key_conflict_warning.rb | 55 | ||||
-rw-r--r-- | lib/hashie/mash.rb | 66 |
3 files changed, 90 insertions, 69 deletions
diff --git a/lib/hashie.rb b/lib/hashie.rb index fd93e05..a3fc11c 100644 --- a/lib/hashie.rb +++ b/lib/hashie.rb @@ -12,26 +12,26 @@ module Hashie autoload :Utils, 'hashie/utils' module Extensions - autoload :Coercion, 'hashie/extensions/coercion' - autoload :DeepMerge, 'hashie/extensions/deep_merge' - autoload :IgnoreUndeclared, 'hashie/extensions/ignore_undeclared' - autoload :IndifferentAccess, 'hashie/extensions/indifferent_access' - autoload :MergeInitializer, 'hashie/extensions/merge_initializer' - autoload :MethodAccess, 'hashie/extensions/method_access' - autoload :MethodQuery, 'hashie/extensions/method_access' - autoload :MethodReader, 'hashie/extensions/method_access' - autoload :MethodWriter, 'hashie/extensions/method_access' - autoload :StringifyKeys, 'hashie/extensions/stringify_keys' - autoload :SymbolizeKeys, 'hashie/extensions/symbolize_keys' - autoload :DeepFetch, 'hashie/extensions/deep_fetch' - autoload :DeepFind, 'hashie/extensions/deep_find' - autoload :DeepLocate, 'hashie/extensions/deep_locate' - autoload :PrettyInspect, 'hashie/extensions/pretty_inspect' - autoload :KeyConversion, 'hashie/extensions/key_conversion' + autoload :Coercion, 'hashie/extensions/coercion' + autoload :DeepMerge, 'hashie/extensions/deep_merge' + autoload :IgnoreUndeclared, 'hashie/extensions/ignore_undeclared' + autoload :IndifferentAccess, 'hashie/extensions/indifferent_access' + autoload :MergeInitializer, 'hashie/extensions/merge_initializer' + autoload :MethodAccess, 'hashie/extensions/method_access' + autoload :MethodQuery, 'hashie/extensions/method_access' + autoload :MethodReader, 'hashie/extensions/method_access' + autoload :MethodWriter, 'hashie/extensions/method_access' + autoload :StringifyKeys, 'hashie/extensions/stringify_keys' + autoload :SymbolizeKeys, 'hashie/extensions/symbolize_keys' + autoload :DeepFetch, 'hashie/extensions/deep_fetch' + autoload :DeepFind, 'hashie/extensions/deep_find' + autoload :DeepLocate, 'hashie/extensions/deep_locate' + 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' - autoload :RubyVersion, 'hashie/extensions/ruby_version' - autoload :RubyVersionCheck, 'hashie/extensions/ruby_version_check' + autoload :StrictKeyAccess, 'hashie/extensions/strict_key_access' + autoload :RubyVersion, 'hashie/extensions/ruby_version' + autoload :RubyVersionCheck, 'hashie/extensions/ruby_version_check' module Parsers autoload :YamlErbParser, 'hashie/extensions/parsers/yaml_erb_parser' diff --git a/lib/hashie/extensions/key_conflict_warning.rb b/lib/hashie/extensions/key_conflict_warning.rb new file mode 100644 index 0000000..7ce56a1 --- /dev/null +++ b/lib/hashie/extensions/key_conflict_warning.rb @@ -0,0 +1,55 @@ +module Hashie + module Extensions + module KeyConflictWarning + class CannotDisableMashWarnings < StandardError + def initialize + super( + 'You cannot disable warnings on the base Mash class. ' \ + 'Please subclass the Mash and disable it in the subclass.' + ) + end + end + + # Disable the logging of warnings based on keys conflicting keys/methods + # + # @api semipublic + # @return [void] + def disable_warnings(*method_keys) + raise CannotDisableMashWarnings if self == Hashie::Mash + if method_keys.any? + disabled_warnings.concat(method_keys).tap(&:flatten!).uniq! + else + disabled_warnings.clear + end + + @disable_warnings = true + end + + # Checks whether this class disables warnings for conflicting keys/methods + # + # @api semipublic + # @return [Boolean] + def disable_warnings?(method_key = nil) + return disabled_warnings.include?(method_key) if disabled_warnings.any? && method_key + @disable_warnings ||= false + end + + # Returns an array of blacklisted methods that this class disables warnings for. + # + # @api semipublic + # @return [Boolean] + def disabled_warnings + @_disabled_warnings ||= [] + end + + # Inheritance hook that sets class configuration when inherited. + # + # @api semipublic + # @return [void] + def inherited(subclass) + super + subclass.disable_warnings(disabled_warnings) if disable_warnings? + end + end + end +end diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb index 888b4f6..77a852f 100644 --- a/lib/hashie/mash.rb +++ b/lib/hashie/mash.rb @@ -2,6 +2,7 @@ require 'hashie/hash' require 'hashie/array' require 'hashie/utils' require 'hashie/logger' +require 'hashie/extensions/key_conflict_warning' module Hashie # Mash allows you to create pseudo-objects that have method-like @@ -62,59 +63,10 @@ module Hashie class Mash < Hash include Hashie::Extensions::PrettyInspect include Hashie::Extensions::RubyVersionCheck + extend Hashie::Extensions::KeyConflictWarning ALLOWED_SUFFIXES = %w[? ! = _].freeze - class CannotDisableMashWarnings < StandardError - def initialize - super( - 'You cannot disable warnings on the base Mash class. ' \ - 'Please subclass the Mash and disable it in the subclass.' - ) - end - end - - # Disable the logging of warnings based on keys conflicting keys/methods - # - # @api semipublic - # @return [void] - def self.disable_warnings(*method_keys) - raise CannotDisableMashWarnings if self == Hashie::Mash - if method_keys.any? - disable_warnings_blacklist.concat(method_keys).tap(&:flatten!).uniq! - else - disable_warnings_blacklist.clear - end - - @disable_warnings = true - end - - # Checks whether this class disables warnings for conflicting keys/methods - # - # @api semipublic - # @return [Boolean] - def self.disable_warnings?(method_key = nil) - return disable_warnings_blacklist.include?(method_key) if disable_warnings_blacklist.any? && method_key - @disable_warnings ||= false - end - - # Returns an array of blacklisted methods that this class disables warnings for. - # - # @api semipublic - # @return [Boolean] - def self.disable_warnings_blacklist - @_disable_warnings_blacklist ||= [] - end - - # Inheritance hook that sets class configuration when inherited. - # - # @api semipublic - # @return [void] - def self.inherited(subclass) - super - subclass.disable_warnings(disable_warnings_blacklist) if disable_warnings? - end - def self.load(path, options = {}) @_mashes ||= new @@ -149,6 +101,20 @@ module Hashie default ? super(default) : super(&blk) end + # Creates a new anonymous subclass with key conflict + # warnings disabled. You may pass an array of method + # symbols to restrict the warnings blacklist to. + # Hashie::Mash.quiet.new(hash) all warnings disabled. + # Hashie::Mash.quiet(:zip).new(hash) only zip warning + # is disabled. + def self.quiet(*method_keys) + (@memoized_classes ||= {})[method_keys] || + Class.new(self).tap do |k| + k.send(:disable_warnings, *method_keys) + @memoized_classes[method_keys] = k + end + end + class << self; alias [] new; end alias regular_reader [] |