diff options
| author | Michael Herold <michael.j.herold@gmail.com> | 2018-02-03 10:23:14 -0600 |
|---|---|---|
| committer | Michael Herold <michael.j.herold@gmail.com> | 2018-06-17 11:04:56 -0500 |
| commit | 5a6ffc7e2df076c266c322ae9272b098b3ab40ed (patch) | |
| tree | 578b274928ce6ac710922c85d050771f52753736 /lib/hashie/extensions | |
| parent | 8fc10095fc409f4db495713c92c0cf8e31d3bfc1 (diff) | |
| download | hashie-5a6ffc7e2df076c266c322ae9272b098b3ab40ed.tar.gz | |
Update Rubocop and address the addressable todos
This is a big step forward in our Rubocop setup. I addressed all of the todos
from the current version of Rubocop that made sense to. The only things that
remain are metrics and one cop that relies on the line length metric to work.
I made some judgment calls on disabling a few cops:
1. The `Layout/IndentHeredoc` cop wants you to either use the squiggly heredoc
from Ruby 2.3 or introduce a library. Since we are a low-level library that
is used as a transitive dependency, we cannot introduce another library as a
dependence, so that option is out. Also, we support Rubies back to 2.0
currently, so using the squiggly heredoc isn't an option. Once we remove
support for Rubies older than 2.3, we can switch to the squiggly heredoc cop.
2. The `Naming/FileName` cop was reporting false positives for a few files in
the repository, so I disabled it on those files.
3. The `Style/DoubleNegation` cop reports lints on a few cases where we use
double negation. Given the very generic nature of Hashie, the double-negation
is the easiest, clearest way to express that we want an item to be a Boolean.
I disabled the cop because we exist in the gray area where this makes sense.
Diffstat (limited to 'lib/hashie/extensions')
| -rw-r--r-- | lib/hashie/extensions/coercion.rb | 10 | ||||
| -rw-r--r-- | lib/hashie/extensions/dash/property_translation.rb | 20 | ||||
| -rw-r--r-- | lib/hashie/extensions/deep_fetch.rb | 2 | ||||
| -rw-r--r-- | lib/hashie/extensions/deep_find.rb | 4 | ||||
| -rw-r--r-- | lib/hashie/extensions/deep_locate.rb | 9 | ||||
| -rw-r--r-- | lib/hashie/extensions/deep_merge.rb | 17 | ||||
| -rw-r--r-- | lib/hashie/extensions/indifferent_access.rb | 6 | ||||
| -rw-r--r-- | lib/hashie/extensions/mash/keep_original_keys.rb | 8 | ||||
| -rw-r--r-- | lib/hashie/extensions/mash/safe_assignment.rb | 2 | ||||
| -rw-r--r-- | lib/hashie/extensions/mash/symbolize_keys.rb | 2 | ||||
| -rw-r--r-- | lib/hashie/extensions/method_access.rb | 10 | ||||
| -rw-r--r-- | lib/hashie/extensions/parsers/yaml_erb_parser.rb | 2 | ||||
| -rw-r--r-- | lib/hashie/extensions/strict_key_access.rb | 17 | ||||
| -rw-r--r-- | lib/hashie/extensions/stringify_keys.rb | 2 | ||||
| -rw-r--r-- | lib/hashie/extensions/symbolize_keys.rb | 2 |
15 files changed, 52 insertions, 61 deletions
diff --git a/lib/hashie/extensions/coercion.rb b/lib/hashie/extensions/coercion.rb index eb0a6dd..704343e 100644 --- a/lib/hashie/extensions/coercion.rb +++ b/lib/hashie/extensions/coercion.rb @@ -10,14 +10,14 @@ module Hashie Rational => :to_r, String => :to_s, Symbol => :to_sym - } + }.freeze ABSTRACT_CORE_TYPES = if RubyVersion.new(RUBY_VERSION) >= RubyVersion.new('2.4.0') { Numeric => [Integer, Float, Complex, Rational] } else { - Integer => [Fixnum, Bignum], - Numeric => [Fixnum, Bignum, Float, Complex, Rational] + Integer => [Fixnum, Bignum], # rubocop:disable Lint/UnifiedInteger + Numeric => [Fixnum, Bignum, Float, Complex, Rational] # rubocop:disable Lint/UnifiedInteger } end @@ -78,7 +78,7 @@ module Hashie attrs.each { |key| key_coercions[key] = into } end - alias_method :coerce_keys, :coerce_key + alias coerce_keys coerce_key # Returns a hash of any existing key coercions. def key_coercions @@ -180,7 +180,7 @@ module Hashie type.new(value) end else - fail TypeError, "#{type} is not a coercable type" + raise TypeError, "#{type} is not a coercable type" end end diff --git a/lib/hashie/extensions/dash/property_translation.rb b/lib/hashie/extensions/dash/property_translation.rb index be858b7..2d28080 100644 --- a/lib/hashie/extensions/dash/property_translation.rb +++ b/lib/hashie/extensions/dash/property_translation.rb @@ -74,7 +74,7 @@ module Hashie if options[:from] if property_name == options[:from] - fail ArgumentError, "Property name (#{property_name}) and :from option must not be the same" + raise ArgumentError, "Property name (#{property_name}) and :from option must not be the same" end translations_hash[options[:from]] ||= {} @@ -85,10 +85,8 @@ module Hashie self[name] = with.respond_to?(:call) ? with.call(val) : val end end - else - if options[:transform_with].respond_to? :call - transforms[property_name] = options[:transform_with] - end + elsif options[:transform_with].respond_to? :call + transforms[property_name] = options[:transform_with] end end @@ -107,11 +105,11 @@ module Hashie def translations @translations ||= {}.tap do |h| translations_hash.each do |(property_name, property_translations)| - if property_translations.size > 1 - h[property_name] = property_translations.keys - else - h[property_name] = property_translations.keys.first - end + h[property_name] = if property_translations.size > 1 + property_translations.keys + else + property_translations.keys.first + end end end end @@ -119,7 +117,7 @@ module Hashie def inverse_translations @inverse_translations ||= {}.tap do |h| translations_hash.each do |(property_name, property_translations)| - property_translations.keys.each do |k| + property_translations.each_key do |k| h[k] = property_name end end diff --git a/lib/hashie/extensions/deep_fetch.rb b/lib/hashie/extensions/deep_fetch.rb index 9149f0d..6ed34f3 100644 --- a/lib/hashie/extensions/deep_fetch.rb +++ b/lib/hashie/extensions/deep_fetch.rb @@ -19,7 +19,7 @@ module Hashie arg = Integer(arg) if obj.is_a? Array obj.fetch(arg) rescue ArgumentError, IndexError, NoMethodError => e - break block.call(arg) if block + break yield(arg) if block raise UndefinedPathError, "Could not fetch path (#{args.join(' > ')}) at #{arg}", e.backtrace end end diff --git a/lib/hashie/extensions/deep_find.rb b/lib/hashie/extensions/deep_find.rb index 616e659..c4a7646 100644 --- a/lib/hashie/extensions/deep_find.rb +++ b/lib/hashie/extensions/deep_find.rb @@ -19,7 +19,7 @@ module Hashie _deep_find(key) end - alias_method :deep_detect, :deep_find + alias deep_detect deep_find # Performs a depth-first search on deeply nested data structures for # a key and returns all occurrences of the key. @@ -40,7 +40,7 @@ module Hashie matches.empty? ? nil : matches end - alias_method :deep_select, :deep_find_all + alias deep_select deep_find_all private diff --git a/lib/hashie/extensions/deep_locate.rb b/lib/hashie/extensions/deep_locate.rb index 930513a..3577e93 100644 --- a/lib/hashie/extensions/deep_locate.rb +++ b/lib/hashie/extensions/deep_locate.rb @@ -61,8 +61,6 @@ module Hashie Hashie::Extensions::DeepLocate.deep_locate(comparator, self) end - private - def self._construct_key_comparator(search_key, object) search_key = search_key.to_s if defined?(::ActiveSupport::HashWithIndifferentAccess) && object.is_a?(::ActiveSupport::HashWithIndifferentAccess) search_key = search_key.to_s if object.respond_to?(:indifferent_access?) && object.indifferent_access? @@ -71,12 +69,11 @@ module Hashie ->(key, _, _) { key == non_callable_object } end.call(search_key) end + private_class_method :_construct_key_comparator def self._deep_locate(comparator, object, result = []) if object.is_a?(::Enumerable) - if object.any? { |value| _match_comparator?(value, comparator, object) } - result.push object - end + result.push object if object.any? { |value| _match_comparator?(value, comparator, object) } (object.respond_to?(:values) ? object.values : object.entries).each do |value| _deep_locate(comparator, value, result) end @@ -84,6 +81,7 @@ module Hashie result end + private_class_method :_deep_locate def self._match_comparator?(value, comparator, object) if object.is_a?(::Hash) @@ -94,6 +92,7 @@ module Hashie comparator.call(key, value, object) end + private_class_method :_match_comparator? end end end diff --git a/lib/hashie/extensions/deep_merge.rb b/lib/hashie/extensions/deep_merge.rb index 5d8fe34..5363904 100644 --- a/lib/hashie/extensions/deep_merge.rb +++ b/lib/hashie/extensions/deep_merge.rb @@ -20,15 +20,14 @@ module Hashie def _recursive_merge(hash, other_hash, &block) other_hash.each do |k, v| - hash[k] = if hash.key?(k) && hash[k].is_a?(::Hash) && v.is_a?(::Hash) - _recursive_merge(hash[k], v, &block) - else - if hash.key?(k) && block_given? - block.call(k, hash[k], v) - else - v.respond_to?(:deep_dup) ? v.deep_dup : v - end - end + hash[k] = + if hash.key?(k) && hash[k].is_a?(::Hash) && v.is_a?(::Hash) + _recursive_merge(hash[k], v, &block) + elsif hash.key?(k) && block_given? + yield(k, hash[k], v) + else + v.respond_to?(:deep_dup) ? v.deep_dup : v + end end hash end diff --git a/lib/hashie/extensions/indifferent_access.rb b/lib/hashie/extensions/indifferent_access.rb index e6e670a..4f5bd2c 100644 --- a/lib/hashie/extensions/indifferent_access.rb +++ b/lib/hashie/extensions/indifferent_access.rb @@ -32,12 +32,12 @@ module Hashie alias_method :regular_writer, :[]= unless method_defined?(:regular_writer) alias_method :[]=, :indifferent_writer alias_method :store, :indifferent_writer - %w(default update replace fetch delete key? values_at).each do |m| + %w[default update replace fetch delete key? values_at].each do |m| alias_method "regular_#{m}", m unless method_defined?("regular_#{m}") alias_method m, "indifferent_#{m}" end - %w(include? member? has_key?).each do |key_alias| + %w[include? member? has_key?].each do |key_alias| alias_method key_alias, :indifferent_key? end @@ -75,7 +75,7 @@ module Hashie # their proper indifferent state. Used when IndifferentAccess # is injecting itself into member hashes. def convert! - keys.each do |k| + keys.each do |k| # rubocop:disable Performance/HashEachMethods regular_writer convert_key(k), indifferent_value(regular_delete(k)) end self diff --git a/lib/hashie/extensions/mash/keep_original_keys.rb b/lib/hashie/extensions/mash/keep_original_keys.rb index 1b529a7..91b9b44 100644 --- a/lib/hashie/extensions/mash/keep_original_keys.rb +++ b/lib/hashie/extensions/mash/keep_original_keys.rb @@ -14,14 +14,12 @@ module Hashie # mash['string_key'] == mash[:string_key] #=> true # mash[:symbol_key] == mash['symbol_key'] #=> true module KeepOriginalKeys - private - def self.included(descendant) - unless descendant <= Hashie::Mash - fail ArgumentError, "#{descendant} is not a kind of Hashie::Mash" - end + raise ArgumentError, "#{descendant} is not a kind of Hashie::Mash" unless descendant <= Hashie::Mash end + private + # Converts the key when necessary to access the correct Mash key. # # @param [Object, String, Symbol] key the key to access. diff --git a/lib/hashie/extensions/mash/safe_assignment.rb b/lib/hashie/extensions/mash/safe_assignment.rb index 10a57dd..b1b2046 100644 --- a/lib/hashie/extensions/mash/safe_assignment.rb +++ b/lib/hashie/extensions/mash/safe_assignment.rb @@ -3,7 +3,7 @@ module Hashie module Mash module SafeAssignment def custom_writer(key, *args) #:nodoc: - fail ArgumentError, "The property #{key} clashes with an existing method." if !key?(key) && respond_to?(key, true) + raise ArgumentError, "The property #{key} clashes with an existing method." if !key?(key) && respond_to?(key, true) super end diff --git a/lib/hashie/extensions/mash/symbolize_keys.rb b/lib/hashie/extensions/mash/symbolize_keys.rb index 4e1d2be..91a6b14 100644 --- a/lib/hashie/extensions/mash/symbolize_keys.rb +++ b/lib/hashie/extensions/mash/symbolize_keys.rb @@ -19,7 +19,7 @@ module Hashie # @return [void] # @raise [ArgumentError] when the base class isn't a Mash def self.included(base) - fail ArgumentError, "#{base} must descent from Hashie::Mash" unless base <= Hashie::Mash + raise ArgumentError, "#{base} must descent from Hashie::Mash" unless base <= Hashie::Mash end private diff --git a/lib/hashie/extensions/method_access.rb b/lib/hashie/extensions/method_access.rb index 0ed2b22..e1ad56a 100644 --- a/lib/hashie/extensions/method_access.rb +++ b/lib/hashie/extensions/method_access.rb @@ -27,7 +27,7 @@ module Hashie # # user.not_declared # => NoMethodError module MethodReader - def respond_to?(name, include_private = false) + def respond_to_missing?(name, include_private = false) return true if key?(name.to_s) || key?(name.to_sym) super end @@ -67,15 +67,13 @@ module Hashie # h['awesome'] # => 'sauce' # module MethodWriter - def respond_to?(name, include_private = false) + def respond_to_missing?(name, include_private = false) return true if name.to_s =~ /=$/ super end def method_missing(name, *args) - if args.size == 1 && name.to_s =~ /(.*)=$/ - return self[convert_key(Regexp.last_match[1])] = args.first - end + return self[convert_key(Regexp.last_match[1])] = args.first if args.size == 1 && name.to_s =~ /(.*)=$/ super end @@ -106,7 +104,7 @@ module Hashie # h.def? # => false # h.hji? # => NoMethodError module MethodQuery - def respond_to?(name, include_private = false) + def respond_to_missing?(name, include_private = false) if query_method?(name) && indifferent_key?(key_from_query_method(name)) true else diff --git a/lib/hashie/extensions/parsers/yaml_erb_parser.rb b/lib/hashie/extensions/parsers/yaml_erb_parser.rb index c0dda75..c2808de 100644 --- a/lib/hashie/extensions/parsers/yaml_erb_parser.rb +++ b/lib/hashie/extensions/parsers/yaml_erb_parser.rb @@ -14,7 +14,7 @@ module Hashie def perform template = ERB.new(@content) template.filename = @file_path - YAML.load template.result + YAML.safe_load template.result end def self.perform(file_path) diff --git a/lib/hashie/extensions/strict_key_access.rb b/lib/hashie/extensions/strict_key_access.rb index 78f86f3..0822c53 100644 --- a/lib/hashie/extensions/strict_key_access.rb +++ b/lib/hashie/extensions/strict_key_access.rb @@ -46,27 +46,26 @@ module Hashie end def default(_ = nil) - fail DefaultError + raise DefaultError end def default=(_) - fail DefaultError + raise DefaultError end def default_proc - fail DefaultError + raise DefaultError end def default_proc=(_) - fail DefaultError + raise 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 + super.tap do |result| + if result.nil? && (!key?(result) || self[result] != value) + raise KeyError, "key not found with value of #{value.inspect}" + end end end end diff --git a/lib/hashie/extensions/stringify_keys.rb b/lib/hashie/extensions/stringify_keys.rb index 41b0fe3..bad29b6 100644 --- a/lib/hashie/extensions/stringify_keys.rb +++ b/lib/hashie/extensions/stringify_keys.rb @@ -44,7 +44,7 @@ module Hashie # test # => {'abc' => 'def'} def stringify_keys!(hash) hash.extend(Hashie::Extensions::StringifyKeys) unless hash.respond_to?(:stringify_keys!) - hash.keys.each do |k| + hash.keys.each do |k| # rubocop:disable Performance/HashEachMethods stringify_keys_recursively!(hash[k]) hash[k.to_s] = hash.delete(k) end diff --git a/lib/hashie/extensions/symbolize_keys.rb b/lib/hashie/extensions/symbolize_keys.rb index 274889c..e4315bd 100644 --- a/lib/hashie/extensions/symbolize_keys.rb +++ b/lib/hashie/extensions/symbolize_keys.rb @@ -44,7 +44,7 @@ module Hashie # test # => {:abc => 'def'} def symbolize_keys!(hash) hash.extend(Hashie::Extensions::SymbolizeKeys) unless hash.respond_to?(:symbolize_keys!) - hash.keys.each do |k| + hash.keys.each do |k| # rubocop:disable Performance/HashEachMethods symbolize_keys_recursively!(hash[k]) hash[k.to_sym] = hash.delete(k) end |
