-
Notifications
You must be signed in to change notification settings - Fork 8
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
Maskers respond to #call #23
Conversation
skalee
commented
Jul 30, 2017
- Extract the existing masker into a separate file, rework it, add tests.
- Change the standard masker method from #mask to #call.
- Improve the implementation of the default masker.
- Delete support for the :mask_method options.
spec/maskers/simple_spec.rb
Outdated
|
||
example{ expect(subject.(value: "Solo")).to eq("(redacted)") } | ||
example{ expect(subject.(value: Math::PI)).to eq("(redacted)") } | ||
example{ expect(subject.(value: nil)).to eq("(redacted)") } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space missing to the left of {.
spec/maskers/simple_spec.rb
Outdated
subject{ described_class } | ||
|
||
example{ expect(subject.(value: "Solo")).to eq("(redacted)") } | ||
example{ expect(subject.(value: Math::PI)).to eq("(redacted)") } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space missing to the left of {.
spec/maskers/simple_spec.rb
Outdated
RSpec.describe AttrMasker::Maskers::SIMPLE do | ||
subject{ described_class } | ||
|
||
example{ expect(subject.(value: "Solo")).to eq("(redacted)") } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space missing to the left of {.
spec/maskers/simple_spec.rb
Outdated
require "spec_helper" | ||
|
||
RSpec.describe AttrMasker::Maskers::SIMPLE do | ||
subject{ described_class } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space missing to the left of {.
spec/maskers/simple_spec.rb
Outdated
# | ||
|
||
# No point in using ApplicationRecord here. | ||
# rubocop:disable Rails/ApplicationRecord |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary disabling of Rails/ApplicationRecord.
lib/attr_masker/maskers/simple.rb
Outdated
# +opts+ is a Hash with the key :value that gives you the current attribute | ||
# value. | ||
# | ||
SIMPLE = lambda do |opts| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused block argument - opts. If it's necessary, use _ or _opts as an argument name to indicate that it won't be used. Also consider using a proc without arguments instead of a lambda if you want it to accept any arguments but don't care about them.
lib/attr_masker/maskers/simple.rb
Outdated
@@ -0,0 +1,13 @@ | |||
module AttrMasker | |||
module Maskers | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra empty line detected at module body beginning.
lib/attr_masker.rb
Outdated
@@ -127,8 +128,7 @@ def mask(attribute, value, options = {}) | |||
# if options[:if] && !options[:unless] && !value.nil? && !(value.is_a?(String) && value.empty?) | |||
if options[:if] && !options[:unless] | |||
value = options[:marshal] ? options[:marshaler].send(options[:dump_method], value) : value.to_s | |||
# masker_value = options[:masker].send(options[:mask_method], options.merge!(:value => value)) | |||
masker_value = options[:masker].send(options[:mask_method], options.merge!(:value => value)) | |||
masker_value = options[:masker].call(options.merge!(:value => value)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the new Ruby 1.9 hash syntax.
@@ -79,8 +81,7 @@ def attr_masker(*attributes) | |||
:marshaler => Marshal, | |||
:dump_method => "dump", | |||
:load_method => "load", | |||
:masker => AttrMasker::Masker, | |||
:mask_method => "mask", | |||
:masker => AttrMasker::Maskers::SIMPLE, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the new Ruby 1.9 hash syntax.
# :masker => The object to use for masking. Defaults to Masker. | ||
# | ||
# :mask_method => The mask method name to call on the <tt>:masker</tt> object. Defaults to 'mask'. | ||
# :masker => The object to use for masking. It must respond to +#mask+. Defaults to AttrMasker::Maskers::Simple. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is too long. [126/80]
When masking, use the masker's #call method instead of #mask. This also adds support for proc-style maskers.
So far, all the objects passed as options to the ::attr_masker method were eagerly evaluated, if they were responding to #call or were symbols denoting some model methods. Actually, that was the source of problems and could result in a premature masker evaluation. In the worst case, that could cause the unexpected behaviour when checking the :if and :unless options, as the option evaluation order was arbitrary. This is fixed in this commit.
Rename the AttrMasker::Maskers::Simple to AttrMasker::Maskers::SIMPLE and turn it into a lambda object.
The line of code which is being deleted in this commit was an exact copy of the line right below, just commented out.
This option was really pretty useless, that feature can be easily duplicated with help of procs or BoundMethod instances.