csv_step_importer 0.11.2 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -0
- data/Gemfile.lock +1 -1
- data/lib/csv_step_importer/base.rb +6 -0
- data/lib/csv_step_importer/model/dao.rb +12 -13
- data/lib/csv_step_importer/model/importable_model.rb +7 -21
- data/lib/csv_step_importer/model/model.rb +12 -14
- data/lib/csv_step_importer/node.rb +1 -1
- data/lib/csv_step_importer/row.rb +5 -0
- 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: c7e47c79367ebc96d77f98f8a222efed6058b41e297fd330a5819a3e7449214a
|
4
|
+
data.tar.gz: c60c54fbca29a869584a8ef08bd0149a6d90c4f4f6bfafc6addf89847922c740
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d03d12132eeccfa84468e1f861f25bc462b1d00957f3ccb0719d40437d6a23a8058a35997ada9eadeaaf087fb7051f5950c4a35780404426fcb4df99381ca19d
|
7
|
+
data.tar.gz: aefe551280cb52ca746f57d4d5c5bcd13ade807b3bececd1561fe81bfeea953a93a5ee3b7184b97a4d39d6f1ff91000ee5f12db83d704607f065395e78959e6a
|
data/CHANGELOG.md
CHANGED
@@ -141,3 +141,19 @@ See lib/csv_step_importer/file.rb for more options
|
|
141
141
|
- Revert "ImportableModel's Importer (uses ActiveRecord::Import) now raises an exception if the import fails"
|
142
142
|
Since import! runs validations on the model, I reverted import! to import (validations should be performed inside csv_step_importers logic)
|
143
143
|
|
144
|
+
## 2018-09-14 Version 0.12.0
|
145
|
+
### Added
|
146
|
+
- Added a `set` method to Base, in order to make settings shorter
|
147
|
+
Usage:
|
148
|
+
```ruby
|
149
|
+
class X < Node
|
150
|
+
set :config_a, true
|
151
|
+
set :config_b, -> { row.some_array.first }
|
152
|
+
end
|
153
|
+
```
|
154
|
+
- Added dao_for to Row
|
155
|
+
### Changed
|
156
|
+
- Changed settings to use the new set method
|
157
|
+
- dao_for's interface changed
|
158
|
+
Before: `dao_for(model: some_model_class_or_instance, pluralize: optional_boolean)`
|
159
|
+
After: `dao_for(some_model_class_or_instance, pluralize: optional_boolean)`
|
data/Gemfile.lock
CHANGED
@@ -12,6 +12,12 @@ module CSVStepImporter
|
|
12
12
|
class CSVImportError < RuntimeError; end
|
13
13
|
class CSVFileImportError < CSVImportError; end
|
14
14
|
|
15
|
+
# defines a method with the specified proc or a proc returning the value of the second attribute
|
16
|
+
def self.set name, value_or_proc
|
17
|
+
procedure = value_or_proc.is_a?(Proc) ? value_or_proc : -> { value_or_proc }
|
18
|
+
define_method name, procedure
|
19
|
+
end
|
20
|
+
|
15
21
|
def assign_attributes(attributes)
|
16
22
|
attributes.each do |key, value|
|
17
23
|
send("#{key}=", value)
|
@@ -6,9 +6,21 @@ module CSVStepImporter
|
|
6
6
|
attr_accessor :id, :row, :attributes
|
7
7
|
|
8
8
|
delegate :columns, to: :model
|
9
|
+
delegate :dao_for, to: :row
|
9
10
|
|
10
11
|
validates :row, presence: true
|
11
12
|
|
13
|
+
#########################################################
|
14
|
+
# Configuration
|
15
|
+
#########################################################
|
16
|
+
|
17
|
+
set :created_at, -> { current_timestamp }
|
18
|
+
set :updated_at, -> { current_timestamp }
|
19
|
+
|
20
|
+
#########################################################
|
21
|
+
# Logic
|
22
|
+
#########################################################
|
23
|
+
|
12
24
|
def initialize(parent:, row:, **attributes)
|
13
25
|
super parent: parent
|
14
26
|
|
@@ -51,19 +63,6 @@ module CSVStepImporter
|
|
51
63
|
model.cache[:updated_at] ||= (::ActiveRecord::Base.default_timezone == :utc ? ::Time.now.utc : ::Time.now).to_s(:db)
|
52
64
|
end
|
53
65
|
|
54
|
-
def created_at
|
55
|
-
current_timestamp
|
56
|
-
end
|
57
|
-
|
58
|
-
def updated_at
|
59
|
-
current_timestamp
|
60
|
-
end
|
61
|
-
|
62
|
-
# retrieve a dao for a different model using the same CSV row. This is useful e.g. if you use the reflector to get ids of related data
|
63
|
-
def dao_for(model:, pluralize: false)
|
64
|
-
row.cache[model.cache_key(pluralize: pluralize)]
|
65
|
-
end
|
66
|
-
|
67
66
|
# link this dao to a row
|
68
67
|
def link!
|
69
68
|
# add to cache with pluralized key
|
@@ -8,45 +8,31 @@ module CSVStepImporter
|
|
8
8
|
#########################################################
|
9
9
|
|
10
10
|
# example: User
|
11
|
-
|
12
|
-
raise "please extend and implement"
|
13
|
-
end
|
11
|
+
set :model_class, -> { raise "please extend and implement" }
|
14
12
|
|
15
13
|
# set to nil in order to deactivate
|
16
|
-
|
17
|
-
CSVStepImporter::Model::Importer
|
18
|
-
end
|
14
|
+
set :importer_class, CSVStepImporter::Model::Importer
|
19
15
|
|
20
16
|
# return CSVStepImporter::Model::Reflector in order to enable reflections (e.g. get ids of all rows)
|
21
17
|
# disabled by default
|
22
|
-
|
23
|
-
nil
|
24
|
-
end
|
18
|
+
set :reflector_class, nil
|
25
19
|
|
26
20
|
# NOTE: required only when reflector class is set
|
27
21
|
# example: env[:company].company_users
|
28
|
-
|
29
|
-
model_class.all
|
30
|
-
end
|
22
|
+
set :finder_scope, -> { model_class.all }
|
31
23
|
|
32
24
|
# NOTE: required only when reflector class is set
|
33
25
|
# example: [:email]
|
34
|
-
|
35
|
-
composite_key_columns || raise("please extend and implement")
|
36
|
-
end
|
26
|
+
set :finder_keys, -> { composite_key_columns || raise("please extend and implement") }
|
37
27
|
|
38
28
|
# NOTE: required only when importer class is set
|
39
29
|
# see: https://github.com/zdennis/activerecord-import/wiki/On-Duplicate-Key-Update
|
40
|
-
|
41
|
-
false
|
42
|
-
end
|
30
|
+
set :on_duplicate_key_ignore, false
|
43
31
|
|
44
32
|
# NOTE: required only when importer class is set
|
45
33
|
# example [:email, :updated_at]
|
46
34
|
# see: https://github.com/zdennis/activerecord-import/wiki/On-Duplicate-Key-Update
|
47
|
-
|
48
|
-
raise "please extend and implement"
|
49
|
-
end
|
35
|
+
set :on_duplicate_key_update, -> { raise "please extend and implement" }
|
50
36
|
|
51
37
|
#########################################################
|
52
38
|
# Logic
|
@@ -9,6 +9,18 @@ module CSVStepImporter
|
|
9
9
|
delegate :rows, :cache, to: :parent
|
10
10
|
delegate :cache_key, to: :class
|
11
11
|
|
12
|
+
#########################################################
|
13
|
+
# Configuration
|
14
|
+
#########################################################
|
15
|
+
|
16
|
+
set :columns, -> { raise "please extend and implement" } # example: [:email, :updated_at, :created_at]
|
17
|
+
set :composite_key_columns, nil # specify to an array of columns in order filter duplicates from daos
|
18
|
+
set :dao_class, CSVStepImporter::Model::DAO
|
19
|
+
|
20
|
+
#########################################################
|
21
|
+
# Logic
|
22
|
+
#########################################################
|
23
|
+
|
12
24
|
def initialize(**attributes)
|
13
25
|
super **attributes
|
14
26
|
|
@@ -26,20 +38,6 @@ module CSVStepImporter
|
|
26
38
|
(pluralize ? key.pluralize : key.singularize).to_sym
|
27
39
|
end
|
28
40
|
|
29
|
-
# example: [:email, :updated_at, :created_at]
|
30
|
-
def columns
|
31
|
-
raise "please extend and implement"
|
32
|
-
end
|
33
|
-
|
34
|
-
def dao_class
|
35
|
-
CSVStepImporter::Model::DAO
|
36
|
-
end
|
37
|
-
|
38
|
-
# specify to an array of columns in order filter duplicates from daos
|
39
|
-
def composite_key_columns
|
40
|
-
nil
|
41
|
-
end
|
42
|
-
|
43
41
|
#########################################################
|
44
42
|
# Logic
|
45
43
|
#########################################################
|
@@ -16,5 +16,10 @@ module CSVStepImporter
|
|
16
16
|
# Rowの保存処理は基本的にstepsで行います
|
17
17
|
true
|
18
18
|
end
|
19
|
+
|
20
|
+
# retrieve a dao for a different model using the same CSV row. This is useful e.g. if you use the reflector to get ids of related data
|
21
|
+
def dao_for(model, pluralize: false)
|
22
|
+
cache[model.cache_key(pluralize: pluralize)]
|
23
|
+
end
|
19
24
|
end
|
20
25
|
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.12.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-09-
|
11
|
+
date: 2018-09-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|