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

Fix Custom Strategy #25

Merged
merged 1 commit into from
Dec 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions lib/polo/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,28 @@ class Configuration
def initialize(options={})
options = { on_duplicate: nil, obfuscate: {} }.merge(options)
@on_duplicate_strategy = options[:on_duplicate]
@blacklist = options[:obfuscate]
obfuscate(options[:obfuscate])
end

# TODO: document this
# This normalizes an array or hash of fields to a hash of
# { field_name => strategy }
def obfuscate(*fields)
@blacklist = fields
if fields.is_a?(Array)
fields = fields.flatten
end

fields_and_strategies = {}

fields.each do |field|
if field.is_a?(Symbol) || field.is_a?(String)
fields_and_strategies[field] = nil
elsif field.is_a?(Hash)
fields_and_strategies = fields_and_strategies.merge(field)
end
end

@blacklist = fields_and_strategies
end

def on_duplicate(strategy)
Expand Down
3 changes: 2 additions & 1 deletion lib/polo/translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def instances
def obfuscate!(instances, fields)
instances.each do |instance|
next if intersection(instance.attributes.keys, fields).empty?

fields.each do |field, strategy|
value = instance.attributes[field.to_s] || ''
instance.send("#{field}=", new_field_value(field, strategy, value))
Expand All @@ -44,7 +45,7 @@ def obfuscate!(instances, fields)
end

def intersection(attrs, fields)
attrs & fields.to_a.flatten.map(&:to_s)
attrs & fields.keys.map(&:to_s)
end

def new_field_value(field, strategy, value)
Expand Down
6 changes: 4 additions & 2 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@
end

defaults = Polo.defaults
expect(defaults.blacklist).to eq([:email, :password])
expect(defaults.blacklist).to eq({ :email => nil, :password => nil })
end

it 'allows for the user to define fields with strategies in blacklist' do
Polo.configure do

email_strategy = lambda {|e| "#{e.split("@")[0]}_test@example.com" }
credit_card_strategy = lambda {|_| "4111111111111111"}

obfuscate({email: email_strategy, credit_card: credit_card_strategy})
end

defaults = Polo.defaults
expect(defaults.blacklist.first[:email].call("a@b.com")).to eq("a_test@example.com")
expect(defaults.blacklist[:email].call("a@b.com")).to eq("a_test@example.com")
end
end
end
10 changes: 10 additions & 0 deletions spec/polo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@
expect(scrambled_email).to_not eq('nettofarah@gmail.com')
expect(insert).to match(exp.first)
end

it 'can apply custom strategies' do
Polo.configure do
obfuscate(email: lambda { |_| 'changeme' })
end

inserts = Polo.explore(AR::Chef, 1)

expect(inserts).to eq [ %q{INSERT INTO `chefs` (`id`, `name`, `email`) VALUES (1, 'Netto', 'changeme')} ]
end
end

describe 'on_duplicate' do
Expand Down