Class: Kiba::Extend::Transforms::Cspace::MovementReferenceNumber
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::Cspace::MovementReferenceNumber
- Defined in:
- lib/kiba/extend/transforms/cspace/movement_reference_number.rb
Overview
Generates typical pattern of movementReferenceNumbers based on year in location date, where available. For each year, the procedure data is sorted by the full date value, and the reference number value is incremented.
The typical number pattern used is the location record pattern from the number autogenerator in Location/Movement/Inventory (LMI) the UI. The pattern is:
- LOC (customizable via the
prefixparameter) - 4-digit year (for rows with no date, the value given for
nodatevalwill be inserted here) - .1 (this is one of those meaningless id number segments that never
gets incremented, but clients like to have it there to match the
default autogenerator, so here we are… customize this if desired
via the
initialsegmentparam) - .
- autoincrementing digit
ASSUMPTIONS OF THIS TRANSFORM
- You will have already reduced the output to one row per LMI to be created in CollectionSpace
- You will have used DateValue::ForceDayPrecision or some other
method to create a field containing only blank values and valid
ISO 8601 dates to be mapped to the unstructured date
locationdatefield in CollectionSpace
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
-
#initialize(datefield:, prefix: "LOC", nodateval: "0000", initialsegment: ".1", target: :movementreferencenumber, sorter: DEFAULT_SORTER) ⇒ MovementReferenceNumber
constructor
A new instance of MovementReferenceNumber.
-
#process(row) ⇒ Object
Constructor Details
#initialize(datefield:, prefix: "LOC", nodateval: "0000", initialsegment: ".1", target: :movementreferencenumber, sorter: DEFAULT_SORTER) ⇒ MovementReferenceNumber
Returns a new instance of MovementReferenceNumber.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/kiba/extend/transforms/cspace/movement_reference_number.rb', line 157 def initialize(datefield:, prefix: "LOC", nodateval: "0000", initialsegment: ".1", target: :movementreferencenumber, sorter: DEFAULT_SORTER) @datefield = datefield @prefix = prefix @nodateval = nodateval @initialsegment = initialsegment @target = target @nodatesortval = nodatesortval @sorter = sorter @holder = {} end |
Instance Method Details
#close ⇒ Object
181 182 183 184 185 |
# File 'lib/kiba/extend/transforms/cspace/movement_reference_number.rb', line 181 def close holder.map { |year, data| generate_for_year(year, data) } .flatten .each { |row| yield row } end |
#process(row) ⇒ Object
173 174 175 176 177 178 179 |
# File 'lib/kiba/extend/transforms/cspace/movement_reference_number.rb', line 173 def process(row) dateval = row[datefield] year = dateval.blank? ? nodateval : dateval[0..3] populate_holder(row, year) nil end |