NeoCSV
NeoCSV copied to clipboard
Defining an instance creation method for recordClass
Sometimes I'd like to create a new instance of my recordClass with some additional initializations. Say I need to create a new empty address in a Customer Address on import. So I'd like the CSVREader not only to create a new instance by sending #new to the class, but something else.
I see two or three scenarios for this:
- we add an inst var #creationMethod (with a default of
#new
)to NeoCSVReader and send that method to the #recordClass in #readNextRecordAsObject ex:myReader creationMethod: #newWithEmptyAddress
- we add an inst var #postCreationMethod and send it to the newly created instance after its creation
- we add some Block for this purpose
First question: does this make sense at all? Second question: I have never used Pharo with Iceberg and git before and have no real idea hwo to do it, but if I wanted to, what version of Pharo and NeoCSVReader should I use these days? Pharo 8 or 9?
Here is what I think #readNextRecordAsObject would look like with this change:
``readNextRecordAsObject
| object conversionBlock idx |
conversionBlock := [:each :obj :input :fieldIndex |
[each value: obj value: input]
on: Error
do: [:err |
Error signal: (
'Error parsing "%1" in column #%2: %3' bindWith: input with: fieldIndex asString with: err description)]].
object := recordClass perform: self instanceCreationMethod. "<--- that's all, folks"
idx := 1.
fieldAccessors
do: [:each | | rawValue |
... ``
There is now the following commit:
https://github.com/svenvc/NeoCSV/commit/0acc2270b382f52533c478f2f1585341e390d4b5
which should address a couple of issues.
There subclass hooks for instanciating recordClass.