Created
April 6, 2011 04:52
-
-
Save pillowfactory/905161 to your computer and use it in GitHub Desktop.
Work in progress example of desired use/functionality of CsvMapper
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
# Import with CSV Mapper | |
CsvMapper.import('path') do | |
start_at_row 1 # Skip the first row (probably a header row) | |
stop_at_row 10 # Stop parsing on the 10th row | |
map_to Business do | |
business_name # Parse to String by default | |
net_worth :type => Float # Parse column value Float | |
net_worth.to_f # Parse column value Float | |
# Map an aggregate model that is an instance of Address | |
address.map_to Address do | |
line_1(3) # Map explicitly by column index | |
line_2("Address Line 2") # Map explicitly by column header name (assumes zeroth row is header) | |
city(/^City$/) # Map explicitly by column header matching provided Regexp (assumes zeroth row is header) | |
state.map {|value| value.to_s.upcase} # Parse the raw value using the given block | |
zip | |
end | |
# Map a collection of employees that are instances of Employee | |
employees.map_to Employee, :count => 2 do | |
first_name | |
last_name | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment