data_seeder 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -9
- data/lib/data_seeder/loader/csv.rb +5 -0
- data/lib/data_seeder/loader.rb +16 -3
- data/lib/data_seeder/version.rb +1 -1
- data/lib/data_seeder.rb +13 -8
- data/test/dummy/log/test.log +55480 -0
- data/test/models/data_seeder_test.rb +25 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe35b0a06aa428694ae185ff9f85906d7130b57a
|
4
|
+
data.tar.gz: 4dcb35cdf6202ea08fd5513f9bc09988f00fe40c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f61bf04fde69ddad46a8f18e2b45ee688d875886d282e17cc297736c15cfa202bfef7bea08ff48596d138db79fd7cf5a30f61f3afb7d1e41f02a34ab81e1d7e
|
7
|
+
data.tar.gz: e6ea30bcdcf7ffd25d4be978695fca17b5d770a23f2b08ccc2fbc4e2677d231b06d20f0113d031471769cde7c127bd3569a189ac8e64c67c6ed603a19b03b6e6
|
data/README.md
CHANGED
@@ -82,16 +82,18 @@ data_seeder has default loaders for txt, csv, json and yml extensions but you ca
|
|
82
82
|
your own custom loaders.
|
83
83
|
For instance, suppose you had the following tables:
|
84
84
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
end
|
90
|
-
add_index "app_errors", ["app_id"], name: "index_app_errors_on_app_id"
|
85
|
+
```ruby
|
86
|
+
create_table "apps", force: :cascade do |t|
|
87
|
+
t.string "name"
|
88
|
+
end
|
91
89
|
|
92
|
-
|
93
|
-
|
94
|
-
|
90
|
+
create_table "app_errors", force: :cascade do |t|
|
91
|
+
t.integer "app_id"
|
92
|
+
t.string "code"
|
93
|
+
t.string "message"
|
94
|
+
end
|
95
|
+
add_index "app_errors", ["app_id"], name: "index_app_errors_on_app_id"
|
96
|
+
```
|
95
97
|
|
96
98
|
And you wanted to load up separate error messages for each app such as the following 2 files:
|
97
99
|
|
@@ -223,6 +225,8 @@ seeding above.
|
|
223
225
|
|
224
226
|
Allow config-driven initialization so that we could require: false in the Gemfile and only load as needed.
|
225
227
|
|
228
|
+
Add depends_on option.
|
229
|
+
|
226
230
|
Meta
|
227
231
|
----
|
228
232
|
|
data/lib/data_seeder/loader.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'english'
|
2
|
+
|
1
3
|
module DataSeeder
|
2
4
|
module Loader
|
3
5
|
attr_accessor :file_config, :key_attribute
|
@@ -32,8 +34,11 @@ module DataSeeder
|
|
32
34
|
dot_index = @path.rindex('.')
|
33
35
|
@path_minus_ext = @path[0, dot_index]
|
34
36
|
@file_config = {}
|
37
|
+
cfg_file = "#{@path_minus_ext}.cfg"
|
38
|
+
@file_config = eval(File.read(cfg_file)) if File.exist?(cfg_file)
|
35
39
|
File.open(@path, 'r') do |fin|
|
36
|
-
load_file_config(fin)
|
40
|
+
load_file_config(fin) if @file_config.empty?
|
41
|
+
@file_config = ActiveSupport::HashWithIndifferentAccess.new(@file_config)
|
37
42
|
setup
|
38
43
|
load(fin)
|
39
44
|
teardown
|
@@ -84,9 +89,17 @@ module DataSeeder
|
|
84
89
|
throw 'Must override load'
|
85
90
|
end
|
86
91
|
|
92
|
+
def line_number
|
93
|
+
$INPUT_LINE_NUMBER
|
94
|
+
end
|
95
|
+
|
87
96
|
def save(attr)
|
88
|
-
|
89
|
-
|
97
|
+
if @file_config[:use_line_number_as_id]
|
98
|
+
key = self.line_number
|
99
|
+
else
|
100
|
+
key = attr[@key_attribute.to_s] || attr[@key_attribute.to_sym]
|
101
|
+
raise "No #{@key_attribute} in #{attr.inspect}" unless key
|
102
|
+
end
|
90
103
|
@old_keys.delete(key.to_s)
|
91
104
|
model = self.klass.find_or_initialize_by(@key_attribute => key)
|
92
105
|
model.attributes = attr
|
data/lib/data_seeder/version.rb
CHANGED
data/lib/data_seeder.rb
CHANGED
@@ -8,6 +8,8 @@ module DataSeeder
|
|
8
8
|
attr_writer :config
|
9
9
|
end
|
10
10
|
|
11
|
+
@@mutex = Mutex.new
|
12
|
+
|
11
13
|
def self.config
|
12
14
|
@config ||= Config.new
|
13
15
|
end
|
@@ -25,17 +27,20 @@ module DataSeeder
|
|
25
27
|
end
|
26
28
|
|
27
29
|
def self.run(new_config={})
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
Dir
|
34
|
-
|
30
|
+
@@mutex.synchronize do
|
31
|
+
msec = Benchmark.ms do
|
32
|
+
new_config.each do |key, value|
|
33
|
+
self.config.send("#{key}=", value)
|
34
|
+
end
|
35
|
+
Dir.chdir(config.seed_dir) do
|
36
|
+
Dir['**/*'].each do |path|
|
37
|
+
next if path.end_with?('.cfg')
|
38
|
+
SeedFile.load(path) if File.file?(path)
|
39
|
+
end
|
35
40
|
end
|
36
41
|
end
|
42
|
+
logger.info { "DataSeeder.run took #{msec.to_i} msec" }
|
37
43
|
end
|
38
|
-
logger.info { "DataSeeder.run took #{msec.to_i} msec" }
|
39
44
|
end
|
40
45
|
|
41
46
|
def self.test_run(new_config={})
|