Skip to content

Instantly share code, notes, and snippets.

@pillowfactory
Created September 17, 2010 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pillowfactory/583573 to your computer and use it in GitHub Desktop.
Save pillowfactory/583573 to your computer and use it in GitHub Desktop.
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
# 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
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
# 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
# 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
@pillowfactory
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment