Class: Kiba::Extend::Transforms::DateValue::ForceDayPrecision

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/date_value/force_day_precision.rb

Overview

Note:

The current iteration of this transform has very limited date parsing intelligence. We recommend that you set the target field to a new field, so that you can check the output against the original values.

Note:

The current iteration interprets a date like “2/4/2001” as February 4, not April 2. This is not currently configurable.

Note:

The current iteration of this transform will handle 2-digit years unpredictably

Pads out year or year-month date values to year-month-day values in ISO 8601 format (yyyy-mm-dd).

If a date cannot be converted to a valid ISO 8601 date, the target field will be nil, and “error” will be written to the warnfield.

If a date could be converted, but via an unpredictable means (e.g. the default Ruby Date.parse functionality), then the converted valid ISO 8601 date is written to target, and “warn” is written to the warnfield.

Examples:

# Used in pipeline as:
# transform DateValue::ForceDayPrecision,
#   source: :in,
#   target: :out,
#   month: 2,
#   day: 29,
#   warnfield: :rept
xform = DateValue::ForceDayPrecision.new(
  source: :in,
  target: :out,
  month: 2,
  day: 29,
  warnfield: :rept
)
input = [
  {in: "2000"},
  {in: "2001"},
  {in: "2000-02"},
  {in: "2000/02"},
  {in: "02/2000"},
  {in: "02/2001"},
  {in: "March 2017"},
  {in: "2000 Feb"},
  {in: "2/4/2017"},
  {in: "2/26/2017"},
  {in: "2017-02-04"},
  {in: "2001-02-29"},
  {in: "March 4"},
  {in: "March 4, 2015"},
  {in: "boom/bop/bam"},
  {in: nil},
  {in: ""}
]
result = Kiba::StreamingRunner.transform_stream(input, xform)
  .map{ |row| row }
expected = [
  {in: "2000", out: "2000-02-29", rept: nil},
  {in: "2001", out: nil, rept: "error"},
  {in: "2000-02", out: "2000-02-29", rept: nil},
  {in: "2000/02", out: "2000-02-29", rept: nil},
  {in: "02/2000", out: "2000-02-29", rept: nil},
  {in: "02/2001", out: nil, rept: "error"},
  {in: "March 2017", out: "2017-03-29", rept: nil},
  {in: "2000 Feb", out: "2000-02-29", rept: nil},
  {in: "2/4/2017", out: "2017-02-04", rept: nil},
  {in: "2/26/2017", out: "2017-02-26", rept: nil},
  {in: "2017-02-04", out: "2017-02-04", rept: nil},
  {in: "2001-02-29", out: nil, rept: "error"},
  {in: "March 4", out: "2026-03-04", rept: "warn"},
  {in: "March 4, 2015", out: "2015-03-04", rept: "warn"},
  {in: "boom/bop/bam", out: nil, rept: "error"},
  {in: nil, out: nil, rept: nil},
  {in: "", out: nil, rept: nil}
]
expect(result).to eq(expected)

Instance Method Summary collapse

Constructor Details

#initialize(source:, target:, warnfield: :date_warning, month: 1, day: 1) ⇒ ForceDayPrecision

Returns a new instance of ForceDayPrecision.

Parameters:

  • source (Symbol)

    field containing non-day-precise date values

  • target (Symbol)

    field in which to write day-precise date values

  • warnfield (Symbol) (defaults to: :date_warning)

    field in which to write error and warning flags

  • month (Integer) (defaults to: 1)

    Default month to provide

  • day (Integer) (defaults to: 1)

    Default day to provide. This must be a valid day for any month in a year-month source value. For instance, you cannot set this to 31, and succssfully pad 2002-02.



92
93
94
95
96
97
98
99
# File 'lib/kiba/extend/transforms/date_value/force_day_precision.rb', line 92

def initialize(source:, target:, warnfield: :date_warning,
  month: 1, day: 1)
  @source = source
  @target = target
  @warnfield = warnfield
  @month = month
  @day = day
end

Instance Method Details

#process(row) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/kiba/extend/transforms/date_value/force_day_precision.rb', line 101

def process(row)
  [target, warnfield].each { |f| row[f] = nil }
  val = row[source]
  return row if val.blank?

  write_target_and_warnings(get_result(val), row)

  row
end