Skip to content

Commit

Permalink
Merge pull request #45 from seandenigris/enh_magritte-writer-header-opt
Browse files Browse the repository at this point in the history
[Enh]: Magritte Writer - Header Option & Survive Empty Coll.
  • Loading branch information
seandenigris authored Nov 16, 2023
2 parents 704e459 + e059c21 commit 2dba1b7
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions repository/Neo-CSV-Magritte/MACSVWriter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ MACSVWriter >> execute [
ifFalse: [ self target ensureCreateFile writeStreamDo: [ :str | self writeToStream: str ] ]
]

{ #category : #private }
MACSVWriter >> fieldDescriptions [
^ self subjectDescription
select: [ :desc | desc hasProperty: self fieldNamePropertyKey ]
]

{ #category : #accessing }
MACSVWriter >> fieldNamePropertyKey [
"The property where the element description stores the field name; override to customize"
Expand All @@ -33,6 +39,22 @@ MACSVWriter >> fieldWriterPropertyKey [
^ #csvWriter
]

{ #category : #private }
MACSVWriter >> header [
^ self fieldDescriptions children
collect: [ :field | field propertyAt: self fieldNamePropertyKey ifAbsent: [ field name ] ]
]

{ #category : #accessing }
MACSVWriter >> includesHeader [
^ includesHeader ifNil: [ true ].
]

{ #category : #accessing }
MACSVWriter >> includesHeader: anObject [
includesHeader := anObject
]

{ #category : #accessing }
MACSVWriter >> map [

Expand Down Expand Up @@ -81,28 +103,23 @@ MACSVWriter >> target: aFileOrStream [

{ #category : #private }
MACSVWriter >> writeToStream: aStream [
| fieldDescriptions header |
self subjects isEmptyOrNil ifTrue: [ ^ self ].

self map do: [ :field | field configureDescriptionFor: self ].

fieldDescriptions := self subjectDescription
select: [ :desc | desc hasProperty: self fieldNamePropertyKey ].

header := fieldDescriptions children
collect: [ :field | field propertyAt: self fieldNamePropertyKey ifAbsent: [ field name ] ].

fieldDescriptions
self fieldDescriptions
do: [ :field |
| converter |
converter := field
propertyAt: self fieldWriterPropertyKey
ifAbsent: [ [ :anObject | field read: anObject ] ].
self writer addField: converter ].

self writer
on: aStream;
writeHeader: header;
nextPutAll: self subjects
writer := self writer on: aStream.

self includesHeader ifTrue: [ writer writeHeader: self header ].

writer nextPutAll: self subjects
]

{ #category : #accessing }
Expand Down

0 comments on commit 2dba1b7

Please sign in to comment.