class DataAnon::Strategy::Field::AnonymizeTime

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.

!!!ruby
# 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

!!!ruby
# 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

Constants

DEFAULT_ANONYMIZATION

Public Class Methods

new(anonymize_month, anonymize_day, anonymize_hour, anonymize_min) click to toggle source
# 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
only_day() click to toggle source
# File lib/strategy/field/datetime/anonymize_time.rb, line 32
def self.only_day
  self.new false, true, false, false
end
only_hour() click to toggle source
# File lib/strategy/field/datetime/anonymize_time.rb, line 36
def self.only_hour
  self.new false, false, true, false
end
only_minute() click to toggle source
# File lib/strategy/field/datetime/anonymize_time.rb, line 40
def self.only_minute
  self.new false, false, false, true
end
only_month() click to toggle source
# File lib/strategy/field/datetime/anonymize_time.rb, line 28
def self.only_month
  self.new true, false, false, false
end

Public Instance Methods

anonymize(field) click to toggle source
# 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(0,23) : original_time.hour
  min = @anonymize_min? DataAnon::Utils::RandomInt.generate(0,59) : original_time.min
  sec = original_time.sec

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

Private Instance Methods

create_object(year, month, day, hour, min, sec) click to toggle source
# File lib/strategy/field/datetime/anonymize_time.rb, line 70
def create_object(year, month, day, hour, min, sec)
  Time.new(year, month, day, hour, min, sec)
end