csv-importer 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +38 -3
- data/README.md +8 -0
- data/csv-importer.gemspec +1 -1
- data/lib/csv_importer/config.rb +1 -2
- data/lib/csv_importer/dsl.rb +2 -2
- data/lib/csv_importer/row.rb +16 -2
- data/lib/csv_importer/runner.rb +10 -1
- data/lib/csv_importer/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6449a2ee619cee70575daee4f274e70e8e03a57
|
4
|
+
data.tar.gz: f4b8243bc34a4e65383838536b012c6ad631ed6e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6928796fdba7eff29eb46f83d1c7de1c6fa70786602792d5cf08c24f94452e5f02844c56645339c861e07870b71fd1c913ab5bb2f8e82509863a98ea6d7a331
|
7
|
+
data.tar.gz: 5f0bfae5115d6a8a494a94e80872ac542a4deb6d29b81aef396e2bf5f6ea07fe7139e875637419f5f3b3308dc38943f2d6fa1e760b365450da586840113d6f53
|
data/CHANGELOG.md
CHANGED
@@ -2,11 +2,40 @@
|
|
2
2
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
4
|
|
5
|
+
## [0.5.0] - 2018-01-13
|
6
|
+
|
7
|
+
### Added
|
8
|
+
|
9
|
+
* after_save supports block with arity of 2 for access to raw
|
10
|
+
attributes. [#68][] by [@macfanatic][].
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
class Importer
|
14
|
+
model Task
|
15
|
+
|
16
|
+
column :assignee, to: ->(name) { User.active.find_by(name: name) }
|
17
|
+
|
18
|
+
after_save do |task, attributes|
|
19
|
+
if task.errors[:assignee].present? && attributes['Assignee'].present?
|
20
|
+
task.errors.add(:assignee, "'#{ attributes['Assignee'] }' is not part of this project."
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
```
|
25
|
+
|
26
|
+
* support Proc identifiers. [#69][] by [@danielweinmann][]
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class Importer
|
30
|
+
identifier ->(user) { user.email.present? ? :email : [:company_id, :employee_id] }
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
5
34
|
## [0.4.0] - 2017-08-10
|
6
35
|
|
7
36
|
### Added
|
8
37
|
|
9
|
-
* Rows are now aware of their line number.
|
38
|
+
* Rows are now aware of their line number. [#63][] by [@paulodeleo][]
|
10
39
|
|
11
40
|
```ruby
|
12
41
|
import.report.invalid_rows.map { |row| [row.line_number, row.model.email, row.errors] }
|
@@ -127,7 +156,13 @@ report object instead of raising an exception.
|
|
127
156
|
[#38]: https://github.com/BrewhouseTeam/csv-importer/issues/38
|
128
157
|
[#47]: https://github.com/BrewhouseTeam/csv-importer/issues/47
|
129
158
|
[#52]: https://github.com/BrewhouseTeam/csv-importer/issues/52
|
159
|
+
[#63]: https://github.com/BrewhouseTeam/csv-importer/issues/63
|
160
|
+
[#68]: https://github.com/BrewhouseTeam/csv-importer/issues/68
|
161
|
+
[#69]: https://github.com/BrewhouseTeam/csv-importer/issues/69
|
162
|
+
[@danielweinmann]: https://github.com/danielweinmann
|
130
163
|
[@egg-chicken]: https://github.com/egg-chicken
|
131
|
-
[@pnomolos]: https://github.com/pnomolos
|
132
164
|
[@fxgallego]: https://github.com/fxgallego
|
133
|
-
[@
|
165
|
+
[@macfanatic]: https://github.com/macfanatic
|
166
|
+
[@paulodeleo]: https://github.com/paulodeleo
|
167
|
+
[@pnomolos]: https://github.com/pnomolos
|
168
|
+
[@shvetsovdm]: https://github.com/shvetsovdm
|
data/README.md
CHANGED
@@ -224,6 +224,14 @@ You can also define a composite identifier:
|
|
224
224
|
identifier :company_id, :employee_id
|
225
225
|
```
|
226
226
|
|
227
|
+
Or a Proc:
|
228
|
+
|
229
|
+
```ruby
|
230
|
+
# Update records with email if email is present
|
231
|
+
# Update records matching company_id AND employee_id if email is not present
|
232
|
+
identifier ->(user) { user.email.empty? ? [:company_id, :employee_id] : :email }
|
233
|
+
```
|
234
|
+
|
227
235
|
### Skip or Abort on error
|
228
236
|
|
229
237
|
By default, we skip invalid records and report errors back to the user.
|
data/csv-importer.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["pcreux@gmail.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{CSV Import for humans}
|
13
|
-
spec.homepage = "https://github.com/
|
13
|
+
spec.homepage = "https://github.com/pcreux/csv-importer"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
data/lib/csv_importer/config.rb
CHANGED
@@ -5,7 +5,7 @@ module CSVImporter
|
|
5
5
|
|
6
6
|
attribute :model
|
7
7
|
attribute :column_definitions, Array[ColumnDefinition], default: proc { [] }
|
8
|
-
attribute :identifiers
|
8
|
+
attribute :identifiers # Array[Symbol] or Proc
|
9
9
|
attribute :when_invalid, Symbol, default: proc { :skip }
|
10
10
|
attribute :after_build_blocks, Array[Proc], default: []
|
11
11
|
attribute :after_save_blocks, Array[Proc], default: []
|
@@ -27,4 +27,3 @@ module CSVImporter
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
30
|
-
|
data/lib/csv_importer/dsl.rb
CHANGED
@@ -10,8 +10,8 @@ module CSVImporter
|
|
10
10
|
config.column_definitions << options.merge(name: name)
|
11
11
|
end
|
12
12
|
|
13
|
-
def identifier(*
|
14
|
-
config.identifiers =
|
13
|
+
def identifier(*params)
|
14
|
+
config.identifiers = params.first.is_a?(Proc) ? params.first : params
|
15
15
|
end
|
16
16
|
|
17
17
|
alias_method :identifiers, :identifier
|
data/lib/csv_importer/row.rb
CHANGED
@@ -10,7 +10,7 @@ module CSVImporter
|
|
10
10
|
attribute :line_number, Integer
|
11
11
|
attribute :row_array, Array[String]
|
12
12
|
attribute :model_klass
|
13
|
-
attribute :identifiers
|
13
|
+
attribute :identifiers # Array[Symbol] or Proc
|
14
14
|
attribute :after_build_blocks, Array[Proc], default: []
|
15
15
|
attribute :skip, Boolean, default: false
|
16
16
|
|
@@ -89,10 +89,14 @@ module CSVImporter
|
|
89
89
|
end
|
90
90
|
|
91
91
|
def find_model
|
92
|
-
return nil if identifiers.
|
92
|
+
return nil if identifiers.nil?
|
93
93
|
|
94
94
|
model = build_model
|
95
95
|
set_attributes(model)
|
96
|
+
|
97
|
+
identifiers = model_identifiers(model)
|
98
|
+
return nil if identifiers.empty?
|
99
|
+
|
96
100
|
query = Hash[
|
97
101
|
identifiers.map { |identifier| [ identifier, model.public_send(identifier) ] }
|
98
102
|
]
|
@@ -106,5 +110,15 @@ module CSVImporter
|
|
106
110
|
def skip!
|
107
111
|
self.skip = true
|
108
112
|
end
|
113
|
+
|
114
|
+
private
|
115
|
+
|
116
|
+
def model_identifiers(model)
|
117
|
+
if identifiers.is_a?(Proc)
|
118
|
+
[identifiers.call(model)].flatten
|
119
|
+
else
|
120
|
+
identifiers
|
121
|
+
end
|
122
|
+
end
|
109
123
|
end
|
110
124
|
end
|
data/lib/csv_importer/runner.rb
CHANGED
@@ -63,7 +63,16 @@ module CSVImporter
|
|
63
63
|
end
|
64
64
|
|
65
65
|
add_to_report(row, tags)
|
66
|
-
|
66
|
+
|
67
|
+
after_save_blocks.each do |block|
|
68
|
+
case block.arity
|
69
|
+
when 0 then block.call
|
70
|
+
when 1 then block.call(row.model)
|
71
|
+
when 2 then block.call(row.model, row.csv_attributes)
|
72
|
+
else
|
73
|
+
raise ArgumentError, "after_save block of arity #{ block.arity } is not supported"
|
74
|
+
end
|
75
|
+
end
|
67
76
|
end
|
68
77
|
end
|
69
78
|
end
|
data/lib/csv_importer/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv-importer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Philippe Creux
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: virtus
|
@@ -85,7 +85,7 @@ files:
|
|
85
85
|
- lib/csv_importer/row.rb
|
86
86
|
- lib/csv_importer/runner.rb
|
87
87
|
- lib/csv_importer/version.rb
|
88
|
-
homepage: https://github.com/
|
88
|
+
homepage: https://github.com/pcreux/csv-importer
|
89
89
|
licenses:
|
90
90
|
- MIT
|
91
91
|
metadata: {}
|
@@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
105
|
version: '0'
|
106
106
|
requirements: []
|
107
107
|
rubyforge_project:
|
108
|
-
rubygems_version: 2.
|
108
|
+
rubygems_version: 2.6.13
|
109
109
|
signing_key:
|
110
110
|
specification_version: 4
|
111
111
|
summary: CSV Import for humans
|