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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 26029667adccdd12b5098fb5e370050bb3bf4877dceb712cba6ce4f4032aec3f
4
- data.tar.gz: '05569d49fa0ad8da29bb07947e77db6425ba12506699b073c27690a644592b52'
3
+ metadata.gz: 10ca1f715220a41986809df5de3ecaeb399b09205b537428cea17c79c9b0d5ef
4
+ data.tar.gz: bc9f8069a449f7844ca39f9db4ad60729bc9badf2be2f55f3752e55631744cd4
5
5
  SHA512:
6
- metadata.gz: 1ae5ff614ad52587111bf87f10ea736d1087100ff3c782e25c4a8f8e01732f2829e7cd7640172c3e20756f20d6f9faa733b97f6fa53da39b9f442b0d796cd149
7
- data.tar.gz: 66c22f9871d06bc1a1d3f4852ef57d919b78960821eabd1f9c7e2c137954db3aa2e2d2d85d31c87f5038710d6a8bc66b139c5d753bc7df9abd3bf40236249a73
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", git: "https://github.com/thoughtbot/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.result.csv_exports
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`.
@@ -1,3 +1,3 @@
1
1
  module ArtVandelay
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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
- row.to_h.stringify_keys.transform_keys do |key|
181
- attributes[key] || key
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.1.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: 2022-12-09 00:00:00.000000000 Z
11
+ date: 2023-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails