Class: Kiba::Extend::Transforms::IdGenerator::YearBasedIncrementing
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::IdGenerator::YearBasedIncrementing
- Defined in:
- lib/kiba/extend/transforms/id_generator/year_based_incrementing.rb
Overview
Generates unique ID values based on the year extracted from a given date field, where available. For each year extracted, the rows are sorted by the full date value, and the id number value is incremented.
The transform supports a multi-segment number pattern. All segments except for the year and incrementing digit(s) are optional. Set them as empty Strings when you set up the transform to omit them. The supported segments are:
- prefix
- 4-digit year (for rows with no date, the value given for
nodatevalwill be inserted here) - preincrementsegment
- autoincrementing digit
ASSUMPTIONS OF THIS TRANSFORM
- You will have already reduced the output to one row per unique ID that needs to be created
- You will have used DateValue::ForceDayPrecision or some other
method to create a field containing only blank values and valid
ISO 8601 dates, that will be set as the
datefieldparam
SORTING, or, how the end digit gets incremented within a year
During the first pass over the data, we populate holder hash. Keys
are the years extracted from the datefield values in all rows.
Values are the rows from whom that year was extracted.
In the final pass, a sorter is applied to the Array of rows for each year key.
The default sorter:
- parses the
datefieldvalue to create a sortable Ruby Date if a value is present; uses0001-01-01if not - sorts by this Ruby Date
- if a number of rows have the same date, they’ll be in the same order as they appear in the data before this transform is applied
If your data contains full time stamps or some other sequential value
you would like to use to sort, you can pass a custom sorter Lamda.
This Lambda should take two positional arguments. The transform
passes the Array of rows for the year key as the first argument,
and the datefield attr value as the second. See the custom sorter
example for how you can indicate the datefield attr won’t be used.
It should return the Array of rows, sorted as desired.
Constant Summary collapse
- DEFAULT_SORTER =
This sorter is used if no custom sorter is passed in when initializing the transform
->(rows, datefield) do rows.sort_by do |row| dateval = row[datefield] dateval.blank? ? Date.new(1, 1, 1) : Date.parse(dateval) end end
Instance Method Summary collapse
-
#close ⇒ Object
-
#finalized_rows ⇒ Array{Hash}
Array of rows to be yielded by
closemethod of using class. -
#initialize(datefield:, target:, prefix:, preincrementsegment: ".", nodateval: "0000", sorter: DEFAULT_SORTER) ⇒ YearBasedIncrementing
constructor
A new instance of YearBasedIncrementing.
-
#process(row) ⇒ Object
Constructor Details
#initialize(datefield:, target:, prefix:, preincrementsegment: ".", nodateval: "0000", sorter: DEFAULT_SORTER) ⇒ YearBasedIncrementing
Returns a new instance of YearBasedIncrementing.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/kiba/extend/transforms/id_generator/year_based_incrementing.rb', line 160 def initialize(datefield:, target:, prefix:, preincrementsegment: ".", nodateval: "0000", sorter: DEFAULT_SORTER) @datefield = datefield @target = target @prefix = prefix @preincrementsegment = preincrementsegment @nodateval = nodateval @sorter = sorter || DEFAULT_SORTER @holder = {} end |
Instance Method Details
#close ⇒ Object
183 184 185 |
# File 'lib/kiba/extend/transforms/id_generator/year_based_incrementing.rb', line 183 def close finalized_rows.each { |row| yield row } end |
#finalized_rows ⇒ Array{Hash}
Returns Array of rows to be yielded by close method
of using class.
189 190 191 192 |
# File 'lib/kiba/extend/transforms/id_generator/year_based_incrementing.rb', line 189 def finalized_rows holder.map { |year, data| generate_for_year(year, data) } .flatten end |
#process(row) ⇒ Object
175 176 177 178 179 180 181 |
# File 'lib/kiba/extend/transforms/id_generator/year_based_incrementing.rb', line 175 def process(row) dateval = row[datefield] year = dateval.blank? ? nodateval : dateval[0..3] populate_holder(row, year) nil end |