active_tsv 0.3.1 → 0.3.2

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: a1e82adf6d31ab6578a3e5b5f290682135ab9a3b
4
- data.tar.gz: 877883f7b97bb19854e17b801f2b71c054caf50b
3
+ metadata.gz: bd70390aab63f668f621f2305a840d60169f6e1e
4
+ data.tar.gz: dd47ec7b43b36397d701db69aadcedc1166a64a8
5
5
  SHA512:
6
- metadata.gz: 2a7e77ed3e9fc8f04924549bed9327913dc66c381ef863437d107745246fbefe1dae9c6a80a2c86b73866e37a2022a99684bb6a256baa8f2bdf517e695fce613
7
- data.tar.gz: 03a7633a7bec5da34353e40a7fba5719c12bada29217a1b5686ed9de47a33e594a38dff57904ef6ad3d784db1ef011d32dd7b61404fd09fdc9dfe4cae5858546
6
+ metadata.gz: c200088cb1725b2d2357642b99c4aa2102c0e591c8b2c88a4b9ba7c42b2077f907f059c0c3f9ba238b453f882772d3fdd69a70e6cca61119dff17bcede9ab188
7
+ data.tar.gz: 2ad08364ab1dd69f2937e6f8b4a54f2a55275da21f900a8e1fffa7ac79ac62d05cb57d75b2b9e24bc8cdc719f06e668b975fee4228e346dcb9a805c8cc0c94ac
@@ -2,5 +2,6 @@ language: ruby
2
2
  rvm:
3
3
  - 2.2.6
4
4
  - 2.3.3
5
+ - 2.4.0
5
6
  notifications:
6
7
  email: false
@@ -0,0 +1,3 @@
1
+ 1 2 3
2
+ 2 3 4
3
+ 3 4 5
@@ -28,6 +28,7 @@ module ActiveTsv
28
28
  end
29
29
 
30
30
  @column_names = nil
31
+ @custom_column_name = false
31
32
  @table_path = path
32
33
  column_names.each do |k|
33
34
  define_method(k) { @attrs[k] }
@@ -48,7 +49,24 @@ module ActiveTsv
48
49
  end
49
50
 
50
51
  def column_names
51
- @column_names ||= open { |csv| csv.gets }
52
+ @column_names ||= begin
53
+ @custom_column_name = false
54
+ open { |csv| csv.gets }
55
+ end
56
+ end
57
+
58
+ def column_names=(names)
59
+ @custom_column_name = true
60
+ column_names = names.map(&:to_s)
61
+ column_names.each do |k|
62
+ define_method(k) { @attrs[k] }
63
+ define_method("#{k}=") { |v| @attrs[k] = v }
64
+ end
65
+ @column_names = column_names
66
+ end
67
+
68
+ def custom_column_name?
69
+ @custom_column_name
52
70
  end
53
71
 
54
72
  def primary_key
@@ -76,7 +94,17 @@ module ActiveTsv
76
94
  def initialize(attrs = {})
77
95
  case attrs
78
96
  when Hash
79
- @attrs = attrs
97
+ h = {}
98
+ self.class.column_names.each do |name|
99
+ h[name] = nil
100
+ end
101
+ attrs.each do |name, v|
102
+ unless respond_to?("#{name}=")
103
+ raise UnknownAttributeError, "unknown attribute '#{name}' for #{self.class}."
104
+ end
105
+ h[name.to_s] = v
106
+ end
107
+ @attrs = h
80
108
  when Array
81
109
  @attrs = self.class.column_names.zip(attrs).to_h
82
110
  else
@@ -2,4 +2,5 @@ module ActiveTsv
2
2
  ActiveTsvError = Class.new(StandardError)
3
3
  RecordNotFound = Class.new(ActiveTsvError)
4
4
  StatementInvalid = Class.new(ActiveTsvError)
5
+ UnknownAttributeError = Class.new(NoMethodError)
5
6
  end
@@ -198,7 +198,7 @@ module ActiveTsv
198
198
 
199
199
  key_to_value_index = @model.column_names.each_with_index.to_h
200
200
  @model.open do |csv|
201
- csv.gets
201
+ csv.gets if !@model.custom_column_name?
202
202
  csv.each do |value|
203
203
  yield value if @where_values.all? do |cond|
204
204
  case cond
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveTsv
4
- VERSION = "0.3.1"
4
+ VERSION = "0.3.2"
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.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ksss
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-14 00:00:00.000000000 Z
11
+ date: 2017-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -87,6 +87,7 @@ files:
87
87
  - data/books.tsv
88
88
  - data/names.tsv
89
89
  - data/nicknames.tsv
90
+ - data/nokeys.tsv
90
91
  - data/passwords.tsv
91
92
  - data/user_books.tsv
92
93
  - data/users.csv