Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blacklisted warnings #478

Merged
merged 2 commits into from
Jul 18, 2019

Conversation

BobbyMcWho
Copy link
Collaborator

@BobbyMcWho BobbyMcWho commented Jul 16, 2019

Adds an optional array parameter to the disable_warnings method, so that you can pick and choose which warnings to ignore.

Usage:

class Response < Hashie::Mash
  disable_warnings :zip
end

Response.new('zip' => '90210') # does not log a warning
Response.new('merge' => 'yes') # does log a warning

This preserves existing usage.

@BobbyMcWho BobbyMcWho force-pushed the add-blacklisted-warnings branch from e248f53 to ad523c8 Compare July 16, 2019 19:16
@dblock
Copy link
Member

dblock commented Jul 16, 2019

Why do we need the only keyword? Is there going to be some other exclusion here in the future?

I feel like disable_warnings: [:zip] and disable_warning :zip is the most straightforward interface.

@BobbyMcWho
Copy link
Collaborator Author

@dblock I suppose we don't explicitly need the only keyword. I can refactor if you think your proposal is more straightforward. Is it worth adding the disable_warning singular case vs just using the plural disable_warnings with an array of 1 (or checking in the function if a symbol was passed vs an array)

@@ -59,7 +59,7 @@ module Hashie
# mash.author_.name = "Michael Bleigh" (assigned to temp object)
# mash.author # => <Mash>
#
class Mash < Hash
class Mash < Hash # rubocop:disable Metrics/ClassLength
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For rubocop just run rubocop -a ; rubocop --auto-gen-config. Or just disable Metrics/ClassLength altogether in .rubocop_config.yml.

@dblock
Copy link
Member

dblock commented Jul 16, 2019

I think the behavior when both variations are used is broken here. I'd expect these to pass:

    it 'does not write to the logger when warnings are disabled' do
      mash_class = Class.new(Hashie::Mash) do
        disable_warnings :zip
        disable_warnings
      end

      mash_class.new('trust' => { 'two' => 2 })

      expect(logger_output).to be_blank
    end

and this one when using an array

    it 'has cumulative behavior' do
      mash_class = Class.new(Hashie::Mash) do
        disable_warnings :two, :zip
      end

      mash_class.new('trust' => { 'two' => 2 })
      mash_class.new('address' => { 'zip' => '90210' })

      expect(logger_output).to be_blank
    end

@BobbyMcWho
Copy link
Collaborator Author

BobbyMcWho commented Jul 17, 2019

  it 'has cumulative behavior' do
      mash_class = Class.new(Hashie::Mash) do
        disable_warnings :two, :zip
      end

      mash_class.new('trust' => { 'two' => 2 })
      mash_class.new('address' => { 'zip' => '90210' })

      expect(logger_output).to be_blank
    end

This scenario works for the given keys, the error being thrown logged is due to #trust being defined from Kernal

I'll give some thoughts to the other scenario you mentioned.

@BobbyMcWho
Copy link
Collaborator Author

After looking at my test, the case where a child calls disable_warnings was incorrect, I resolved that and your first case should be resolved as well.

Now when disable_warnings is called without params after it has been called with them, we will clear the blacklist and ignore all conflicting method keys.

Copy link
Member

@michaelherold michaelherold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the implementation but I think we can get away with fewer lines of added code by changing the implementation of the disable_warnings? predicate.

lib/hashie/mash.rb Outdated Show resolved Hide resolved
lib/hashie/mash.rb Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
spec/hashie/mash_spec.rb Outdated Show resolved Hide resolved
spec/hashie/mash_spec.rb Show resolved Hide resolved
@dblock
Copy link
Member

dblock commented Jul 18, 2019

I am happy with this with the minor fixes in my last comments. @BobbyMcWho you should squash your commits, leaving the final review/comments/merge to @michaelherold.

Copy link
Member

@michaelherold michaelherold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking really good! I'd like to see a little more documentation in the readme and dB's typo fix, otherwise we're good-to-go.

README.md Show resolved Hide resolved
Use keyword arg instead of opts

Rubocop fixes

Uniq the blacklist

Add only arg for disable_warnings

Add Hashie::Mash.disable_warnings parameter

Remove :only keyword

Add array parameter Mash.disable_warnings

Allows for only ignoring specific warnings

Latest called disable_warnings takes priority

When called in succession, disable_warnings with params will simply append to the blacklist.
If called without params, we clear the blacklist and simply ignore all errors.

Change disable_warnings signature

This will make various pieces of code much more readable and reduce some duplication.

Additional multiple disable_warnings call specs

fix typo

Add additional disable_warnings documentation
@BobbyMcWho BobbyMcWho force-pushed the add-blacklisted-warnings branch from 359ac81 to a17f96f Compare July 18, 2019 14:17
@BobbyMcWho
Copy link
Collaborator Author

Thanks for the documentation suggestion @dblock, for the sake of clarity I also added:

Disable warnings will honor the last disable_warnings call. Calling without parameters will override the blacklist, and calling with parameters will create a new blacklist. This includes child classes that inherit from a class that disables warnings.

class Message < Hashie::Mash
  disable_warnings :zip, :zap
  disable_warnings
end
# No errors will be logged
Message.new(merge: 'true', compact: true)
class Message < Hashie::Mash
  disable_warnings
end
class Response < Message
  disable_warnings :zip, :zap
end
# 2 errors will be logged
Response.new(merge: 'true', compact: true, zip: '90210', zap: 'electric')

Copy link
Member

@michaelherold michaelherold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really happy with this now. I fixed up some spacing issues in the readme that I'll squash out once the tests pass.

Thanks a bunch for the contribution!

@BobbyMcWho
Copy link
Collaborator Author

Thanks for all the input from both of you, it's been a pleasure!

@michaelherold michaelherold merged commit f0faaca into hashie:master Jul 18, 2019
@dblock
Copy link
Member

dblock commented Jul 18, 2019

Nice work @BobbyMcWho, thanks for hanging in there with us!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants