tabular 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/tabular.rb +2 -3
- data/lib/tabular/blank.rb +21 -0
- data/lib/tabular/column.rb +3 -1
- data/lib/tabular/columns.rb +3 -2
- data/lib/tabular/table.rb +4 -2
- data/lib/tabular/zero.rb +18 -0
- data/tabular.gemspec +4 -4
- data/test/zero_test.rb +16 -14
- metadata +5 -5
- data/lib/tabular/support/object.rb +0 -59
- data/lib/tabular/support/zero.rb +0 -29
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.2
|
data/lib/tabular.rb
CHANGED
@@ -2,13 +2,12 @@ require "rubygems"
|
|
2
2
|
|
3
3
|
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib")
|
4
4
|
|
5
|
+
require "tabular/blank"
|
5
6
|
require "tabular/keys"
|
7
|
+
require "tabular/zero"
|
6
8
|
|
7
9
|
require "tabular/column"
|
8
10
|
require "tabular/columns"
|
9
11
|
require "tabular/renderer"
|
10
12
|
require "tabular/row"
|
11
13
|
require "tabular/table"
|
12
|
-
|
13
|
-
require "tabular/support/object"
|
14
|
-
require "tabular/support/zero"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Tabular
|
2
|
+
# Don't mess with Object
|
3
|
+
module Blank
|
4
|
+
def is_blank?(object)
|
5
|
+
case object
|
6
|
+
when NilClass
|
7
|
+
true
|
8
|
+
when FalseClass
|
9
|
+
true
|
10
|
+
when TrueClass
|
11
|
+
true
|
12
|
+
when String
|
13
|
+
object !~ /\S/
|
14
|
+
when Numeric
|
15
|
+
false
|
16
|
+
else
|
17
|
+
object.respond_to?(:empty?) ? object.empty? : !object
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/tabular/column.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module Tabular
|
2
2
|
class Column
|
3
|
+
include Tabular::Blank
|
4
|
+
|
3
5
|
attr_reader :key, :column_type
|
4
6
|
|
5
7
|
# +table+ -- parent Table
|
@@ -69,7 +71,7 @@ module Tabular
|
|
69
71
|
private
|
70
72
|
|
71
73
|
def symbolize(key)
|
72
|
-
return nil if key
|
74
|
+
return nil if is_blank?(key)
|
73
75
|
|
74
76
|
begin
|
75
77
|
key.to_s.strip.gsub(/::/, '/').
|
data/lib/tabular/columns.rb
CHANGED
@@ -2,6 +2,7 @@ module Tabular
|
|
2
2
|
# The Table's header: a list of Columns.
|
3
3
|
class Columns
|
4
4
|
include Enumerable
|
5
|
+
include Tabular::Blank
|
5
6
|
include Tabular::Keys
|
6
7
|
|
7
8
|
attr_accessor :renderer
|
@@ -19,7 +20,7 @@ module Tabular
|
|
19
20
|
@columns = nil
|
20
21
|
@columns = names.map do |column|
|
21
22
|
new_column = Tabular::Column.new(table, self, column, @columns_map)
|
22
|
-
unless new_column.key
|
23
|
+
unless is_blank?(new_column.key)
|
23
24
|
@column_indexes[new_column.key] = index
|
24
25
|
@columns_by_key[new_column.key] = new_column
|
25
26
|
end
|
@@ -52,7 +53,7 @@ module Tabular
|
|
52
53
|
# Add a new Column with +key+
|
53
54
|
def <<(key)
|
54
55
|
column = Column.new(@table, self, key, @columns_map)
|
55
|
-
unless column.key
|
56
|
+
unless is_blank?(column.key) || has_key?(key)
|
56
57
|
@column_indexes[column.key] = @columns.size
|
57
58
|
@column_indexes[@columns.size] = column
|
58
59
|
@columns_by_key[column.key] = column
|
data/lib/tabular/table.rb
CHANGED
@@ -2,7 +2,9 @@ module Tabular
|
|
2
2
|
# Simple Enumerable list of Hashes. Use Table.read(file_path) to read file. Can also create a Table with Table.new. Either
|
3
3
|
# pass in data or set options and then call row=.
|
4
4
|
class Table
|
5
|
+
include Tabular::Blank
|
5
6
|
include Tabular::Keys
|
7
|
+
include Tabular::Zero
|
6
8
|
|
7
9
|
attr_reader :options, :rows
|
8
10
|
attr_accessor :row_mapper
|
@@ -126,7 +128,7 @@ module Tabular
|
|
126
128
|
# Remove all columns that only contain a blank string, zero, or nil
|
127
129
|
def delete_blank_columns!
|
128
130
|
columns.map(&:key).each do |key|
|
129
|
-
if rows.all? { |row| row[key]
|
131
|
+
if rows.all? { |row| is_blank?(row[key]) || is_zero?(row[key]) }
|
130
132
|
delete_column key
|
131
133
|
end
|
132
134
|
end
|
@@ -190,7 +192,7 @@ module Tabular
|
|
190
192
|
end
|
191
193
|
|
192
194
|
def self.format_from(as_option, file_path)
|
193
|
-
if as_option
|
195
|
+
if as_option
|
194
196
|
as_option
|
195
197
|
else
|
196
198
|
case File.extname(file_path)
|
data/lib/tabular/zero.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Tabular
|
2
|
+
module Zero
|
3
|
+
def is_zero?(object)
|
4
|
+
if object.respond_to?(:zero?)
|
5
|
+
return object.zero?
|
6
|
+
end
|
7
|
+
|
8
|
+
case object
|
9
|
+
when NilClass, FalseClass, TrueClass
|
10
|
+
false
|
11
|
+
when String
|
12
|
+
object == "0" || object[/^0+\.0+$/]
|
13
|
+
else
|
14
|
+
false
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/tabular.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "tabular"
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.2"
|
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 = "2013-05-
|
12
|
+
s.date = "2013-05-20"
|
13
13
|
s.description = "Tabular is a Ruby library for reading, writing, and manipulating CSV, tab-delimited and Excel data."
|
14
14
|
s.email = "scott.willson@gmail.cpm"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -24,14 +24,14 @@ Gem::Specification.new do |s|
|
|
24
24
|
"Rakefile",
|
25
25
|
"VERSION",
|
26
26
|
"lib/tabular.rb",
|
27
|
+
"lib/tabular/blank.rb",
|
27
28
|
"lib/tabular/column.rb",
|
28
29
|
"lib/tabular/columns.rb",
|
29
30
|
"lib/tabular/keys.rb",
|
30
31
|
"lib/tabular/renderer.rb",
|
31
32
|
"lib/tabular/row.rb",
|
32
|
-
"lib/tabular/support/object.rb",
|
33
|
-
"lib/tabular/support/zero.rb",
|
34
33
|
"lib/tabular/table.rb",
|
34
|
+
"lib/tabular/zero.rb",
|
35
35
|
"tabular.gemspec",
|
36
36
|
"test/column_test.rb",
|
37
37
|
"test/columns_test.rb",
|
data/test/zero_test.rb
CHANGED
@@ -1,21 +1,23 @@
|
|
1
1
|
require "helper"
|
2
2
|
|
3
3
|
class ZeroTest < Test::Unit::TestCase
|
4
|
+
include Tabular::Zero
|
5
|
+
|
4
6
|
def test_zero
|
5
|
-
assert 0
|
6
|
-
assert (0.0)
|
7
|
-
assert "0"
|
8
|
-
assert "0.0"
|
9
|
-
assert "00000.00000"
|
7
|
+
assert is_zero?(0)
|
8
|
+
assert is_zero?(0.0)
|
9
|
+
assert is_zero?("0")
|
10
|
+
assert is_zero?("0.0")
|
11
|
+
assert is_zero?("00000.00000")
|
10
12
|
|
11
|
-
assert !(1
|
12
|
-
assert !(-1
|
13
|
-
assert !(
|
14
|
-
assert !("1"
|
15
|
-
assert !("ABC"
|
16
|
-
assert !(nil
|
17
|
-
assert !(""
|
18
|
-
assert !(false
|
19
|
-
assert !(true
|
13
|
+
assert !is_zero?(1)
|
14
|
+
assert !is_zero?(-1)
|
15
|
+
assert !is_zero?(0.1)
|
16
|
+
assert !is_zero?("1")
|
17
|
+
assert !is_zero?("ABC")
|
18
|
+
assert !is_zero?(nil)
|
19
|
+
assert !is_zero?("")
|
20
|
+
assert !is_zero?(false)
|
21
|
+
assert !is_zero?(true)
|
20
22
|
end
|
21
23
|
end
|
metadata
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
name: tabular
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.2.
|
5
|
+
version: 0.2.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Scott Willson
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: ruby-ole
|
@@ -75,14 +75,14 @@ files:
|
|
75
75
|
- Rakefile
|
76
76
|
- VERSION
|
77
77
|
- lib/tabular.rb
|
78
|
+
- lib/tabular/blank.rb
|
78
79
|
- lib/tabular/column.rb
|
79
80
|
- lib/tabular/columns.rb
|
80
81
|
- lib/tabular/keys.rb
|
81
82
|
- lib/tabular/renderer.rb
|
82
83
|
- lib/tabular/row.rb
|
83
|
-
- lib/tabular/support/object.rb
|
84
|
-
- lib/tabular/support/zero.rb
|
85
84
|
- lib/tabular/table.rb
|
85
|
+
- lib/tabular/zero.rb
|
86
86
|
- tabular.gemspec
|
87
87
|
- test/column_test.rb
|
88
88
|
- test/columns_test.rb
|
@@ -105,7 +105,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
105
105
|
requirements:
|
106
106
|
- - ! '>='
|
107
107
|
- !ruby/object:Gem::Version
|
108
|
-
hash:
|
108
|
+
hash: 1566100413199475638
|
109
109
|
segments:
|
110
110
|
- 0
|
111
111
|
version: '0'
|
@@ -1,59 +0,0 @@
|
|
1
|
-
# Copied from Rails' ActiveSupport
|
2
|
-
class Object
|
3
|
-
# An object is blank if it's false, empty, or a whitespace string.
|
4
|
-
# For example, "", " ", +nil+, [], and {} are blank.
|
5
|
-
#
|
6
|
-
# This simplifies
|
7
|
-
#
|
8
|
-
# if !address.nil? && !address.empty?
|
9
|
-
#
|
10
|
-
# to
|
11
|
-
#
|
12
|
-
# if !address.blank?
|
13
|
-
def blank?
|
14
|
-
respond_to?(:empty?) ? empty? : !self
|
15
|
-
end
|
16
|
-
|
17
|
-
# An object is present if it's not blank.
|
18
|
-
def present?
|
19
|
-
!blank?
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
class NilClass #:nodoc:
|
24
|
-
def blank?
|
25
|
-
true
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
class FalseClass #:nodoc:
|
30
|
-
def blank?
|
31
|
-
true
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
class TrueClass #:nodoc:
|
36
|
-
def blank?
|
37
|
-
false
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
class Array #:nodoc:
|
42
|
-
alias_method :blank?, :empty?
|
43
|
-
end
|
44
|
-
|
45
|
-
class Hash #:nodoc:
|
46
|
-
alias_method :blank?, :empty?
|
47
|
-
end
|
48
|
-
|
49
|
-
class String #:nodoc:
|
50
|
-
def blank?
|
51
|
-
self !~ /\S/
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
class Numeric #:nodoc:
|
56
|
-
def blank?
|
57
|
-
false
|
58
|
-
end
|
59
|
-
end
|
data/lib/tabular/support/zero.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
class Object
|
2
|
-
def zero?
|
3
|
-
false
|
4
|
-
end
|
5
|
-
end
|
6
|
-
|
7
|
-
class NilClass #:nodoc:
|
8
|
-
def zero?
|
9
|
-
false
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class FalseClass #:nodoc:
|
14
|
-
def zero?
|
15
|
-
false
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
class TrueClass #:nodoc:
|
20
|
-
def zero?
|
21
|
-
false
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
class String #:nodoc:
|
26
|
-
def zero?
|
27
|
-
self == "0" || self[/^0+\.0+$/]
|
28
|
-
end
|
29
|
-
end
|