active_tsv 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 +4 -4
- data/.travis.yml +2 -3
- data/README.md +0 -1
- data/data/benchmark.rb +2 -2
- data/lib/active_tsv/base.rb +9 -13
- data/lib/active_tsv/relation.rb +18 -17
- data/lib/active_tsv/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1e82adf6d31ab6578a3e5b5f290682135ab9a3b
|
4
|
+
data.tar.gz: 877883f7b97bb19854e17b801f2b71c054caf50b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a7e77ed3e9fc8f04924549bed9327913dc66c381ef863437d107745246fbefe1dae9c6a80a2c86b73866e37a2022a99684bb6a256baa8f2bdf517e695fce613
|
7
|
+
data.tar.gz: 03a7633a7bec5da34353e40a7fba5719c12bada29217a1b5686ed9de47a33e594a38dff57904ef6ad3d784db1ef011d32dd7b61404fd09fdc9dfe4cae5858546
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
data/data/benchmark.rb
CHANGED
@@ -25,9 +25,9 @@ module ActiveHashTsv
|
|
25
25
|
def load_path(path)
|
26
26
|
data = []
|
27
27
|
CSV.open(path, col_sep: self::SEPARATER) do |csv|
|
28
|
-
|
28
|
+
column_names = csv.gets.map(&:to_sym)
|
29
29
|
while line = csv.gets
|
30
|
-
data <<
|
30
|
+
data << column_names.zip(line).to_h
|
31
31
|
end
|
32
32
|
end
|
33
33
|
data
|
data/lib/active_tsv/base.rb
CHANGED
@@ -20,16 +20,16 @@ module ActiveTsv
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def reload(path)
|
23
|
-
if @
|
24
|
-
|
23
|
+
if @column_names
|
24
|
+
column_names.each do |k|
|
25
25
|
remove_method(k)
|
26
26
|
remove_method("#{k}=")
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
@
|
30
|
+
@column_names = nil
|
31
31
|
@table_path = path
|
32
|
-
|
32
|
+
column_names.each do |k|
|
33
33
|
define_method(k) { @attrs[k] }
|
34
34
|
define_method("#{k}=") { |v| @attrs[k] = v }
|
35
35
|
end
|
@@ -47,12 +47,8 @@ module ActiveTsv
|
|
47
47
|
CSV.open(table_path, "r:#{encoding}:UTF-8", col_sep: self::SEPARATER, &block)
|
48
48
|
end
|
49
49
|
|
50
|
-
def
|
51
|
-
@
|
52
|
-
end
|
53
|
-
|
54
|
-
def keys=(headers)
|
55
|
-
@keys = headers.map(&:to_sym)
|
50
|
+
def column_names
|
51
|
+
@column_names ||= open { |csv| csv.gets }
|
56
52
|
end
|
57
53
|
|
58
54
|
def primary_key
|
@@ -82,7 +78,7 @@ module ActiveTsv
|
|
82
78
|
when Hash
|
83
79
|
@attrs = attrs
|
84
80
|
when Array
|
85
|
-
@attrs = self.class.
|
81
|
+
@attrs = self.class.column_names.zip(attrs).to_h
|
86
82
|
else
|
87
83
|
raise ArgumentError, "#{attrs.class} is not supported value"
|
88
84
|
end
|
@@ -93,11 +89,11 @@ module ActiveTsv
|
|
93
89
|
end
|
94
90
|
|
95
91
|
def [](key)
|
96
|
-
@attrs[key.
|
92
|
+
@attrs[key.to_s]
|
97
93
|
end
|
98
94
|
|
99
95
|
def []=(key, value)
|
100
|
-
@attrs[key.
|
96
|
+
@attrs[key.to_s] = value
|
101
97
|
end
|
102
98
|
|
103
99
|
def attributes
|
data/lib/active_tsv/relation.rb
CHANGED
@@ -61,14 +61,14 @@ module ActiveTsv
|
|
61
61
|
end
|
62
62
|
|
63
63
|
def pluck(*fields)
|
64
|
-
key_to_value_index = @model.
|
64
|
+
key_to_value_index = @model.column_names.each_with_index.to_h
|
65
65
|
if fields.empty?
|
66
66
|
to_value_a
|
67
67
|
elsif fields.one?
|
68
|
-
|
69
|
-
to_value_a.map! { |v| v[key_to_value_index[field]] }
|
68
|
+
to_value_a.map! { |v| v[key_to_value_index[fields.first.to_s]] }
|
70
69
|
else
|
71
|
-
|
70
|
+
indexes = fields.map(&:to_s).map! { |field| key_to_value_index[field] }
|
71
|
+
to_value_a.map! { |v| v.values_at(*indexes) }
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
@@ -174,7 +174,7 @@ module ActiveTsv
|
|
174
174
|
def to_value_a
|
175
175
|
ret = each_value.to_a
|
176
176
|
if @order_values.empty?.!
|
177
|
-
key_to_value_index = @model.
|
177
|
+
key_to_value_index = @model.column_names.each_with_index.to_h
|
178
178
|
if @order_values.one?
|
179
179
|
order_condition = @order_values.first
|
180
180
|
index = key_to_value_index[order_condition.column]
|
@@ -196,7 +196,7 @@ module ActiveTsv
|
|
196
196
|
def each_value
|
197
197
|
return to_enum(__method__) unless block_given?
|
198
198
|
|
199
|
-
key_to_value_index = @model.
|
199
|
+
key_to_value_index = @model.column_names.each_with_index.to_h
|
200
200
|
@model.open do |csv|
|
201
201
|
csv.gets
|
202
202
|
csv.each do |value|
|
@@ -204,7 +204,7 @@ module ActiveTsv
|
|
204
204
|
case cond
|
205
205
|
when Condition::Equal
|
206
206
|
cond.values.all? do |k, v|
|
207
|
-
index = key_to_value_index[k.
|
207
|
+
index = key_to_value_index[k.to_s]
|
208
208
|
raise StatementInvalid, "no such column: #{k}" unless index
|
209
209
|
if v.respond_to?(:to_a)
|
210
210
|
v.to_a.any? { |vv| value[index] == vv.to_s }
|
@@ -214,7 +214,7 @@ module ActiveTsv
|
|
214
214
|
end
|
215
215
|
when Condition::NotEqual
|
216
216
|
cond.values.all? do |k, v|
|
217
|
-
index = key_to_value_index[k.
|
217
|
+
index = key_to_value_index[k.to_s]
|
218
218
|
raise StatementInvalid, "no such column: #{k}" unless index
|
219
219
|
if v.respond_to?(:to_a)
|
220
220
|
!v.to_a.any? { |vv| value[index] == vv.to_s }
|
@@ -237,20 +237,21 @@ module ActiveTsv
|
|
237
237
|
def order_conditions(columns)
|
238
238
|
columns.map { |column|
|
239
239
|
case column
|
240
|
-
when Symbol
|
241
|
-
Ordering::Ascending.new(column)
|
240
|
+
when Symbol, String
|
241
|
+
Ordering::Ascending.new(column.to_s)
|
242
242
|
when Hash
|
243
243
|
column.map do |col, direction|
|
244
|
-
|
244
|
+
case direction
|
245
|
+
when :asc, :ASC, "asc", "ASC"
|
246
|
+
Ordering::Ascending.new(col.to_s)
|
247
|
+
when :desc, :DESC, "desc", "DESC"
|
248
|
+
Ordering::Descending.new(col.to_s)
|
249
|
+
else
|
245
250
|
raise ArgumentError, %(Direction "#{direction}" is invalid. Valid directions are: #{Ordering::VALID_DIRECTIONS})
|
246
251
|
end
|
247
|
-
case direction.downcase.to_sym
|
248
|
-
when :asc
|
249
|
-
Ordering::Ascending.new(col)
|
250
|
-
when :desc
|
251
|
-
Ordering::Descending.new(col)
|
252
|
-
end
|
253
252
|
end
|
253
|
+
else
|
254
|
+
raise TypeError, "Cannot visit #{column.class}"
|
254
255
|
end
|
255
256
|
}.flatten
|
256
257
|
end
|
data/lib/active_tsv/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_tsv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ksss
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|