csv-importer 0.5.0 → 0.6.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 +17 -1
- data/README.md +9 -0
- data/lib/csv_importer/row.rb +6 -4
- data/lib/csv_importer/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: 204d73b513134e022dd8e3529869e8f9c63602d7
|
4
|
+
data.tar.gz: 46b7b9e27dab7add3abfd117959f8919f71b5f22
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 586def4222cccb61d571e4904d76d1f055537e25529be90552aad206fa527845f6de0ee99ba8fccea786817c961d3b7366e3fc64456df32cefdc25a44e8bd730
|
7
|
+
data.tar.gz: ae9443c1998169329c5c432b9df9b7480206c32bcc2da4cd733cd1351a4586823bf0b05b205e74a7a5645308e5e39016930a537fa30ccd45bb1b7d6e708c3f07
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,20 @@
|
|
2
2
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
4
|
|
5
|
+
## [0.6.0] - 2018-05-22
|
6
|
+
|
7
|
+
### Added
|
8
|
+
|
9
|
+
* We now pass the `column` object as the third parameter of the `column`
|
10
|
+
block for advanced usage. [#73][] by [@stas][].
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
column :extra, as: [/extra/], to: ->(value, user, column) do
|
14
|
+
attribute = column.name.sub(/^extra /, '')
|
15
|
+
user[attribute] = value
|
16
|
+
end
|
17
|
+
```
|
18
|
+
|
5
19
|
## [0.5.0] - 2018-01-13
|
6
20
|
|
7
21
|
### Added
|
@@ -159,10 +173,12 @@ report object instead of raising an exception.
|
|
159
173
|
[#63]: https://github.com/BrewhouseTeam/csv-importer/issues/63
|
160
174
|
[#68]: https://github.com/BrewhouseTeam/csv-importer/issues/68
|
161
175
|
[#69]: https://github.com/BrewhouseTeam/csv-importer/issues/69
|
176
|
+
[#73]: https://github.com/BrewhouseTeam/csv-importer/issues/73
|
162
177
|
[@danielweinmann]: https://github.com/danielweinmann
|
163
178
|
[@egg-chicken]: https://github.com/egg-chicken
|
164
179
|
[@fxgallego]: https://github.com/fxgallego
|
165
180
|
[@macfanatic]: https://github.com/macfanatic
|
166
181
|
[@paulodeleo]: https://github.com/paulodeleo
|
167
182
|
[@pnomolos]: https://github.com/pnomolos
|
168
|
-
[@shvetsovdm]: https://github.com/shvetsovdm
|
183
|
+
[@shvetsovdm]: https://github.com/shvetsovdm
|
184
|
+
[@stas]: https://github.com/stas
|
data/README.md
CHANGED
@@ -178,6 +178,15 @@ If you need to do more advanced stuff, you've got access to the model:
|
|
178
178
|
column :email, as: [/e.?mail/i, "courriel"], to: ->(email, user) { user.email = email.downcase; model.super_user! if email[/@brewhouse.io\z/] }
|
179
179
|
```
|
180
180
|
|
181
|
+
Like very advanced stuff? We grant you access to the [`column`](https://github.com/pcreux/csv-importer/blob/master/lib/csv_importer/column.rb) object itself which contains the column name – quite handy if you want to support arbitrary columns.
|
182
|
+
|
183
|
+
```ruby
|
184
|
+
column :extra, as: [/extra/], to: ->(value, user, column) do
|
185
|
+
attribute = column.name.sub(/^extra /, '')
|
186
|
+
user[attribute] = value
|
187
|
+
end
|
188
|
+
```
|
189
|
+
|
181
190
|
Now, what if the user does not provide the email column? It's not worth
|
182
191
|
running the import, we should just reject the CSV file right away.
|
183
192
|
That's easy:
|
data/lib/csv_importer/row.rb
CHANGED
@@ -41,17 +41,17 @@ module CSVImporter
|
|
41
41
|
# can't dup Symbols, Integer etc...
|
42
42
|
end
|
43
43
|
|
44
|
-
|
45
|
-
next if column_definition.nil?
|
44
|
+
next if column.definition.nil?
|
46
45
|
|
47
|
-
set_attribute(model,
|
46
|
+
set_attribute(model, column, value)
|
48
47
|
end
|
49
48
|
|
50
49
|
model
|
51
50
|
end
|
52
51
|
|
53
52
|
# Set the attribute using the column_definition and the csv_value
|
54
|
-
def set_attribute(model,
|
53
|
+
def set_attribute(model, column, csv_value)
|
54
|
+
column_definition = column.definition
|
55
55
|
if column_definition.to && column_definition.to.is_a?(Proc)
|
56
56
|
to_proc = column_definition.to
|
57
57
|
|
@@ -60,6 +60,8 @@ module CSVImporter
|
|
60
60
|
model.public_send("#{column_definition.name}=", to_proc.call(csv_value))
|
61
61
|
when 2 # to: ->(published, post) { post.published_at = Time.now if published == "true" }
|
62
62
|
to_proc.call(csv_value, model)
|
63
|
+
when 3 # to: ->(field_value, post, column) { post.hash_field[column.name] = field_value }
|
64
|
+
to_proc.call(csv_value, model, column)
|
63
65
|
else
|
64
66
|
raise ArgumentError, "`to` proc can only have 1 or 2 arguments"
|
65
67
|
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.6.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: 2018-
|
11
|
+
date: 2018-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: virtus
|