art_vandelay 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +19 -3
- data/lib/art_vandelay/version.rb +1 -1
- data/lib/art_vandelay.rb +12 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10ca1f715220a41986809df5de3ecaeb399b09205b537428cea17c79c9b0d5ef
|
4
|
+
data.tar.gz: bc9f8069a449f7844ca39f9db4ad60729bc9badf2be2f55f3752e55631744cd4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc8edf62faa35e6a6ea83715990174072ceaa357c0d095c19819f95db08ecf025d74d7dc2032cb818ad4a11d75d9e90e5902b37dfddb780a4cc23519c153863b
|
7
|
+
data.tar.gz: 88bd0d02e5b9555cc111951e0845c4a3112f88b965668263d336c32def905d4eba631f9873c6607ad901389ab03eb1a2818f770a6b299f439f3f5ff5d1c63fe5
|
data/README.md
CHANGED
@@ -18,7 +18,7 @@ Have you ever been on a project where, out of nowhere, someone asks you to send
|
|
18
18
|
Add this line to your application's Gemfile:
|
19
19
|
|
20
20
|
```ruby
|
21
|
-
gem "art_vandelay"
|
21
|
+
gem "art_vandelay"
|
22
22
|
```
|
23
23
|
|
24
24
|
And then execute:
|
@@ -67,7 +67,7 @@ Returns an instance of `ArtVandelay::Export::Result`.
|
|
67
67
|
result = ArtVandelay::Export.new(User.all).csv
|
68
68
|
# => #<ArtVandelay::Export::Result>
|
69
69
|
|
70
|
-
csv_exports = result.
|
70
|
+
csv_exports = result.csv_exports
|
71
71
|
# => [#<CSV::Table>, #<CSV::Table>, ...]
|
72
72
|
|
73
73
|
csv = csv_exports.first.to_a
|
@@ -140,13 +140,14 @@ ArtVandelay::Import.new(model_name, **options)
|
|
140
140
|
|Argument|Description|
|
141
141
|
|--------|-----------|
|
142
142
|
|`model_name`|The name of the model being imported. E.g. `:users`, `:user`, `"users"` or `"user"`|
|
143
|
-
|`**options`|A hash of options. Available options are `rollback:`|
|
143
|
+
|`**options`|A hash of options. Available options are `rollback:`, `strip:`|
|
144
144
|
|
145
145
|
#### Options
|
146
146
|
|
147
147
|
|Option|Description|
|
148
148
|
|------|-----------|
|
149
149
|
|`rollback:`|Whether the import should rollback if any of the records fails to save.|
|
150
|
+
|`strip:`|Strips leading and trailing whitespace from all values, including headers.|
|
150
151
|
|
151
152
|
#### ArtVandelay::Import#csv
|
152
153
|
|
@@ -221,6 +222,21 @@ result = ArtVandelay::Import.new(:users).csv(csv_string, attributes: {email_addr
|
|
221
222
|
# => #<ArtVandelay::Import::Result>
|
222
223
|
```
|
223
224
|
|
225
|
+
##### Stripping whitespace
|
226
|
+
|
227
|
+
```ruby
|
228
|
+
csv_string = CSV.generate do |csv|
|
229
|
+
csv << ["email_address ", " passcode "]
|
230
|
+
csv << [" george@vandelay_industries.com ", " bosco "]
|
231
|
+
end
|
232
|
+
|
233
|
+
result = ArtVandelay::Import.new(:users, strip: true).csv(csv_string, attributes: {email_address: :email, passcode: :password})
|
234
|
+
# => #<ArtVandelay::Import::Result>
|
235
|
+
|
236
|
+
result.rows_accepted
|
237
|
+
# => [{:row=>["george@vandelay_industries.com", "bosco"], :id=>1}]
|
238
|
+
```
|
239
|
+
|
224
240
|
## 🙏 Contributing
|
225
241
|
|
226
242
|
1. Run `./bin/setup`.
|
data/lib/art_vandelay/version.rb
CHANGED
data/lib/art_vandelay.rb
CHANGED
@@ -142,6 +142,7 @@ module ArtVandelay
|
|
142
142
|
def initialize(model_name, **options)
|
143
143
|
@options = options.symbolize_keys
|
144
144
|
@rollback = options[:rollback]
|
145
|
+
@strip = options[:strip]
|
145
146
|
@model_name = model_name
|
146
147
|
end
|
147
148
|
|
@@ -164,7 +165,7 @@ module ArtVandelay
|
|
164
165
|
|
165
166
|
private
|
166
167
|
|
167
|
-
attr_reader :model_name, :rollback
|
168
|
+
attr_reader :model_name, :rollback, :strip
|
168
169
|
|
169
170
|
def active_record
|
170
171
|
model_name.to_s.classify.constantize
|
@@ -177,8 +178,16 @@ module ArtVandelay
|
|
177
178
|
def build_params(row, attributes)
|
178
179
|
attributes = attributes.stringify_keys
|
179
180
|
|
180
|
-
|
181
|
-
|
181
|
+
if strip
|
182
|
+
row.to_h.stringify_keys.transform_keys do |key|
|
183
|
+
attributes[key.strip] || key.strip
|
184
|
+
end.tap do |new_params|
|
185
|
+
new_params.transform_values!(&:strip)
|
186
|
+
end
|
187
|
+
else
|
188
|
+
row.to_h.stringify_keys.transform_keys do |key|
|
189
|
+
attributes[key] || key
|
190
|
+
end
|
182
191
|
end
|
183
192
|
end
|
184
193
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: art_vandelay
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve Polito
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|