tb 0.3 → 0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +2 -1
- data/lib/tb.rb +7 -3
- data/lib/tb/basic.rb +1 -1
- data/lib/tb/cmd_cat.rb +1 -3
- data/lib/tb/cmd_consecutive.rb +4 -6
- data/lib/tb/cmd_crop.rb +5 -7
- data/lib/tb/cmd_cross.rb +51 -49
- data/lib/tb/cmd_cut.rb +2 -6
- data/lib/tb/cmd_git_log.rb +20 -11
- data/lib/tb/cmd_grep.rb +1 -3
- data/lib/tb/cmd_group.rb +18 -44
- data/lib/tb/cmd_gsub.rb +2 -4
- data/lib/tb/cmd_join.rb +1 -3
- data/lib/tb/cmd_ls.rb +8 -15
- data/lib/tb/cmd_mheader.rb +3 -4
- data/lib/tb/cmd_nest.rb +4 -9
- data/lib/tb/cmd_newfield.rb +1 -3
- data/lib/tb/cmd_rename.rb +2 -4
- data/lib/tb/cmd_shape.rb +2 -3
- data/lib/tb/cmd_sort.rb +3 -5
- data/lib/tb/cmd_svn_log.rb +3 -5
- data/lib/tb/cmd_tar_tvf.rb +2 -4
- data/lib/tb/cmd_to_csv.rb +1 -1
- data/lib/tb/cmd_unnest.rb +1 -3
- data/lib/tb/cmdutil.rb +57 -135
- data/lib/tb/csv.rb +11 -54
- data/lib/tb/customcmp.rb +41 -0
- data/lib/tb/customeq.rb +41 -0
- data/lib/tb/enumerable.rb +225 -435
- data/lib/tb/enumerator.rb +22 -14
- data/lib/tb/ex_enumerable.rb +659 -0
- data/lib/tb/ex_enumerator.rb +102 -0
- data/lib/tb/fileenumerator.rb +2 -2
- data/lib/tb/func.rb +141 -0
- data/lib/tb/json.rb +1 -1
- data/lib/tb/reader.rb +4 -4
- data/lib/tb/search.rb +2 -4
- data/lib/tb/zipper.rb +60 -0
- data/test/test_cmd_cat.rb +40 -0
- data/test/test_cmd_git_log.rb +116 -0
- data/test/test_cmd_ls.rb +90 -0
- data/test/test_cmd_svn_log.rb +87 -0
- data/test/test_cmd_to_csv.rb +14 -0
- data/test/test_cmdutil.rb +25 -10
- data/test/test_csv.rb +10 -0
- data/test/test_customcmp.rb +14 -0
- data/test/test_customeq.rb +20 -0
- data/test/{test_enumerable.rb → test_ex_enumerable.rb} +181 -3
- data/test/test_search.rb +2 -10
- data/test/test_tbenum.rb +3 -3
- data/test/test_zipper.rb +22 -0
- metadata +20 -8
- data/lib/tb/enum.rb +0 -294
- data/lib/tb/pairs.rb +0 -227
- data/test/test_pairs.rb +0 -122
data/lib/tb/pairs.rb
DELETED
@@ -1,227 +0,0 @@
|
|
1
|
-
# Copyright (C) 2012 Tanaka Akira <akr@fsij.org>
|
2
|
-
#
|
3
|
-
# Redistribution and use in source and binary forms, with or without
|
4
|
-
# modification, are permitted provided that the following conditions
|
5
|
-
# are met:
|
6
|
-
#
|
7
|
-
# 1. Redistributions of source code must retain the above copyright
|
8
|
-
# notice, this list of conditions and the following disclaimer.
|
9
|
-
# 2. Redistributions in binary form must reproduce the above
|
10
|
-
# copyright notice, this list of conditions and the following
|
11
|
-
# disclaimer in the documentation and/or other materials provided
|
12
|
-
# with the distribution.
|
13
|
-
# 3. The name of the author may not be used to endorse or promote
|
14
|
-
# products derived from this software without specific prior
|
15
|
-
# written permission.
|
16
|
-
#
|
17
|
-
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
18
|
-
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
-
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
20
|
-
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
21
|
-
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
22
|
-
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
23
|
-
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
24
|
-
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
25
|
-
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
26
|
-
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
27
|
-
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28
|
-
|
29
|
-
require 'weakref'
|
30
|
-
|
31
|
-
class Tb::Pairs
|
32
|
-
def self.get_key2index(keys)
|
33
|
-
wm = (Thread.current[:tb_pairs_frozen_info] ||= {})
|
34
|
-
w = wm[keys]
|
35
|
-
if w
|
36
|
-
begin
|
37
|
-
return w.__getobj__
|
38
|
-
rescue WeakRef::RefError
|
39
|
-
end
|
40
|
-
end
|
41
|
-
keys = keys.dup.freeze
|
42
|
-
k2i = {}
|
43
|
-
keys.each_with_index {|k, i|
|
44
|
-
k2i[k] = i
|
45
|
-
}
|
46
|
-
k2i.freeze
|
47
|
-
info = [keys, k2i]
|
48
|
-
w = WeakRef.new(info)
|
49
|
-
wm[keys.dup] = w
|
50
|
-
info
|
51
|
-
end
|
52
|
-
|
53
|
-
include Enumerable
|
54
|
-
|
55
|
-
def initialize(pairs)
|
56
|
-
keys = []
|
57
|
-
vals = []
|
58
|
-
pairs.each {|k, v|
|
59
|
-
keys << k
|
60
|
-
vals << v
|
61
|
-
}
|
62
|
-
keys, k2i = Tb::Pairs.get_key2index(keys)
|
63
|
-
@keys = keys
|
64
|
-
@k2i = k2i
|
65
|
-
@vals = vals
|
66
|
-
end
|
67
|
-
|
68
|
-
def pretty_print(q) # :nodoc:
|
69
|
-
q.object_group(self) {
|
70
|
-
fs = @keys
|
71
|
-
unless fs.empty?
|
72
|
-
q.text ':'
|
73
|
-
q.breakable
|
74
|
-
end
|
75
|
-
q.seplist(fs, nil, :each) {|f|
|
76
|
-
v = self[f]
|
77
|
-
q.group {
|
78
|
-
q.pp f
|
79
|
-
q.text '=>'
|
80
|
-
q.group(1) {
|
81
|
-
q.breakable ''
|
82
|
-
q.pp v
|
83
|
-
}
|
84
|
-
}
|
85
|
-
}
|
86
|
-
}
|
87
|
-
end
|
88
|
-
alias inspect pretty_print_inspect # :nodoc:
|
89
|
-
|
90
|
-
def [](key)
|
91
|
-
i = @k2i.fetch(key) {
|
92
|
-
return nil
|
93
|
-
}
|
94
|
-
@vals[i]
|
95
|
-
end
|
96
|
-
|
97
|
-
def each
|
98
|
-
@keys.each_index {|i|
|
99
|
-
yield [@keys[i], @vals[i]]
|
100
|
-
}
|
101
|
-
end
|
102
|
-
|
103
|
-
def each_key(&b)
|
104
|
-
@keys.each(&b)
|
105
|
-
end
|
106
|
-
|
107
|
-
def each_value(&b)
|
108
|
-
@vals.each(&b)
|
109
|
-
end
|
110
|
-
|
111
|
-
def to_h
|
112
|
-
h = {}
|
113
|
-
@keys.each_with_index {|k, i|
|
114
|
-
v = @vals[i]
|
115
|
-
h[k] = v
|
116
|
-
}
|
117
|
-
h
|
118
|
-
end
|
119
|
-
|
120
|
-
def empty?
|
121
|
-
@keys.empty?
|
122
|
-
end
|
123
|
-
|
124
|
-
if defined?(::KeyError)
|
125
|
-
KeyError = ::KeyError
|
126
|
-
else
|
127
|
-
KeyError = IndexError
|
128
|
-
end
|
129
|
-
|
130
|
-
def fetch(key, *rest)
|
131
|
-
if 1 < rest.length
|
132
|
-
raise ArgumentError, "wrong number of arguments (#{1+rest.length} for 1..2)"
|
133
|
-
elsif block_given?
|
134
|
-
i = @k2i.fetch(key) {
|
135
|
-
return yield(key)
|
136
|
-
}
|
137
|
-
@vals[i]
|
138
|
-
elsif !rest.empty?
|
139
|
-
i = @k2i.fetch(key) {
|
140
|
-
return rest[0]
|
141
|
-
}
|
142
|
-
@vals[i]
|
143
|
-
else
|
144
|
-
i = @k2i.fetch(key)
|
145
|
-
@vals[i]
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
def has_key?(key)
|
150
|
-
@k2i.has_key?(key)
|
151
|
-
end
|
152
|
-
alias include? has_key?
|
153
|
-
alias key? has_key?
|
154
|
-
alias member? has_key?
|
155
|
-
|
156
|
-
def has_value?(value)
|
157
|
-
i = index(value)
|
158
|
-
if i
|
159
|
-
true
|
160
|
-
else
|
161
|
-
false
|
162
|
-
end
|
163
|
-
end
|
164
|
-
alias value? has_value?
|
165
|
-
|
166
|
-
def index(value)
|
167
|
-
@vals.each_with_index {|v, i|
|
168
|
-
if v == value
|
169
|
-
return @keys[i]
|
170
|
-
end
|
171
|
-
}
|
172
|
-
nil
|
173
|
-
end
|
174
|
-
alias key index
|
175
|
-
|
176
|
-
def invert
|
177
|
-
Tb::Pairs.new(self.map {|k, v| [v, k] })
|
178
|
-
end
|
179
|
-
|
180
|
-
def keys
|
181
|
-
@keys.dup
|
182
|
-
end
|
183
|
-
|
184
|
-
def length
|
185
|
-
@keys.length
|
186
|
-
end
|
187
|
-
alias size length
|
188
|
-
|
189
|
-
def merge(other)
|
190
|
-
pairs = []
|
191
|
-
self.each {|k, v|
|
192
|
-
if other.has_key? k
|
193
|
-
if block_given?
|
194
|
-
v = yield(k, v, other[k])
|
195
|
-
else
|
196
|
-
v = other[k]
|
197
|
-
end
|
198
|
-
end
|
199
|
-
pairs << [k, v]
|
200
|
-
}
|
201
|
-
other.each {|k, v|
|
202
|
-
next if self.has_key? k
|
203
|
-
pairs << [k, v]
|
204
|
-
}
|
205
|
-
Tb::Pairs.new(pairs)
|
206
|
-
end
|
207
|
-
|
208
|
-
def reject
|
209
|
-
pairs = []
|
210
|
-
self.each {|kv|
|
211
|
-
unless yield kv
|
212
|
-
pairs << kv
|
213
|
-
end
|
214
|
-
}
|
215
|
-
Tb::Pairs.new(pairs)
|
216
|
-
end
|
217
|
-
|
218
|
-
def values
|
219
|
-
@vals.dup
|
220
|
-
end
|
221
|
-
|
222
|
-
def values_at(*keys)
|
223
|
-
keys.map {|k|
|
224
|
-
self[k]
|
225
|
-
}
|
226
|
-
end
|
227
|
-
end
|
data/test/test_pairs.rb
DELETED
@@ -1,122 +0,0 @@
|
|
1
|
-
require 'tb'
|
2
|
-
require 'test/unit'
|
3
|
-
|
4
|
-
class TestTbPairs < Test::Unit::TestCase
|
5
|
-
def test_initialize
|
6
|
-
tp = Tb::Pairs.new([["a", 1], ["b", 2]])
|
7
|
-
assert_kind_of(Tb::Pairs, tp)
|
8
|
-
end
|
9
|
-
|
10
|
-
def test_ref
|
11
|
-
tp = Tb::Pairs.new([["a", 1], ["b", 2]])
|
12
|
-
assert_equal(1, tp["a"])
|
13
|
-
assert_equal(2, tp["b"])
|
14
|
-
assert_equal(nil, tp["z"])
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_each
|
18
|
-
tp = Tb::Pairs.new([["a", 1], ["b", 2]])
|
19
|
-
result = []
|
20
|
-
tp.each {|k, v|
|
21
|
-
result << [k, v]
|
22
|
-
}
|
23
|
-
assert_equal([["a", 1], ["b", 2]], result)
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_each_key
|
27
|
-
tp = Tb::Pairs.new([["a", 1], ["b", 2]])
|
28
|
-
result = []
|
29
|
-
tp.each_key {|k|
|
30
|
-
result << k
|
31
|
-
}
|
32
|
-
assert_equal(["a", "b"], result)
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_each_value
|
36
|
-
tp = Tb::Pairs.new([["a", 1], ["b", 2]])
|
37
|
-
result = []
|
38
|
-
tp.each_value {|v|
|
39
|
-
result << v
|
40
|
-
}
|
41
|
-
assert_equal([1, 2], result)
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_empty?
|
45
|
-
tp = Tb::Pairs.new([])
|
46
|
-
assert_equal(true, tp.empty?)
|
47
|
-
tp = Tb::Pairs.new([["a", 1], ["b", 2]])
|
48
|
-
assert_equal(false, tp.empty?)
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_fetch
|
52
|
-
tp = Tb::Pairs.new([["a", 1], ["b", 2]])
|
53
|
-
assert_equal(false, tp.empty?)
|
54
|
-
assert_equal(1, tp.fetch("a"))
|
55
|
-
assert_raise(Tb::Pairs::KeyError) { tp.fetch("z") }
|
56
|
-
assert_equal(2, tp.fetch("b", 100))
|
57
|
-
assert_equal(2, tp.fetch("b") { 200 })
|
58
|
-
assert_equal(100, tp.fetch("z", 100))
|
59
|
-
assert_equal(200, tp.fetch("z") { 200 })
|
60
|
-
assert_raise(ArgumentError) { tp.fetch() }
|
61
|
-
assert_raise(ArgumentError) { tp.fetch(1, 2, 3) }
|
62
|
-
end
|
63
|
-
|
64
|
-
def test_has_key?
|
65
|
-
tp = Tb::Pairs.new([["a", 1], ["b", 2]])
|
66
|
-
assert_equal(true, tp.has_key?("a"))
|
67
|
-
assert_equal(false, tp.has_key?("z"))
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_has_value?
|
71
|
-
tp = Tb::Pairs.new([["a", 1], ["b", 2]])
|
72
|
-
assert_equal(true, tp.has_value?(1))
|
73
|
-
assert_equal(false, tp.has_value?(100))
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_index
|
77
|
-
tp = Tb::Pairs.new([["a", 1], ["b", 2]])
|
78
|
-
assert_equal("b", tp.index(2))
|
79
|
-
assert_equal(nil, tp.index(200))
|
80
|
-
end
|
81
|
-
|
82
|
-
def test_invert
|
83
|
-
tp = Tb::Pairs.new([["a", 1], ["b", 2]])
|
84
|
-
tp2 = tp.invert
|
85
|
-
assert_kind_of(Tb::Pairs, tp2)
|
86
|
-
assert_equal([[1, "a"], [2, "b"]], tp2.to_a)
|
87
|
-
end
|
88
|
-
|
89
|
-
def test_keys
|
90
|
-
tp = Tb::Pairs.new([["a", 1], ["b", 2]])
|
91
|
-
assert_equal(["a", "b"], tp.keys)
|
92
|
-
end
|
93
|
-
|
94
|
-
def test_length
|
95
|
-
tp = Tb::Pairs.new([["a", 1], ["b", 2]])
|
96
|
-
assert_equal(2, tp.length)
|
97
|
-
end
|
98
|
-
|
99
|
-
def test_merge
|
100
|
-
tp1 = Tb::Pairs.new([["a", 1], ["b", 2]])
|
101
|
-
tp2 = Tb::Pairs.new([["b", 3], ["c", 4]])
|
102
|
-
assert_equal([["a", 1], ["b", 3], ["c", 4]], tp1.merge(tp2).to_a)
|
103
|
-
assert_equal([["a", 1], ["b", ["b", 2, 3]], ["c", 4]], tp1.merge(tp2) {|k, v1, v2| [k, v1, v2] }.to_a)
|
104
|
-
end
|
105
|
-
|
106
|
-
def test_reject
|
107
|
-
tp = Tb::Pairs.new([["a", 1], ["b", 2]])
|
108
|
-
assert_equal([["a", 1]], tp.reject {|k, v| k == "b" }.to_a)
|
109
|
-
assert_equal([["b", 2]], tp.reject {|k, v| v == 1 }.to_a)
|
110
|
-
end
|
111
|
-
|
112
|
-
def test_values
|
113
|
-
tp = Tb::Pairs.new([["a", 1], ["b", 2]])
|
114
|
-
assert_equal([1, 2], tp.values)
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_values_at
|
118
|
-
tp = Tb::Pairs.new([["a", 1], ["b", 2]])
|
119
|
-
assert_equal([1, 2], tp.values_at("a", "b"))
|
120
|
-
end
|
121
|
-
|
122
|
-
end
|