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

Refactor anonymisable interface into fields and destroy groups #32

Merged
merged 3 commits into from
Dec 4, 2019

Conversation

nickcampbell18
Copy link
Contributor

@nickcampbell18 nickcampbell18 commented Nov 27, 2019

Since it's incompatible to have both destroy and field-level configuration, this PR modifies a fairly hefty chunk of the anony internals to accomplish what felt like a reasonable goal: isolating the field-level strategies and the destroy strategies into different classes. The code is structured so that it's impossible to define both of these, because the configuration is essentially an enum on a single instance variable (Anony::ModelConfig#@strategy).

This makes the public interface a bit more unwieldy, but I'd argue this makes sense in the context of some proposed changes in discussion. An example will hopefully be illustrative:

class Manager
  anonymise do
    fields do
      hex :first_name, :last_name
      ignore :company_id
    end

    skip_if { some_condition? }

    before { puts "Starting to anonymise..." }
  end
end

As you can see, hex and ignore don't logically belong at the same level as skip_if or before/after. This isolates them into a fields block, which is encapsulated as an Anony::Strategies::Fields instance (hence the first commit moving other things around). Sandi Metz would be proud of the subsequent approach: we delegate the valid?, validate! and apply methods to those strategies.

strategy
end
self.class.anonymise_config.validate!
self.class.anonymise_config.apply(self)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm incredibly happy about this part.

Copy link
Contributor

Choose a reason for hiding this comment

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

And so you should be.

Copy link
Contributor

@matt-thomson matt-thomson left a comment

Choose a reason for hiding this comment

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

Like it very much. Agree that moving over to this is a slight pain, but should be pretty mechanical.

We don't have to tackle it now, but I'm wondering if there's a use case for specifying your own "model-level" strategies?

lib/anony/anonymisable.rb Outdated Show resolved Hide resolved
strategy
end
self.class.anonymise_config.validate!
self.class.anonymise_config.apply(self)
Copy link
Contributor

Choose a reason for hiding this comment

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

And so you should be.

lib/anony/field_level_strategies.rb Outdated Show resolved Hide resolved
lib/anony/model_config.rb Outdated Show resolved Hide resolved
lib/anony/model_config.rb Outdated Show resolved Hide resolved
anony.gemspec Outdated Show resolved Hide resolved
@nickcampbell18 nickcampbell18 marked this pull request as ready for review December 3, 2019 16:53
Copy link
Contributor

@matt-thomson matt-thomson left a comment

Choose a reason for hiding this comment

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

Couple of minor comments, otherwise this looks good 👍

lib/anony/field_level_strategies.rb Outdated Show resolved Hide resolved
lib/anony/anonymisable.rb Outdated Show resolved Hide resolved
Nick Campbell added 2 commits December 4, 2019 11:27
In preparation for an upcoming refactor, the Strategies namespace is
going to be reused for a higher-level concept. This moves strategies
relating to fields into a special module, FieldLevelStrategies.

This commit also moves all of the strategies and their specs into a
single file. This is because the definition of strategies should be
effectively atomic with the loading of the dynamic registration code.
This means that consumers which require this module will automatically
get all of the necessary strategies configured for them without having
to load separate files.
@nickcampbell18 nickcampbell18 merged commit 7164fe2 into master Dec 4, 2019
@nickcampbell18 nickcampbell18 deleted the fields branch December 4, 2019 12:01
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.

2 participants