tabular 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/tabular/table.rb +36 -18
  3. data/tabular.gemspec +2 -2
  4. metadata +3 -6
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
data/lib/tabular/table.rb CHANGED
@@ -8,21 +8,16 @@ module Tabular
8
8
  def self.read(file_path, *options)
9
9
  raise "Could not find '#{file_path}'" unless File.exists?(file_path)
10
10
  options = extract_options(options)
11
- as = options.delete(:as)
12
11
 
13
- if as.present?
14
- format = as
15
- else
16
- format = case File.extname(file_path)
17
- when ".xls", ".xlsx"
18
- :xls
19
- when ".txt"
20
- :txt
21
- when ".csv"
22
- :csv
23
- end
24
- end
12
+ format = self.format_from(options.delete(:as), file_path)
13
+ data = read_file(file_path, format)
25
14
 
15
+ Table.new data, options
16
+ end
17
+
18
+ # +format+ : :csv, :txt, or :xls
19
+ # Returns Array of Arrays
20
+ def self.read_file(file_path, format)
26
21
  case format
27
22
  when :xls
28
23
  require "spreadsheet"
@@ -31,17 +26,25 @@ module Tabular
31
26
  Spreadsheet.open(file_path).worksheets.first.each do |excel_row|
32
27
  data << excel_row.inject([]) { |row, cell| row << cell; row }
33
28
  end
29
+ data
34
30
  when :txt
35
31
  require "csv"
36
- data = ::CSV.open(file_path, "r","\t").collect { |row| row }
32
+ if RUBY_VERSION < "1.9"
33
+ ::CSV.open(file_path, "r","\t").collect { |row| row }
34
+ else
35
+ CSV.read(file_path)
36
+ end
37
37
  when :csv
38
- require "fastercsv"
39
- data = FasterCSV.read(file_path)
38
+ if RUBY_VERSION < "1.9"
39
+ require "fastercsv"
40
+ FasterCSV.read(file_path)
41
+ else
42
+ require "csv"
43
+ CSV.read(file_path)
44
+ end
40
45
  else
41
46
  raise "Cannot read '#{format}' format. Expected :xls, :xlsx, :txt, or :csv"
42
47
  end
43
-
44
- Table.new data, options
45
48
  end
46
49
 
47
50
  # Pass data in as +rows+. Expects rows to be an Enumerable of Enumerables.
@@ -93,5 +96,20 @@ module Tabular
93
96
  {}
94
97
  end
95
98
  end
99
+
100
+ def self.format_from(as_option, file_path)
101
+ if as_option.present?
102
+ as_option
103
+ else
104
+ case File.extname(file_path)
105
+ when ".xls", ".xlsx"
106
+ :xls
107
+ when ".txt"
108
+ :txt
109
+ when ".csv"
110
+ :csv
111
+ end
112
+ end
113
+ end
96
114
  end
97
115
  end
data/tabular.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{tabular}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Scott Willson"]
12
- s.date = %q{2010-06-07}
12
+ s.date = %q{2010-06-08}
13
13
  s.description = %q{Tabular is a Ruby library for reading, writing, and manipulating CSV, tab-delimited and Excel data.}
14
14
  s.email = %q{scott.willson@gmail.cpm}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tabular
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 0
9
- - 2
10
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
11
10
  platform: ruby
12
11
  authors:
13
12
  - Scott Willson
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-06-07 00:00:00 -07:00
17
+ date: 2010-06-08 00:00:00 -07:00
19
18
  default_executable:
20
19
  dependencies: []
21
20
 
@@ -64,7 +63,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
64
63
  requirements:
65
64
  - - ">="
66
65
  - !ruby/object:Gem::Version
67
- hash: 3
68
66
  segments:
69
67
  - 0
70
68
  version: "0"
@@ -73,7 +71,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
71
  requirements:
74
72
  - - ">="
75
73
  - !ruby/object:Gem::Version
76
- hash: 3
77
74
  segments:
78
75
  - 0
79
76
  version: "0"