csv_step_importer 0.7.5 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +27 -0
- data/lib/csv_step_importer/file.rb +37 -9
- data/lib/csv_step_importer/version.rb +1 -1
- 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: be53e398ba3edb80965c62aae4b900166c45c348ed3bd6dadd432f4c3b0ea9c7
|
4
|
+
data.tar.gz: d29aedb38946e7d4e2e6ae8bdb6bd870a95d336ed712e9628dcb346753f5213b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3ce3c6ac60969835e3582ff5dd7da0b364163918ca0fba0fd49ba30e6a546428ff113966e07c5f6a65f2e4dc84e583b94d34e67c76892477ffa928c1343dd58b
|
7
|
+
data.tar.gz: 6a6bac4957ac2937a658c2089bbae55363fc76efb9d15c934f5f2b01701caca74cc8e08a8e9fee1277dac63fc92ccce14fd0a29e681132309233e2ed914c7a57
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -85,6 +85,33 @@ CSVStepImporter::File.new(path: 'currencies.csv', processor_classes: [Currency::
|
|
85
85
|
puts Currency.all.to_yaml
|
86
86
|
```
|
87
87
|
|
88
|
+
### Simple model
|
89
|
+
|
90
|
+
By default, for each row read from the CSV file, a DAO belonging to a model will be created.
|
91
|
+
These models will be validated and saved in the order specified by the processor_classes option.
|
92
|
+
|
93
|
+
The simplest model is one, which simply calls `save` on all DAOs, which calls internally `create_or_update`.
|
94
|
+
`create_or_update` is customizable.
|
95
|
+
|
96
|
+
Example:
|
97
|
+
|
98
|
+
This example will call `find_or_create_by` for each row after all validations have passed.
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
class SimpleDAO < CSVStepImporter::Model::DAO
|
102
|
+
def create_or_update
|
103
|
+
Currency.find_or_create_by( name: row.name, code: row.code )
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
class SimpleModel < CSVStepImporter::Model::Model
|
108
|
+
def dao_class
|
109
|
+
SimpleDAO
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
CSVStepImporter::File.new(path: 'currencies.csv', processor_classes: [SimpleModel]).save
|
114
|
+
```
|
88
115
|
|
89
116
|
## Development
|
90
117
|
|
@@ -22,10 +22,8 @@ module CSVStepImporter
|
|
22
22
|
self.row_class = row_class || CSVStepImporter::Row
|
23
23
|
self.processor_classes = processor_classes
|
24
24
|
|
25
|
-
self.csv_options =
|
26
|
-
|
27
|
-
file_encoding: "CP932:UTF-8",
|
28
|
-
}.merge(csv_options)
|
25
|
+
self.csv_options = csv_options
|
26
|
+
precompile_csv_options!
|
29
27
|
|
30
28
|
load_csv
|
31
29
|
end
|
@@ -35,11 +33,7 @@ module CSVStepImporter
|
|
35
33
|
|
36
34
|
first_row = 2
|
37
35
|
|
38
|
-
|
39
|
-
options[:header_transformations] ||= []
|
40
|
-
options[:header_transformations] << header_proc
|
41
|
-
|
42
|
-
::SmarterCSV.process(path, **options) do |rows|
|
36
|
+
::SmarterCSV.process(path, **csv_options) do |rows|
|
43
37
|
add_children chunk_class.new(
|
44
38
|
first_row: first_row,
|
45
39
|
parent: self,
|
@@ -78,5 +72,39 @@ module CSVStepImporter
|
|
78
72
|
headers
|
79
73
|
}
|
80
74
|
end
|
75
|
+
|
76
|
+
def keys_as_symbols(case_sensitive: false)
|
77
|
+
if case_sensitive
|
78
|
+
Proc.new { |headers|
|
79
|
+
headers.map { |x| x.strip.gsub(%r{"}, '').gsub(/(\s|\-)+/, '_').to_sym }
|
80
|
+
}
|
81
|
+
else
|
82
|
+
# use existing helper
|
83
|
+
:keys_as_symbols
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def precompile_csv_options!
|
88
|
+
# set default options
|
89
|
+
self.csv_options = {
|
90
|
+
chunk_size: 1000,
|
91
|
+
file_encoding: "CP932:UTF-8",
|
92
|
+
}.merge(csv_options)
|
93
|
+
|
94
|
+
# easier specification of headers, including case case sensitive headers
|
95
|
+
if csv_options[:headers] || csv_options[:case_sensitive_headers]
|
96
|
+
if csv_options[:header_transformations]
|
97
|
+
raise "either use header_transformations or headers (and case_sensitive_headers optionally)"
|
98
|
+
end
|
99
|
+
|
100
|
+
csv_options[:header_transformations] = [ :none ]
|
101
|
+
csv_options[:header_transformations] << { key_mapping: csv_options.delete(:headers) } if csv_options[:headers]
|
102
|
+
csv_options[:header_transformations] << keys_as_symbols(case_sensitive: csv_options.delete(:case_sensitive_headers))
|
103
|
+
end
|
104
|
+
|
105
|
+
# retrieve headers from CSV
|
106
|
+
csv_options[:header_transformations] ||= []
|
107
|
+
csv_options[:header_transformations] << header_proc
|
108
|
+
end
|
81
109
|
end
|
82
110
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv_step_importer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christian-Manuel Butzke
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-06-
|
11
|
+
date: 2018-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|