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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 583d8e8209374ac577bd0837daba375f490e39de
4
- data.tar.gz: 22f74200fc7f26da338a2c16f061dd83449aa406
3
+ metadata.gz: a1e82adf6d31ab6578a3e5b5f290682135ab9a3b
4
+ data.tar.gz: 877883f7b97bb19854e17b801f2b71c054caf50b
5
5
  SHA512:
6
- metadata.gz: b6e48455014b19f9ce07a0aa54f1b11ba52329dda243ef9e8d9b0ccbc6f68d2ca642e731c4e5c40041373ae10e73282713d7a9e408b8ff105802652777d83e8c
7
- data.tar.gz: 17eddd9dd791b10c7234350ae809bde5391ece5ee95e868ee1fb3b7f6788ac27ee2eff41b26f551e1b32e65974a4b8db7152c1751b2b1a4edcec93ccdeca461e
6
+ metadata.gz: 2a7e77ed3e9fc8f04924549bed9327913dc66c381ef863437d107745246fbefe1dae9c6a80a2c86b73866e37a2022a99684bb6a256baa8f2bdf517e695fce613
7
+ data.tar.gz: 03a7633a7bec5da34353e40a7fba5719c12bada29217a1b5686ed9de47a33e594a38dff57904ef6ad3d784db1ef011d32dd7b61404fd09fdc9dfe4cae5858546
data/.travis.yml CHANGED
@@ -1,7 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.10
4
- - 2.2.5
5
- - 2.3.1
3
+ - 2.2.6
4
+ - 2.3.3
6
5
  notifications:
7
6
  email: false
data/README.md CHANGED
@@ -32,7 +32,6 @@ class User < ActiveTsv::Base
32
32
  self.table_path = "data/users.tsv" # required
33
33
  # self.encoding = Encoding::Shift_JIS # optional
34
34
  # self.primary_key = "uid" # optional
35
- # self.keys = %w(id name age) # optional
36
35
  has_many :nicknames
37
36
  end
38
37
 
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
- keys = csv.gets.map(&:to_sym)
28
+ column_names = csv.gets.map(&:to_sym)
29
29
  while line = csv.gets
30
- data << keys.zip(line).to_h
30
+ data << column_names.zip(line).to_h
31
31
  end
32
32
  end
33
33
  data
@@ -20,16 +20,16 @@ module ActiveTsv
20
20
  end
21
21
 
22
22
  def reload(path)
23
- if @keys
24
- keys.each do |k|
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
- @keys = nil
30
+ @column_names = nil
31
31
  @table_path = path
32
- keys.each do |k|
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 keys
51
- @keys ||= open { |csv| csv.gets }.map(&:to_sym)
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.keys.zip(attrs).to_h
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.to_sym]
92
+ @attrs[key.to_s]
97
93
  end
98
94
 
99
95
  def []=(key, value)
100
- @attrs[key.to_sym] = value
96
+ @attrs[key.to_s] = value
101
97
  end
102
98
 
103
99
  def attributes
@@ -61,14 +61,14 @@ module ActiveTsv
61
61
  end
62
62
 
63
63
  def pluck(*fields)
64
- key_to_value_index = @model.keys.each_with_index.to_h
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
- field = fields.first.to_sym
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
- to_value_a.map! { |v| fields.map { |field| v[key_to_value_index[field.to_sym]] } }
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.keys.each_with_index.to_h
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.keys.each_with_index.to_h
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.to_sym]
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.to_sym]
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
- unless Ordering::VALID_DIRECTIONS.include?(direction)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveTsv
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
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.0
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: 2016-11-24 00:00:00.000000000 Z
11
+ date: 2017-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport