Class: CsvMapper::AttributeMap
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
294 295 296 |
# File 'lib/csv-mapper.rb', line 294 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.
291 292 293 |
# File 'lib/csv-mapper.rb', line 291 def index @index end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
291 292 293 |
# File 'lib/csv-mapper.rb', line 291 def name @name end |
Instance Method Details
#at(index) ⇒ Object
Set the index that this map is targeting.
Returns this AttributeMap for chainability
301 302 303 304 |
# File 'lib/csv-mapper.rb', line 301 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
311 312 313 314 |
# File 'lib/csv-mapper.rb', line 311 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)
317 318 319 |
# File 'lib/csv-mapper.rb', line 317 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.
322 323 324 |
# File 'lib/csv-mapper.rb', line 322 def raw_value(csv_row) csv_row[self.index] end |