Class: TableSchema::Table
- Inherits:
-
Object
- Object
- TableSchema::Table
- Defined in:
- lib/tableschema/table.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(csv, descriptor, csv_options: {}) ⇒ Table
constructor
A new instance of Table.
- #iter(row_limit: nil, cast: true, keyed: false) ⇒ Object
- #read(row_limit: nil, cast: true, keyed: false) ⇒ Object
- #save(target) ⇒ Object
Constructor Details
#initialize(csv, descriptor, csv_options: {}) ⇒ Table
Returns a new instance of Table.
10 11 12 13 14 15 16 |
# File 'lib/tableschema/table.rb', line 10 def initialize(csv, descriptor, csv_options: {}) = .merge(headers: true) @csv = parse_csv(csv) @headers = initialize_headers @schema = descriptor.nil? ? infer_schema : TableSchema::Schema.new(descriptor) initialize_unique_colums end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
4 5 6 |
# File 'lib/tableschema/table.rb', line 4 def headers @headers end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
4 5 6 |
# File 'lib/tableschema/table.rb', line 4 def schema @schema end |
Class Method Details
.infer_schema(csv, csv_options: {}) ⇒ Object
6 7 8 |
# File 'lib/tableschema/table.rb', line 6 def self.infer_schema(csv, csv_options: {}) TableSchema::Table.new(csv, nil, ) end |
Instance Method Details
#iter(row_limit: nil, cast: true, keyed: false) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/tableschema/table.rb', line 18 def iter(row_limit: nil, cast: true, keyed: false) unless block_given? return enum_for(:iter, row_limit: row_limit, cast: cast, keyed: keyed) end @csv.each_with_index do |row, i| break if row_limit && (row_limit <= i) if cast == true cast_values = @schema.cast_row(row) row = CSV::Row.new(@headers, cast_values) check_unique_fields(row, i) end if keyed == true yield row.to_h else yield row.fields end collect_unique_fields(row, i) end @csv.rewind end |
#read(row_limit: nil, cast: true, keyed: false) ⇒ Object
41 42 43 44 |
# File 'lib/tableschema/table.rb', line 41 def read(row_limit: nil, cast: true, keyed: false) iterator = self.iter(row_limit: row_limit, cast: cast, keyed: keyed) iterator.to_a end |
#save(target) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/tableschema/table.rb', line 46 def save(target) CSV.open(target, "wb", ) do |csv| csv << @headers self.iter{ |row| csv << row } end true end |