row_boat 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
  SHA1:
3
- metadata.gz: 42b9a926cbca073ac25906f8f3ca224d2d665b62
4
- data.tar.gz: 28e138c7db5dfa6e8ef4139c82033551da7b6dfe
3
+ metadata.gz: 349a16106d1b36fe5758231d5f7569573892293f
4
+ data.tar.gz: 02a54356f10fbd87fc23b618301cc597e7028ded
5
5
  SHA512:
6
- metadata.gz: cfc989f812ad764feae56ab730d64fe5f6f5e57306eef8cf46c3940f5ba22394916bd9bd7372d2468a4f06141d9348fa82eae5a7a49f8dc47f165f0e6bf1b5ab
7
- data.tar.gz: 7007c0b60355e42523f5f6b4158c7ae5c82c7d80e012f557dda2093841db42be017f8c54f7e142ee761764ef5f2476d0073af1d35b58b636be393e9b8ea24afc
6
+ metadata.gz: fbbb578399fdafbbb597af6658d2d09935e9599805bfcccb640df24344e0c4db0a2c94c0b35d3afdef7aaa02948933e17d299fa017f375a22133245b99970eae
7
+ data.tar.gz: be36d03d5051fd7e1810da7be25f368cab0ee1c7a285f4bfc6907db746297c38785d3e8b4306f6aadf3f9cffc600816b15cb3a06e3cd9b5e11bf42a71b8f504a
data/API.md CHANGED
@@ -155,13 +155,13 @@ ImportProduct.import("path/to/product.csv")
155
155
 
156
156
  ### Description
157
157
 
158
- It is required that you override this method with a hash that maps columns in your CSV to their preferred names.
158
+ It is required that you override this method with either a hash that maps columns in your CSV to their preferred names or an array of your preferred column names.
159
159
 
160
- By default
160
+ By default when using a hash
161
161
  - CSV column names are downcased symbols of what they look like in the CSV.
162
162
  - CSV columns that are not mapped are ignored when processing the CSV.
163
163
 
164
- If you're familiar with [SmarterCSV](https://github.com/tilo/smarter_csv#documentation), this method essentially defines your `:key_mapping` and with the `:remove_unmapped_keys` setting set to `true`.
164
+ If you're familiar with [SmarterCSV](https://github.com/tilo/smarter_csv#documentation), this method essentially defines your `:key_mapping` with the `:remove_unmapped_keys` setting set to `true` when provided with a hash. When given an array it is the `:user_provided_headers` option.
165
165
 
166
166
  You can change these defaults by overriding the [`options`](#options) method.
167
167
 
@@ -177,6 +177,15 @@ class ImportProduct < RowBoat::Base
177
177
  }
178
178
  end
179
179
  end
180
+
181
+ # or...
182
+
183
+ class ImportProduct < RowBoat::Base
184
+ # other required configuration omitted for brevity
185
+ def column_mapping
186
+ [:name, :price_in_cents]
187
+ end
188
+ end
180
189
  ```
181
190
 
182
191
  ## `preprocess_row`
@@ -187,6 +196,8 @@ Implement this method if you need to do some work on the row before the record i
187
196
 
188
197
  If you return `nil` from this method, the row will be skipped in the import.
189
198
 
199
+ You also have access to `row_number` here.
200
+
190
201
  If the work you intend to do with the row only requires changing one attribute, it is recommended that you override [`value_converters`](#value_converters) instead of this.
191
202
 
192
203
  ### Example
@@ -195,7 +206,7 @@ If the work you intend to do with the row only requires changing one attribute,
195
206
  class ImportProduct < RowBoat::Base
196
207
  # required configuration omitted for brevity
197
208
  def preprocess_row(row)
198
- { default: :value }.merge(row)
209
+ { position: row_number }.merge(row)
199
210
  end
200
211
  # or...
201
212
  def preprocess_row(row)
data/README.md CHANGED
@@ -38,7 +38,7 @@ Or install it yourself as:
38
38
 
39
39
  #### [Full documentation can be found here.](/API.md#rowboat-api)
40
40
 
41
- Below we're defining the required methods ([`import_into`](/API.md#import_into) and [`column_mapping`](/API.md#column_mapping)) and a few additional options as well (via [`value_converters`](/API.md#value_converters) and [`options`](/API.md#options)). Checkout [API.md](/API.md#rowboat-api) for the full documentation for more :)
41
+ Below we're defining the required methods ([`import_into`](/API.md#import_into) and [`column_mapping`](/API.md#column_mapping)) and a few additional options as well (via [`value_converters`](/API.md#value_converters) and [`options`](/API.md#options)). Checkout [API.md](/API.md#rowboat-api) for the full documentation for more details :)
42
42
 
43
43
  ```ruby
44
44
  class ImportProduct
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- row_boat (0.1.0)
4
+ row_boat (0.2.0)
5
5
  activerecord (>= 5.0.0)
6
6
  activerecord-import (~> 0.18.2)
7
7
  smarter_csv (~> 1.1)
data/lib/row_boat/base.rb CHANGED
@@ -6,6 +6,8 @@ require "smarter_csv"
6
6
 
7
7
  module RowBoat
8
8
  class Base
9
+ InvalidColumnMapping = Class.new(StandardError)
10
+
9
11
  attr_reader :csv_source
10
12
 
11
13
  class << self
@@ -120,6 +122,7 @@ module RowBoat
120
122
  # @see #preprocess_row
121
123
  def preprocess_rows(rows)
122
124
  rows.each_with_object([]) do |row, preprocessed_rows|
125
+ increment_row_number
123
126
  preprocessed_row = preprocess_row(row)
124
127
  preprocessed_rows << preprocessed_row unless preprocessed_row.nil?
125
128
  end
@@ -153,13 +156,11 @@ module RowBoat
153
156
  def default_options
154
157
  {
155
158
  chunk_size: 500,
156
- key_mapping: column_mapping,
157
159
  recursive: true,
158
- remove_unmapped_keys: true,
159
160
  validate: true,
160
161
  value_converters: csv_value_converters,
161
162
  wrap_in_transaction: true
162
- }
163
+ }.merge(column_mapping_options)
163
164
  end
164
165
 
165
166
  # @api private
@@ -229,6 +230,28 @@ module RowBoat
229
230
 
230
231
  private
231
232
 
233
+ # @private
234
+ attr_reader :row_number
235
+
236
+ # @api private
237
+ # @private
238
+ def increment_row_number
239
+ @row_number = row_number.to_i + 1
240
+ end
241
+
242
+ # @api private
243
+ # @private
244
+ def column_mapping_options
245
+ case column_mapping
246
+ when Hash
247
+ { key_mapping: column_mapping, remove_unmapped_keys: true }
248
+ when Array
249
+ { user_provided_headers: column_mapping }
250
+ else
251
+ raise InvalidColumnMapping, "#column_mapping must be a Hash or an Array: got `#{column_mapping}`"
252
+ end
253
+ end
254
+
232
255
  # @api private
233
256
  # @private
234
257
  def not_implemented_error_message(method_name)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RowBoat
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: row_boat
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
  - Michael Crismali
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-16 00:00:00.000000000 Z
11
+ date: 2017-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord