Class: ConfidentialInfoRedactorLite::Date

Inherits:
Object
  • Object
show all
Defined in:
lib/confidential_info_redactor_lite/date.rb

Constant Summary collapse

DMY_MDY_REGEX =
/(\d{1,2}(\/|\.|-)){2}\d{4}/
YMD_YDM_REGEX =
/(?<=\A|\s)\d{4}(\/|\.|-)(\d{1,2}(\/|\.|-)){2}/
DIGIT_ONLY_YEAR_FIRST_REGEX =
/[12]\d{7}\D/
DIGIT_ONLY_YEAR_LAST_REGEX =
/\d{4}[12]\d{3}\D/
JA_DATE_REGEX_LONG =
/[0123456789]+年[0123456789]+月[0123456789]+日/
JA_DATE_REGEX_SHORT =
/[0123456789]+月[0123456789]+日/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dow:, dow_abbr:, months:, months_abbr:) ⇒ Date

Returns a new instance of Date.



20
21
22
23
24
25
# File 'lib/confidential_info_redactor_lite/date.rb', line 20

def initialize(dow:, dow_abbr:, months:, months_abbr:)
  @dow = dow
  @dow_abbr = dow_abbr
  @months = months
  @months_abbr = months_abbr
end

Instance Attribute Details

#dowObject (readonly)

Returns the value of attribute dow.



19
20
21
# File 'lib/confidential_info_redactor_lite/date.rb', line 19

def dow
  @dow
end

#dow_abbrObject (readonly)

Returns the value of attribute dow_abbr.



19
20
21
# File 'lib/confidential_info_redactor_lite/date.rb', line 19

def dow_abbr
  @dow_abbr
end

#monthsObject (readonly)

Returns the value of attribute months.



19
20
21
# File 'lib/confidential_info_redactor_lite/date.rb', line 19

def months
  @months
end

#months_abbrObject (readonly)

Returns the value of attribute months_abbr.



19
20
21
# File 'lib/confidential_info_redactor_lite/date.rb', line 19

def months_abbr
  @months_abbr
end

Instance Method Details

#includes_date?(text) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/confidential_info_redactor_lite/date.rb', line 27

def includes_date?(text)
  includes_long_date?(text) || includes_number_only_date?(text)
end

#occurences(text) ⇒ Object



40
41
42
# File 'lib/confidential_info_redactor_lite/date.rb', line 40

def occurences(text)
  replace(text).scan(/<redacted date>/).size
end

#replace(text) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/confidential_info_redactor_lite/date.rb', line 31

def replace(text)
  return text unless is_an_array?
  counter = 0
  dow_abbr.map { |day| counter +=1 if text.include?('day') }
  text = text.gsub(JA_DATE_REGEX_LONG, '<redacted date>').gsub(JA_DATE_REGEX_SHORT, '<redacted date>')
  text = redact_dates(counter, text)
  redact_regex(text)
end

#replace_number_only_date(text) ⇒ Object



44
45
46
47
48
49
# File 'lib/confidential_info_redactor_lite/date.rb', line 44

def replace_number_only_date(text)
  text.gsub(DMY_MDY_REGEX, ' <redacted date> ')
      .gsub(YMD_YDM_REGEX, ' <redacted date> ')
      .gsub(DIGIT_ONLY_YEAR_FIRST_REGEX, ' <redacted date> ')
      .gsub(DIGIT_ONLY_YEAR_LAST_REGEX, ' <redacted date> ')
end