Class: Tabulator::Reader::Worksheet
- Inherits:
-
Object
- Object
- Tabulator::Reader::Worksheet
- Defined in:
- lib/tabulator/reader.rb
Class Method Summary collapse
Instance Method Summary collapse
- #apply(target) ⇒ Object
-
#initialize(rows) ⇒ Worksheet
constructor
A new instance of Worksheet.
- #only(*cols) ⇒ Object
- #reject ⇒ Object
- #save(path) ⇒ Object
- #to_a ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(rows) ⇒ Worksheet
Returns a new instance of Worksheet.
39 40 41 |
# File 'lib/tabulator/reader.rb', line 39 def initialize rows @rows = rows end |
Class Method Details
.build(rows, **options) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/tabulator/reader.rb', line 19 def self.build rows, ** header = [:header] || 0 skip = [:skip] || header + 1 rejected_rows = *[:reject] rejected_rows.each { |index_definition| next rows.delete_if &index_definition if index_definition.is_a? Proc rows.slice! index_definition } header_row = rows[header].map { |raw_header_col| I18n.transliterate(raw_header_col.strip.gsub(/\s/, '_')).downcase.to_sym } new rows.drop(skip).map { |row| header_row.zip(row).to_h } end |
Instance Method Details
#apply(target) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/tabulator/reader.rb', line 51 def apply target self.class.new to_a.map { |row| row[target] = yield row[target], row row } end |
#only(*cols) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/tabulator/reader.rb', line 43 def only *cols self.class.new to_a.map { |row| row.select { |title, _| cols.include? title } } end |
#reject ⇒ Object
58 59 60 61 62 |
# File 'lib/tabulator/reader.rb', line 58 def reject self.class.new to_a.reject { |row| yield row } end |
#save(path) ⇒ Object
72 73 74 75 76 |
# File 'lib/tabulator/reader.rb', line 72 def save path File.open(path, "w") { |file| file.write to_json } end |
#to_a ⇒ Object
64 65 66 |
# File 'lib/tabulator/reader.rb', line 64 def to_a Marshal.load(Marshal.dump(@rows)) end |
#to_json ⇒ Object
68 69 70 |
# File 'lib/tabulator/reader.rb', line 68 def to_json to_a.to_json() end |