tabular 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a72f7305b34731c1fb2e479cfbb0a13039dfd840
4
- data.tar.gz: 11cca1a77c8687abbe9f026edee0422239ff2937
3
+ metadata.gz: d85c0fe883103fa87b589ba84cfdd385ce914133
4
+ data.tar.gz: 38277554d2f6a15b6a2b937b4dac167306b58bad
5
5
  SHA512:
6
- metadata.gz: 298c71843ce790a2f6db8ff682c6fed2665b598234e0703384805f40beb2273193dd280b90e979287635d61ce7e2c95b305fc4842ed1b9befbfc13da5ddbbc7e
7
- data.tar.gz: c522ca24515089828157fffc266450d7b45fd5993f8439ee1df67d60e7e7706b5c51b837956652edbaddb7397313262f236456cab97a4eb7e24f83069638107e
6
+ metadata.gz: b8551d9980fbd71a1a720c39405d3f3ed93bb34d1fb3b2a963efafb6196f73c8179bbaa064460a57176583ca831c1dfcbf389ae172ee9bda62a829bb1e6a7a2d
7
+ data.tar.gz: 6ad55481d57a25b1f4d502bd96488d2fbd7e136423ec86ff1877b0c13dc75307afa48d4b9ddfbd4b01583c207e39d28252d417727b16ac4d7c0a06cda1f452bc
@@ -0,0 +1,26 @@
1
+ module Tabular
2
+ class ColumnMapper
3
+ include Tabular::Blank
4
+
5
+ # Convert +key+ to normalized symbol. Subclass for more complex mapping.
6
+ def map(key)
7
+ return nil if is_blank?(key)
8
+ symbolize key
9
+ end
10
+
11
+ def symbolize(key)
12
+ begin
13
+ key.to_s.strip.gsub(/::/, '/').
14
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
15
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
16
+ tr("-", "_").
17
+ gsub(/ +/, "_").
18
+ gsub(";", "").
19
+ downcase.
20
+ to_sym
21
+ rescue
22
+ nil
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,92 @@
1
+ module Tabular
2
+ module Tables
3
+ module FileReading
4
+ # +file+ : file path as String or File
5
+ # Assumes .txt = tab-delimited, .csv = CSV, .xls = Excel. Assumes first row is the header.
6
+ # Normalizes column names to lower-case with underscores.
7
+ # +format+ : :csv, :txt, or :xls
8
+ # Returns Array of Arrays
9
+ def read(file, format = nil)
10
+ file_path = to_file_path(file)
11
+ format ||= format_from(format, file_path)
12
+
13
+ self.rows = case format
14
+ when :xls, :xlsx
15
+ read_spreadsheet file_path, format
16
+ when :txt
17
+ read_txt file_path
18
+ when :csv
19
+ read_csv file_path
20
+ else
21
+ raise "Cannot read '#{format}' format. Expected :xls, :xlsx, :txt, or :csv"
22
+ end
23
+ end
24
+
25
+ def format_from(as_option, file_path)
26
+ if as_option
27
+ as_option
28
+ else
29
+ case File.extname(file_path)
30
+ when ".xls"
31
+ :xls
32
+ when ".xlsx"
33
+ :xlsx
34
+ when ".txt"
35
+ :txt
36
+ when ".csv"
37
+ :csv
38
+ end
39
+ end
40
+ end
41
+
42
+ def to_file_path(file)
43
+ file_path = case file
44
+ when File
45
+ file.path
46
+ else
47
+ file
48
+ end
49
+
50
+ raise "Could not find '#{file_path}'" unless File.exists?(file_path)
51
+
52
+ file_path
53
+ end
54
+
55
+ def read_spreadsheet(file_path, format)
56
+ require "roo"
57
+
58
+ if format == :xls
59
+ excel = ::Roo::Excel.new(file_path)
60
+ else
61
+ excel = ::Roo::Excelx.new(file_path)
62
+ end
63
+
64
+ # Row#to_a coerces Excel data to Strings, but we want Dates and Numbers
65
+ data = []
66
+ excel.sheet(0).each do |excel_row|
67
+ data << excel_row.inject([]) { |row, cell| row << cell; row }
68
+ end
69
+ data
70
+ end
71
+
72
+ def read_txt(file_path)
73
+ require "csv"
74
+ if RUBY_VERSION < "1.9"
75
+ ::CSV.open file_path, "r", "\t"
76
+ else
77
+ CSV.read file_path, col_sep: "\t"
78
+ end
79
+ end
80
+
81
+ def read_csv(file_path)
82
+ if RUBY_VERSION < "1.9"
83
+ require "fastercsv"
84
+ FasterCSV.read file_path
85
+ else
86
+ require "csv"
87
+ CSV.read file_path
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -1,3 +1,3 @@
1
1
  module Tabular
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
data/tabular.gemspec CHANGED
@@ -19,16 +19,18 @@ Gem::Specification.new do |s|
19
19
  "LICENSE",
20
20
  "README",
21
21
  "Rakefile",
22
+ "lib/tabular/tables/file_reading.rb",
22
23
  "lib/tabular.rb",
23
24
  "lib/tabular/blank.rb",
24
25
  "lib/tabular/column.rb",
26
+ "lib/tabular/column_mapper.rb",
25
27
  "lib/tabular/columns.rb",
26
28
  "lib/tabular/keys.rb",
27
29
  "lib/tabular/renderer.rb",
28
30
  "lib/tabular/row.rb",
29
31
  "lib/tabular/table.rb",
30
- "lib/tabular/zero.rb",
31
32
  "lib/tabular/version.rb",
33
+ "lib/tabular/zero.rb",
32
34
  "tabular.gemspec"
33
35
  ]
34
36
  s.homepage = "http://github.com/scottwillson/tabular"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tabular
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Willson
@@ -41,11 +41,13 @@ files:
41
41
  - lib/tabular.rb
42
42
  - lib/tabular/blank.rb
43
43
  - lib/tabular/column.rb
44
+ - lib/tabular/column_mapper.rb
44
45
  - lib/tabular/columns.rb
45
46
  - lib/tabular/keys.rb
46
47
  - lib/tabular/renderer.rb
47
48
  - lib/tabular/row.rb
48
49
  - lib/tabular/table.rb
50
+ - lib/tabular/tables/file_reading.rb
49
51
  - lib/tabular/version.rb
50
52
  - lib/tabular/zero.rb
51
53
  - tabular.gemspec