Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions lib/csv-mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,49 @@ def map_csv(&map_block)
# <tt>:map</tt>:: Specify an instance of a RowMap to take presidence over a given block defintion.
#
def import(data, options={}, &map_block)
csv_data = options[:type] == :io ? data : File.new(data, 'r')

config = { :type => :file_path,
:map => map_csv_with_data(csv_data, &map_block) }.merge!(options)

map = config[:map]

results = []
FasterCSV.new(csv_data, map.parser_options ).each_with_index do |row, i|
results << map.parse(row) if i >= map.start_at_row && i <= map.stop_at_row
build_results(data, map_block, options) do |result|
results << result
end

results
end
end

# Process works the same was as import except that it returns a count of
# imported rows instead of the results. This is useful for processing large
# files.
def process(data, options={}, &map_block)
count = 0
build_results(data, map_block, options) do |result|
count = count.succ
end

count
end

protected
# Create a new RowMap instance from the definition in the given block and pass the csv_data.
def map_csv_with_data(csv_data, &map_block) # :nodoc:
CsvMapper::RowMap.new(self, csv_data, &map_block)
end


private
# Process each row and yield result to caller
def build_results(data, map_block, options={})
csv_data = options[:type] == :io ? data : File.new(data, 'r')

config = { :type => :file_path,
:map => map_csv_with_data(csv_data, &map_block) }.merge!(options)

map = config[:map]

FasterCSV.new(csv_data, map.parser_options ).each_with_index do |row, i|
if i >= map.start_at_row && i <= map.stop_at_row
yield map.parse(row)
end
end
end

extend self
end

require 'csv-mapper/row_map'
require 'csv-mapper/row_map'
7 changes: 7 additions & 0 deletions spec/csv-mapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ def upcase_name(row)

results[0].first_name.should == 'JOHN'
end

it "should allow us to not capture the output and return a row count instead" do
io = "foo,bar,00,01\na,b,c,d"
results = @mapped.process(io, :type => :io, :capture_output => false)

results.should == 2
end
end

describe "extended" do
Expand Down