Skip to content

Commit

Permalink
Merge pull request #25 from IFTTT/fix-custom-strategies
Browse files Browse the repository at this point in the history
Fix Custom Strategy
  • Loading branch information
nettofarah committed Dec 4, 2015
2 parents 791d8c0 + b79e48e commit 8d926aa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
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

0 comments on commit 8d926aa

Please sign in to comment.