Class: CsvMapper::AttributeMap
- Inherits:
-
Object
- Object
- CsvMapper::AttributeMap
- Defined in:
- lib/csv-mapper/attribute_map.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)
-
- (Object) index
readonly
Returns the value of attribute index.
-
- (Object) name
readonly
Returns the value of attribute name.
Instance Method Summary (collapse)
-
- (Object) at(index)
Set the index that this map is targeting.
-
- (AttributeMap) initialize(name, index, map_context)
constructor
Creates a new instance using the provided attribute name, CSV row index, and evaluation map_context.
-
- (Object) map(transform = nil, &block_transform)
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.
-
- (Object) parse(csv_row)
Given a CSV row, return the value at this AttributeMap's index using any provided map transforms (see map).
-
- (Object) raw_value(csv_row)
Access the raw value of the CSV row without any map transforms applied.
Constructor Details
- (AttributeMap) initialize(name, index, map_context)
Creates a new instance using the provided attribute name, CSV row index, and evaluation map_context
8 9 10 |
# File 'lib/csv-mapper/attribute_map.rb', line 8 def initialize(name, index, map_context) @name, @index, @map_context = name, index, map_context end |
Instance Attribute Details
- (Object) index (readonly)
Returns the value of attribute index
5 6 7 |
# File 'lib/csv-mapper/attribute_map.rb', line 5 def index @index end |
- (Object) name (readonly)
Returns the value of attribute name
5 6 7 |
# File 'lib/csv-mapper/attribute_map.rb', line 5 def name @name end |
Instance Method Details
- (Object) at(index)
Set the index that this map is targeting.
Returns this AttributeMap for chainability
15 16 17 18 |
# File 'lib/csv-mapper/attribute_map.rb', line 15 def at(index) @index = index self end |
- (Object) map(transform = nil, &block_transform)
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
25 26 27 28 |
# File 'lib/csv-mapper/attribute_map.rb', line 25 def map(transform=nil, &block_transform) @transformer = block_transform || transform self end |
- (Object) parse(csv_row)
Given a CSV row, return the value at this AttributeMap's index using any provided map transforms (see map)
31 32 33 |
# File 'lib/csv-mapper/attribute_map.rb', line 31 def parse(csv_row) @transformer ? parse_transform(csv_row) : raw_value(csv_row) end |
- (Object) raw_value(csv_row)
Access the raw value of the CSV row without any map transforms applied.
36 37 38 |
# File 'lib/csv-mapper/attribute_map.rb', line 36 def raw_value(csv_row) csv_row[self.index] end |