Class: CsvMapper::AttributeMap

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(name, index, map_context) ⇒ AttributeMap

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

#indexObject (readonly)

Returns the value of attribute index



5
6
7
# File 'lib/csv-mapper/attribute_map.rb', line 5

def index
  @index
end

#nameObject (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

#at(index) ⇒ Object

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

#map(transform = nil, &block_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



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

#parse(csv_row) ⇒ Object

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

#raw_value(csv_row) ⇒ Object

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