data_seeder 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9201eccfce12d45bbe01559b668fb2fa32c704f1
4
- data.tar.gz: a0d02f94fad7dd14f62c05d4ef33223669dc1665
3
+ metadata.gz: fe35b0a06aa428694ae185ff9f85906d7130b57a
4
+ data.tar.gz: 4dcb35cdf6202ea08fd5513f9bc09988f00fe40c
5
5
  SHA512:
6
- metadata.gz: 0933f7b3628701f8ffbef5f5d6247ffed17c997ec2099beefbf2e32d83ee0a84f39256aff8eefaeea1d1e7d29ec959cb6f13b70fc3b3df5c46d2d6d8becb4501
7
- data.tar.gz: 5327311bdba8e1e880d87db269eb2ec69fe834139276dc6398b9fba35f2e07c185fa66fae61c05f0ba67a2451377bd48125cd8b23ccb22c7b33b459cd346cb0f
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
- create_table "app_errors", force: :cascade do |t|
86
- t.integer "app_id"
87
- t.string "code"
88
- t.string "message"
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
- create_table "apps", force: :cascade do |t|
93
- t.string "name"
94
- end
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
 
@@ -5,6 +5,11 @@ module DataSeeder
5
5
  class CSV
6
6
  include Loader
7
7
 
8
+ def line_number
9
+ # Don't count the header
10
+ $INPUT_LINE_NUMBER-1
11
+ end
12
+
8
13
  def load(io)
9
14
  ::CSV.foreach(io, headers: true) do |row|
10
15
  save(row.to_hash)
@@ -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
- key = attr[@key_attribute.to_s] || attr[@key_attribute.to_sym]
89
- raise "No #{@key_attribute} in #{attr.inspect}" unless key
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
@@ -1,3 +1,3 @@
1
1
  module DataSeeder
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
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
- msec = Benchmark.ms do
29
- new_config.each do |key, value|
30
- self.config.send("#{key}=", value)
31
- end
32
- Dir.chdir(config.seed_dir) do
33
- Dir['**/*'].each do |path|
34
- SeedFile.load(path) if File.file?(path)
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={})