Created
September 17, 2010 02:49
-
-
Save pillowfactory/583573 to your computer and use it in GitHub Desktop.
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
business name | net worth | address line 1 | address line 2 | city | state | zip | |
---|---|---|---|---|---|---|---|
Trader Joes | 50000000.00 | 123 Main St. | Suite 4 | Kansas City | mo | 64055 | |
Price Chopper | 30000000.00 | 456 Cherry | Kansas City | mo | 64055 |
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
# Given | |
class Business | |
attr_accessor :name, :net_worth | |
attr_accessor :line_1, :line_2, :city, :state, :zip | |
end | |
# Import with CSV Mapper | |
CsvMapper.import('path_to_file.csv') do | |
map_to Business | |
start_at_row 1 # Skip the first row (probably a header row) | |
stop_at_row 10 # Stop parsing on the 10th row | |
name # Parse all attribute values to String | |
net_worth | |
line_1 | |
line_2 | |
city | |
state.map {|row, index| row[index].to_s.upcase } # Parse the raw value using the given block | |
zip | |
end | |
# Results - An array of two instances of a Business |
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
CsvMapper.import('path') do |business| | |
map_to Business | |
start_at_row 1 # Skip the first row (probably a header row) | |
stop_at_row 10 # Stop parsing on the 10th row | |
business.name | |
business.net_worth :type => Float | |
business.address.map Address do |address| | |
address.line_1 | |
address.line_2 | |
address.city | |
address.state.map {|value| value.to_s.upcase} | |
address.zip | |
end | |
end |
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
# Given | |
class Business | |
attr_accessor :name, :net_worth | |
attr_accessor :address | |
attr_accessor :employees | |
end | |
class Address | |
attr_accessor :line_1, :line_2, :city, :state, :zip | |
end | |
class Employee | |
attr_accessor :first_name, :last_name | |
end | |
# Import with CSV Mapper | |
CsvMapper.import('path') do |config| | |
config.map_to Business | |
config.start_at_row 1 # Skip the first row (probably a header row) | |
config.stop_at_row 10 # Stop parsing on the 10th row | |
name # Parse to String by default | |
net_worth :type => Float # Parse column value Float | |
# Map an aggregate model that is an instance of Address | |
address.map_to Address do | |
line_1 | |
line_2 | |
city | |
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 | |
# Results - An array of two instances of a Business; each with a fully populated Address instance as the value of the address attribute |
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 | |
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 | |
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 |
Author
pillowfactory
commented
Sep 17, 2010
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment