csv-importer 0.4.0 → 0.5.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cfff4468934d3d7fd8e13b81d618bd2691f3fac7
4
- data.tar.gz: f8980b3710130e74d400c9f649da525da7d46145
3
+ metadata.gz: f6449a2ee619cee70575daee4f274e70e8e03a57
4
+ data.tar.gz: f4b8243bc34a4e65383838536b012c6ad631ed6e
5
5
  SHA512:
6
- metadata.gz: cd7068f3d2a32f40b58234fc0805883891e5212aeed10dab1c42c1b3198475080e18a88107fd095d02cda7002740b05c1d1436ec0a6e034899d9c0b0a12c93dc
7
- data.tar.gz: aa78cf760944b7db3204e746b480e01a161ab4a834ddb79aae65a4d33d7b41f8b03a34d68e898add41a7b209d033c3613b06f264fdfcbb380d092ce69e89ea7d
6
+ metadata.gz: b6928796fdba7eff29eb46f83d1c7de1c6fa70786602792d5cf08c24f94452e5f02844c56645339c861e07870b71fd1c913ab5bb2f8e82509863a98ea6d7a331
7
+ data.tar.gz: 5f0bfae5115d6a8a494a94e80872ac542a4deb6d29b81aef396e2bf5f6ea07fe7139e875637419f5f3b3308dc38943f2d6fa1e760b365450da586840113d6f53
@@ -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
- [@shvetsovdm]: https://github.com/shvetsovdm
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.
@@ -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/BrewhouseTeam/csv-importer"
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)/}) }
@@ -5,7 +5,7 @@ module CSVImporter
5
5
 
6
6
  attribute :model
7
7
  attribute :column_definitions, Array[ColumnDefinition], default: proc { [] }
8
- attribute :identifiers, Array[Symbol], default: []
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
-
@@ -10,8 +10,8 @@ module CSVImporter
10
10
  config.column_definitions << options.merge(name: name)
11
11
  end
12
12
 
13
- def identifier(*identifiers)
14
- config.identifiers = 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
@@ -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, Array[Symbol]
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.empty?
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
@@ -63,7 +63,16 @@ module CSVImporter
63
63
  end
64
64
 
65
65
  add_to_report(row, tags)
66
- after_save_blocks.each { |block| block.call(row.model) }
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
@@ -1,3 +1,3 @@
1
1
  module CSVImporter
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
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.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: 2017-08-10 00:00:00.000000000 Z
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/BrewhouseTeam/csv-importer
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.4.5.1
108
+ rubygems_version: 2.6.13
109
109
  signing_key:
110
110
  specification_version: 4
111
111
  summary: CSV Import for humans