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/dash.rb | |
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/dash.rb')
-rw-r--r-- | lib/hashie/dash.rb | 27 |
1 files changed, 13 insertions, 14 deletions
diff --git a/lib/hashie/dash.rb b/lib/hashie/dash.rb index a111e4b..ee27b11 100644 --- a/lib/hashie/dash.rb +++ b/lib/hashie/dash.rb @@ -15,7 +15,7 @@ module Hashie class Dash < Hash include Hashie::Extensions::PrettyInspect - alias_method :to_s, :inspect + alias to_s inspect # Defines a property on the Dash. Options are # as follows: @@ -48,16 +48,14 @@ module Hashie define_method(property_assignment) { |value| self.[]=(property_name, value) } end - if defined? @subclasses - @subclasses.each { |klass| klass.property(property_name, options) } - end + @subclasses.each { |klass| klass.property(property_name, options) } if defined? @subclasses condition = options.delete(:required) if condition message = options.delete(:message) || "is required for #{name}." required_properties[property_name] = { condition: condition, message: message } - else - fail ArgumentError, 'The :message option should be used with :required option.' if options.key?(:message) + elsif options.key?(:message) + raise ArgumentError, 'The :message option should be used with :required option.' end end @@ -111,8 +109,8 @@ module Hashie assert_required_attributes_set! end - alias_method :_regular_reader, :[] - alias_method :_regular_writer, :[]= + alias _regular_reader [] + alias _regular_writer []= private :_regular_reader, :_regular_writer # Retrieve a value from the Dash (will return the @@ -163,11 +161,12 @@ module Hashie update_attributes(attributes) self.class.defaults.each_pair do |prop, value| + next unless self[prop].nil? self[prop] = begin value.dup rescue TypeError value - end if self[prop].nil? + end end assert_required_attributes_set! end @@ -208,11 +207,11 @@ module Hashie end def fail_property_required_error!(property) - fail ArgumentError, "The property '#{property}' #{self.class.required_properties[property][:message]}" + raise ArgumentError, "The property '#{property}' #{self.class.required_properties[property][:message]}" end def fail_no_property_error!(property) - fail NoMethodError, "The property '#{property}' is not defined for #{self.class.name}." + raise NoMethodError, "The property '#{property}' is not defined for #{self.class.name}." end def required?(property) @@ -220,9 +219,9 @@ module Hashie condition = self.class.required_properties[property][:condition] case condition - when Proc then !!(instance_exec(&condition)) - when Symbol then !!(send(condition)) - else !!(condition) + when Proc then !!instance_exec(&condition) + when Symbol then !!send(condition) + else !!condition end end end |