Class: DataAnon::Strategy::Field::AnonymizeTime

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

Overview

Anonymizes each field(except year and seconds) within the natural range (e.g. hour between 1-24 and day within the month) based on true/false input for that field. By default, all fields are anonymized.

# anonymizes month and hour fields, leaving the day and minute fields untouched
anonymize('DateOfBirth').using FieldStrategy::AnonymizeTime.new(true,false,true,false)

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

# anonymizes only the month field
anonymize('DateOfBirth').using FieldStrategy::AnonymizeTime.only_month
# anonymizes only the day field
anonymize('DateOfBirth').using FieldStrategy::AnonymizeTime.only_day
# anonymizes only the hour field
anonymize('DateOfBirth').using FieldStrategy::AnonymizeTime.only_hour
# anonymizes only the minute field
anonymize('DateOfBirth').using FieldStrategy::AnonymizeTime.only_minute

Direct Known Subclasses

AnonymizeDateTime

Constant Summary collapse

DEFAULT_ANONYMIZATION =
true

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(anonymize_month, anonymize_day, anonymize_hour, anonymize_min) ⇒ AnonymizeTime

Returns a new instance of AnonymizeTime.


44
45
46
47
48
49
50
51
# File 'lib/strategy/field/datetime/anonymize_time.rb', line 44

def initialize anonymize_month, anonymize_day, anonymize_hour, anonymize_min

  @anonymize_month = anonymize_month
  @anonymize_day = anonymize_day
  @anonymize_hour = anonymize_hour
  @anonymize_min = anonymize_min

end

Class Method Details

.only_dayObject


32
33
34
# File 'lib/strategy/field/datetime/anonymize_time.rb', line 32

def self.only_day
  self.new false, true, false, false
end

.only_hourObject


36
37
38
# File 'lib/strategy/field/datetime/anonymize_time.rb', line 36

def self.only_hour
  self.new false, false, true, false
end

.only_minuteObject


40
41
42
# File 'lib/strategy/field/datetime/anonymize_time.rb', line 40

def self.only_minute
  self.new false, false, false, true
end

.only_monthObject


28
29
30
# File 'lib/strategy/field/datetime/anonymize_time.rb', line 28

def self.only_month
  self.new true, false, false, false
end

Instance Method Details

#anonymize(field) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/strategy/field/datetime/anonymize_time.rb', line 53

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
  hour = @anonymize_hour? DataAnon::Utils::RandomInt.generate(1,24) : original_time.hour
  min = @anonymize_min? DataAnon::Utils::RandomInt.generate(1,60) : original_time.min
  sec = original_time.sec

  create_object(year, month, day, hour, min, sec)
end