Class: DataAnon::Strategy::Field::AnonymizeDate

Inherits:
Object
  • Object
show all
Defined in:
lib/strategy/field/datetime/anonymize_date.rb

Overview

Anonmizes day and month fields within natural range based on true/false input for that field. By defaut both fields are anonymized

# anonymizes month and leaves day unchanged
anonymize('DateOfBirth').using FieldStrategy::AnonymizeDate.new(true,false)

In addition to customizing which fields you want anonymized, there are some helper methods which allow for quick anonymization

                                                                                                                                                                                           ```ruby
# anonymizes only the month field
anonymize('DateOfBirth').using FieldStrategy::AnonymizeDate.only_month
# anonymizes only the day field
anonymize('DateOfBirth').using FieldStrategy::AnonymizeDate.only_day

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(anonymize_month, anonymize_day) ⇒ AnonymizeDate

Returns a new instance of AnonymizeDate.


31
32
33
34
35
36
# File 'lib/strategy/field/datetime/anonymize_date.rb', line 31

def initialize anonymize_month, anonymize_day

  @anonymize_month = anonymize_month
  @anonymize_day = anonymize_day

end

Class Method Details

.only_dayObject


27
28
29
# File 'lib/strategy/field/datetime/anonymize_date.rb', line 27

def self.only_day
  self.new false, true
end

.only_monthObject


23
24
25
# File 'lib/strategy/field/datetime/anonymize_date.rb', line 23

def self.only_month
  self.new true, false
end

Instance Method Details

#anonymize(field) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/strategy/field/datetime/anonymize_date.rb', line 38

def anonymize field

  original_time = field.value

  year = original_time.year
  month = @anonymize_month? DataAnon::Utils::RandomInt.generate(1,12) : original_time.month
  days_in_month = Time.new(year,month,1,1,1,1).end_of_month.day
  day = @anonymize_day? DataAnon::Utils::RandomInt.generate(1,days_in_month) : original_time.day

  Date.new(year, month, day)
end