Skip to content

Commit

Permalink
Merge pull request #12 from theodi/feature-stream-csv
Browse files Browse the repository at this point in the history
Stream CSV
  • Loading branch information
Sam Pikesley committed Oct 12, 2016
2 parents 74bd239 + 3bdb635 commit 8487aee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
11 changes: 7 additions & 4 deletions lib/jsontableschema/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ module Data

attr_reader :errors

def cast_rows(rows, fail_fast = true)
def cast_rows(rows, fail_fast = true, limit = nil)
@errors ||= []
rows.map! do |r|
parsed_rows = []
rows.each_with_index do |r, i|
begin
cast_row(r, fail_fast)
break if limit && (limit <= i)
r = r.fields if r.class == CSV::Row
parsed_rows << cast_row(r, fail_fast)
rescue MultipleInvalid, ConversionError => e
raise e if fail_fast == true
@errors << e if e.is_a?(ConversionError)
end
end
check_for_errors
rows
parsed_rows
end

alias_method :convert, :cast_rows
Expand Down
3 changes: 1 addition & 2 deletions lib/jsontableschema/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def csv_options

def rows(opts = {})
fail_fast = opts[:fail_fast] || opts[:fail_fast].nil?
rows = opts[:limit] ? @csv.to_a.drop(1).take(opts[:limit]) : @csv.to_a.drop(1)
converted = @schema.cast_rows(rows, fail_fast)
converted = @schema.cast_rows(@csv, fail_fast, opts[:limit])
opts[:keyed] ? coverted_to_hash(@csv.headers, converted) : converted
end

Expand Down

0 comments on commit 8487aee

Please sign in to comment.