Class: CsvMapper::AttributeMap
- Inherits:
-
Object
- Object
- CsvMapper::AttributeMap
- Defined in:
- lib/csv-mapper.rb
Overview
A CsvMapper::AttributeMap contains the instructions to parse a value from a CSV row and to know the name of the attribute it is targeting.
Instance Attribute Summary collapse
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#at(index) ⇒ Object
Set the index that this map is targeting.
-
#initialize(name, index, map_context) ⇒ AttributeMap
constructor
Creates a new instance using the provided attribute
name
, CSV rowindex
, and evaluationmap_context
. -
#map(transform) ⇒ Object
Provide a lambda or the symbol name of a method on this map’s evaluation context to be used when parsing the value from a CSV row.
-
#parse(csv_row) ⇒ Object
Given a CSV row, return the value at this AttributeMap’s index using any provided map transforms (see map).
-
#raw_value(csv_row) ⇒ Object
Access the raw value of the CSV row without any map transforms applied.
Constructor Details
#initialize(name, index, map_context) ⇒ AttributeMap
Creates a new instance using the provided attribute name
, CSV row index
, and evaluation map_context
258 259 260 |
# File 'lib/csv-mapper.rb', line 258 def initialize(name, index, map_context) @name, @index, @map_context = name, index, map_context end |
Instance Attribute Details
#index ⇒ Object (readonly)
Returns the value of attribute index.
255 256 257 |
# File 'lib/csv-mapper.rb', line 255 def index @index end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
255 256 257 |
# File 'lib/csv-mapper.rb', line 255 def name @name end |
Instance Method Details
#at(index) ⇒ Object
Set the index that this map is targeting.
Returns this AttributeMap for chainability
265 266 267 268 |
# File 'lib/csv-mapper.rb', line 265 def at(index) @index = index self end |
#map(transform) ⇒ Object
Provide a lambda or the symbol name of a method on this map’s evaluation context to be used when parsing the value from a CSV row.
Both the lambda or the method provided should accept a single row
parameter
Returns this AttributeMap for chainability
275 276 277 278 |
# File 'lib/csv-mapper.rb', line 275 def map(transform) @transformer = transform self end |
#parse(csv_row) ⇒ Object
Given a CSV row, return the value at this AttributeMap’s index using any provided map transforms (see map)
281 282 283 |
# File 'lib/csv-mapper.rb', line 281 def parse(csv_row) @transformer ? parse_transform(csv_row) : csv_row[self.index] end |
#raw_value(csv_row) ⇒ Object
Access the raw value of the CSV row without any map transforms applied.
286 287 288 |
# File 'lib/csv-mapper.rb', line 286 def raw_value(csv_row) csv_row[self.index] end |