row_boat 0.1.0.alpha.2 → 0.1.0.alpha.3

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: a52e0cc320cec32723d8004c8664b20db8b2f08c
4
- data.tar.gz: 44976205d05e7290b9cc520e4af0a2b8bb864928
3
+ metadata.gz: 5341bb8c702726b4d6dd4ed105339121815f41ab
4
+ data.tar.gz: 8bc96cb40cc2f160df283acc73d6e9b767212cad
5
5
  SHA512:
6
- metadata.gz: d0cd75d81d723f853a42d8dd4eeee8a9a03bc783b46778457d6b01db3006b3e276531237f57dec9fb26d36447f14d95b9b138b710bab8e7485caf6f761b03c6c
7
- data.tar.gz: 2dce2747abf8ba12b093a393c6db92a06c7e35a61892184c8c85c4dda557ddec75b9a59446bb38d987e9ca67598111a6972da54c7e11accc124addfa6db962d6
6
+ metadata.gz: 4c75ea55754040da725c9919e70f4d84efecb261db688d9f00142d454d3931bb8e81f8c99903297029349a75f627d68b2d9847ed32663f97901334f00f1baccf
7
+ data.tar.gz: f12351b6332d266d841ad07941fe415c0a5a9d23a35cc1e54996cafda6c91cd5ef4c629ea7fffb48419122fa38137cbe16cd2d0711a76222218b8b8dad29d9bc
data/lib/row_boat/base.rb CHANGED
@@ -27,10 +27,8 @@ module RowBoat
27
27
  end
28
28
  end
29
29
 
30
- import_results.each_with_object(invalid_records: [], total_inserted: 0, inserted_ids: []) do |import_result, total_results|
31
- total_results[:invalid_records] += import_result.failed_instances
32
- total_results[:total_inserted] += import_result.num_inserts
33
- total_results[:inserted_ids] += import_result.ids
30
+ process_import_results(import_results).tap do |total_results|
31
+ handle_failed_rows(total_results[:invalid_records])
34
32
  end
35
33
  end
36
34
 
@@ -47,22 +45,65 @@ module RowBoat
47
45
  end
48
46
 
49
47
  def import_rows(rows)
50
- import_options = ::RowBoat::Helpers.extract_import_options(options)
51
- preprocessed_rows = rows.map { |row| preprocess_row(row) }
48
+ import_options = ::RowBoat::Helpers.extract_import_options(merged_options)
49
+ preprocessed_rows = preprocess_rows(rows)
52
50
  import_into.import(preprocessed_rows, import_options)
53
51
  end
54
52
 
53
+ def preprocess_rows(rows)
54
+ rows.each_with_object([]) do |row, preprocessed_rows|
55
+ preprocessed_row = preprocess_row(row)
56
+ preprocessed_rows << preprocessed_row unless preprocessed_row.nil?
57
+ end
58
+ end
59
+
55
60
  def options
61
+ {}
62
+ end
63
+
64
+ def default_options
56
65
  {
57
66
  chunk_size: 500,
58
67
  key_mapping: column_mapping,
59
68
  recursive: true,
60
69
  remove_unmapped_keys: true,
61
70
  validate: true,
71
+ value_converters: csv_value_converters,
62
72
  wrap_in_transaction: true
63
73
  }
64
74
  end
65
75
 
76
+ def merged_options
77
+ default_options.merge(options)
78
+ end
79
+
80
+ def handle_failed_row(row)
81
+ row
82
+ end
83
+
84
+ def handle_failed_rows(rows)
85
+ rows.each { |row| handle_failed_row(row) }
86
+ end
87
+
88
+ def value_converters
89
+ {}
90
+ end
91
+
92
+ def csv_value_converters
93
+ value_converters.each_with_object({}) do |(key, potential_converter), converters_hash|
94
+ case potential_converter
95
+ when Proc
96
+ converters_hash[key] = ::RowBoat::ValueConverter.new(&potential_converter)
97
+ when Symbol
98
+ converters_hash[key] = ::RowBoat::ValueConverter.new { |value| public_send(potential_converter, value) }
99
+ when nil
100
+ next
101
+ else
102
+ converters_hash[key] = potential_converter
103
+ end
104
+ end
105
+ end
106
+
66
107
  private
67
108
 
68
109
  def not_implemented_error_message(method_name)
@@ -70,12 +111,24 @@ module RowBoat
70
111
  end
71
112
 
72
113
  def parse_rows(&block)
73
- csv_options = ::RowBoat::Helpers.extract_csv_options(options)
114
+ csv_options = ::RowBoat::Helpers.extract_csv_options(merged_options)
74
115
  ::SmarterCSV.process(csv_source, csv_options, &block)
75
116
  end
76
117
 
77
118
  def transaction_if_needed(&block)
78
- options[:wrap_in_transaction] ? import_into.transaction(&block) : yield
119
+ merged_options[:wrap_in_transaction] ? import_into.transaction(&block) : yield
120
+ end
121
+
122
+ def process_import_results(import_results)
123
+ import_results.each_with_object(
124
+ invalid_records: [],
125
+ total_inserted: 0,
126
+ inserted_ids: []
127
+ ) do |import_result, total_results|
128
+ total_results[:invalid_records] += import_result.failed_instances
129
+ total_results[:total_inserted] += import_result.num_inserts
130
+ total_results[:inserted_ids] += import_result.ids
131
+ end
79
132
  end
80
133
  end
81
134
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RowBoat
4
+ class ValueConverter
5
+ attr_reader :converter
6
+
7
+ def initialize(&block)
8
+ @converter = block
9
+ end
10
+
11
+ def convert(value)
12
+ converter.call(value)
13
+ end
14
+ end
15
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RowBoat
4
- VERSION = "0.1.0.alpha.2"
4
+ VERSION = "0.1.0.alpha.3"
5
5
  end
data/lib/row_boat.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "row_boat/version"
4
4
  require "row_boat/helpers"
5
+ require "row_boat/value_converter"
5
6
  require "row_boat/base"
6
7
 
7
8
  module RowBoat
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.alpha.2
4
+ version: 0.1.0.alpha.3
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-11 00:00:00.000000000 Z
11
+ date: 2017-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -231,6 +231,7 @@ files:
231
231
  - lib/row_boat.rb
232
232
  - lib/row_boat/base.rb
233
233
  - lib/row_boat/helpers.rb
234
+ - lib/row_boat/value_converter.rb
234
235
  - lib/row_boat/version.rb
235
236
  - row_boat.gemspec
236
237
  homepage: https://github.com/devmynd/row_boat