module JsonTableSchema::Data

Attributes

errors[R]

Public Instance Methods

cast_row(row, fail_fast = true) click to toggle source
# File lib/jsontableschema/data.rb, line 25
def cast_row(row, fail_fast = true)
  @errors ||= []
  raise_header_error(row) if row.count != fields.count
  fields.each_with_index do |field,i|
    row[i] = cast_column(field, row[i], fail_fast)
  end
  check_for_errors
  row
end
Also aliased as: convert_row
cast_rows(rows, fail_fast = true, limit = nil) click to toggle source
# File lib/jsontableschema/data.rb, line 6
def cast_rows(rows, fail_fast = true, limit = nil)
  @errors ||= []
  parsed_rows = []
  rows.each_with_index do |r, i|
    begin
      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
  parsed_rows
end
Also aliased as: convert
convert(rows, fail_fast = true, limit = nil)
Alias for: cast_rows
convert_row(row, fail_fast = true)
Alias for: cast_row

Private Instance Methods

cast_column(field, col, fail_fast) click to toggle source
# File lib/jsontableschema/data.rb, line 47
def cast_column(field, col, fail_fast)
  field.cast_value(col)
rescue Exception => e
  if fail_fast == true
    raise e
  else
    @errors << e
  end
end
Also aliased as: convert_column
check_for_errors() click to toggle source
# File lib/jsontableschema/data.rb, line 43
def check_for_errors
  raise(JsonTableSchema::MultipleInvalid.new("There were errors parsing the data")) if @errors.count > 0
end
convert_column(field, col, fail_fast)
Alias for: cast_column
raise_header_error(row) click to toggle source
# File lib/jsontableschema/data.rb, line 39
def raise_header_error(row)
  raise(JsonTableSchema::ConversionError.new("The number of items to convert (#{row.count}) does not match the number of headers in the schema (#{fields.count})"))
end