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

Maskers respond to #call #23

Merged
merged 7 commits into from
Jul 30, 2017
Merged

Maskers respond to #call #23

merged 7 commits into from
Jul 30, 2017

Conversation

skalee
Copy link
Contributor

@skalee skalee commented Jul 30, 2017

  1. Extract the existing masker into a separate file, rework it, add tests.
  2. Change the standard masker method from #mask to #call.
  3. Improve the implementation of the default masker.
  4. Delete support for the :mask_method options.


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)") }

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 {.

subject{ described_class }

example{ expect(subject.(value: "Solo")).to eq("(redacted)") }
example{ expect(subject.(value: Math::PI)).to eq("(redacted)") }

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 {.

RSpec.describe AttrMasker::Maskers::SIMPLE do
subject{ described_class }

example{ expect(subject.(value: "Solo")).to eq("(redacted)") }

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 {.

require "spec_helper"

RSpec.describe AttrMasker::Maskers::SIMPLE do
subject{ described_class }

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 {.

#

# No point in using ApplicationRecord here.
# rubocop:disable Rails/ApplicationRecord

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.

# +opts+ is a Hash with the key :value that gives you the current attribute
# value.
#
SIMPLE = lambda do |opts|

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.

@@ -0,0 +1,13 @@
module AttrMasker
module Maskers

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.

@@ -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))

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,

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.

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.
@skalee skalee merged commit 9198eac into master Jul 30, 2017
@skalee skalee deleted the callable-maskers branch July 30, 2017 21:01
@skalee skalee mentioned this pull request Jul 30, 2017
4 tasks
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.

None yet

2 participants