Class: Tabulator::Reader::Worksheet

Inherits:
Object
  • Object
show all
Defined in:
lib/tabulator/reader.rb

Class Method Summary collapse

Instance Method Summary collapse

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, **options
  header = options[:header] || 0
  skip = options[:skip] || header + 1

  rejected_rows = *options[: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

#rejectObject



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_aObject



64
65
66
# File 'lib/tabulator/reader.rb', line 64

def to_a
  Marshal.load(Marshal.dump(@rows))
end

#to_jsonObject



68
69
70
# File 'lib/tabulator/reader.rb', line 68

def to_json
  to_a.to_json(json_dump_options)
end