class JsonTableSchema::Table
Attributes
schema[R]
Public Class Methods
infer_schema(csv, opts = {})
click to toggle source
# File lib/jsontableschema/table.rb, line 6 def self.infer_schema(csv, opts = {}) JsonTableSchema::Table.new(csv, nil, opts) end
new(csv, descriptor, opts = {})
click to toggle source
# File lib/jsontableschema/table.rb, line 10 def initialize(csv, descriptor, opts = {}) @opts = opts @csv = parse_csv(csv) @schema = descriptor.nil? ? infer_schema(@csv) : JsonTableSchema::Schema.new(descriptor) end
Public Instance Methods
csv_options()
click to toggle source
# File lib/jsontableschema/table.rb, line 21 def csv_options (@opts[:csv_options] || {}).merge(headers: true) end
parse_csv(csv)
click to toggle source
# File lib/jsontableschema/table.rb, line 16 def parse_csv(csv) csv = csv.is_a?(Array) ? StringIO.new(array_to_csv csv) : open(csv) CSV.new(csv, csv_options) end
rows(opts = {})
click to toggle source
# File lib/jsontableschema/table.rb, line 25 def rows(opts = {}) fail_fast = opts[:fail_fast] || opts[:fail_fast].nil? converted = @schema.cast_rows(@csv, fail_fast, opts[:limit]) opts[:keyed] ? coverted_to_hash(@csv.headers, converted) : converted end
Private Instance Methods
array_to_csv(array)
click to toggle source
# File lib/jsontableschema/table.rb, line 33 def array_to_csv(array) array.map { |row| row.to_csv(row_sep: nil) }.join("\r\n") end
coverted_to_hash(headers, array)
click to toggle source
# File lib/jsontableschema/table.rb, line 37 def coverted_to_hash(headers, array) array.map do |row| Hash[row.map.with_index { |col, i| [headers[i], col] }] end end
infer_schema(csv)
click to toggle source
# File lib/jsontableschema/table.rb, line 43 def infer_schema(csv) headers = csv.first.to_h.keys csv.rewind inferer = JsonTableSchema::Infer.new(headers, csv) inferer.schema end