-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into doc_magritte-parsers
- Loading branch information
Showing
12 changed files
with
723 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
Class { | ||
#name : #MACSVField, | ||
#superclass : #Object, | ||
#instVars : [ | ||
'descriptionSource', | ||
'name', | ||
'encoder', | ||
'decoder' | ||
], | ||
#category : #'Neo-CSV-Magritte' | ||
} | ||
|
||
{ #category : #accessing } | ||
MACSVField >> configureDescriptionFor: aWriter [ | ||
|
||
| description | | ||
self descriptionSource isSymbol ifTrue: [ | ||
description := aWriter subjectDescription detect: [ :fieldDesc | | ||
fieldDesc definingContext methodSelector = self descriptionSource ] ]. | ||
|
||
self descriptionSource isBlock ifTrue: [ | ||
description := self descriptionSource value. | ||
aWriter subjectDescription add: description ]. | ||
|
||
description | ||
propertyAt: aWriter fieldNamePropertyKey | ||
put: self name. | ||
|
||
self encoder ifNotNil: [ :anEncoder | | ||
description | ||
propertyAt: aWriter fieldWriterPropertyKey | ||
put: anEncoder ]. | ||
|
||
self decoder ifNotNil: [ :aDecoder | | ||
description | ||
propertyAt: aWriter fieldWriterPropertyKey | ||
put: aDecoder ]. | ||
|
||
^ description | ||
] | ||
|
||
{ #category : #accessing } | ||
MACSVField >> decoder [ | ||
^ decoder | ||
] | ||
|
||
{ #category : #accessing } | ||
MACSVField >> decoder: anObject [ | ||
decoder := anObject | ||
] | ||
|
||
{ #category : #accessing } | ||
MACSVField >> descriptionSource [ | ||
^ descriptionSource | ||
] | ||
|
||
{ #category : #accessing } | ||
MACSVField >> descriptionSource: anObject [ | ||
"anObject - either a block returning a description, or the defining selector of a description already defined by the domain objects being written" | ||
|
||
descriptionSource := anObject | ||
] | ||
|
||
{ #category : #accessing } | ||
MACSVField >> encoder [ | ||
^ encoder | ||
] | ||
|
||
{ #category : #accessing } | ||
MACSVField >> encoder: anObject [ | ||
encoder := anObject | ||
] | ||
|
||
{ #category : #accessing } | ||
MACSVField >> name [ | ||
^ name | ||
] | ||
|
||
{ #category : #accessing } | ||
MACSVField >> name: anObject [ | ||
name := anObject | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
repository/Neo-CSV-Magritte/MACSVMappedPragmaBuilder.class.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
" | ||
I provide a way to extend Magritte element descriptions for CSV reading without modifying the containing domain class. I help avoid bloating domain classes with extensions for each supported CSV source. For example, for contacts, Google has its field names, Outlook has others… By using me, you will not have to extend existing element descriptions via Magritte's pragma (the one that takes the description selector as an argument). | ||
To configure me, you should: | ||
- set the `#fieldNamePropertyKey` that your reader expects to map descriptions to CSV fields | ||
- as needed, specify the `#fieldReaderPropertyKey` that your reader uses to convert the CSV field string to a domain object; otherwise the default reader will be used | ||
The two primary ways to map fields to descriptions are: | ||
- `aBuilder map: fieldName fieldTo: descriptionSelector` | ||
- `aBuilder map: fieldName fieldTo: descriptionSelector using: reader` | ||
" | ||
Class { | ||
#name : #MACSVMappedPragmaBuilder, | ||
#superclass : #MAPragmaBuilder, | ||
#instVars : [ | ||
'map', | ||
'fieldNamePropertyKey', | ||
'fieldReaderPropertyKey' | ||
], | ||
#category : #'Neo-CSV-Magritte-Support' | ||
} | ||
|
||
{ #category : #private } | ||
MACSVMappedPragmaBuilder >> description: description extendedBy: descriptionExtensions for: descriptionSelector of: anObject [ | ||
| result | | ||
result := super | ||
description: description | ||
extendedBy: descriptionExtensions | ||
for: descriptionSelector | ||
of: anObject. | ||
|
||
self ensureFieldPropertiesForDescription: result from: descriptionSelector. | ||
|
||
^ result | ||
] | ||
|
||
{ #category : #private } | ||
MACSVMappedPragmaBuilder >> ensureFieldPropertiesForDescription: description from: descriptionSelector [ | ||
|
||
self map | ||
at: descriptionSelector | ||
ifPresent: [ :anArray | | ||
description | ||
propertyAt: self fieldNamePropertyKey | ||
put: anArray first. | ||
|
||
anArray second ifNil: [ ^ self ]. | ||
description | ||
propertyAt: self fieldReaderPropertyKey | ||
put: [ :trimmed | | ||
anArray second | ||
cull: trimmed | ||
cull: description ] ]. | ||
] | ||
|
||
{ #category : #accessing } | ||
MACSVMappedPragmaBuilder >> fieldNamePropertyKey [ | ||
^ fieldNamePropertyKey | ||
] | ||
|
||
{ #category : #accessing } | ||
MACSVMappedPragmaBuilder >> fieldNamePropertyKey: anObject [ | ||
fieldNamePropertyKey := anObject | ||
] | ||
|
||
{ #category : #accessing } | ||
MACSVMappedPragmaBuilder >> fieldReaderPropertyKey [ | ||
^ fieldReaderPropertyKey | ||
] | ||
|
||
{ #category : #accessing } | ||
MACSVMappedPragmaBuilder >> fieldReaderPropertyKey: anObject [ | ||
fieldReaderPropertyKey := anObject | ||
] | ||
|
||
{ #category : #accessing } | ||
MACSVMappedPragmaBuilder >> map [ | ||
^ map ifNil: [ map := Dictionary new ] | ||
] | ||
|
||
{ #category : #accessing } | ||
MACSVMappedPragmaBuilder >> map: anObject [ | ||
map := anObject | ||
] | ||
|
||
{ #category : #accessing } | ||
MACSVMappedPragmaBuilder >> map: fieldName fieldTo: descriptionSelector [ | ||
|
||
self map: fieldName fieldTo: descriptionSelector using: nil | ||
] | ||
|
||
{ #category : #accessing } | ||
MACSVMappedPragmaBuilder >> map: fieldName fieldTo: descriptionSelector using: reader [ | ||
" | ||
descriptionSelector - returning the description to be used | ||
reader - typically a block, where arguments are: | ||
1) the field string input and | ||
2) the description | ||
A typical pattern is to customize the description and then use the default reader with something like this: | ||
`desc defaultCsvReader cull: input trimmed`" | ||
|
||
self map at: descriptionSelector put: { fieldName. reader } | ||
] |
Oops, something went wrong.