application_seeds 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +6 -0
- data/lib/application_seeds/version.rb +1 -1
- data/lib/application_seeds.rb +111 -84
- metadata +3 -8
data/CHANGELOG.md
CHANGED
data/lib/application_seeds.rb
CHANGED
@@ -141,6 +141,9 @@ module ApplicationSeeds
|
|
141
141
|
# the dataset could not be found.
|
142
142
|
#
|
143
143
|
def dataset=(dataset)
|
144
|
+
clear_cached_data
|
145
|
+
@dataset = dataset
|
146
|
+
|
144
147
|
if dataset.nil? || dataset.strip.empty? || dataset_path(dataset).nil?
|
145
148
|
datasets = Dir[File.join(seed_data_path, "**", "*")].select { |x| File.directory?(x) }.map { |x| File.basename(x) }.join(', ')
|
146
149
|
|
@@ -150,11 +153,7 @@ module ApplicationSeeds
|
|
150
153
|
raise error_message
|
151
154
|
end
|
152
155
|
|
153
|
-
store_dataset
|
154
|
-
find_seed_data_files(dataset)
|
155
|
-
process_labels(dataset)
|
156
|
-
load_seed_data(dataset)
|
157
|
-
@dataset = dataset
|
156
|
+
store_dataset
|
158
157
|
end
|
159
158
|
|
160
159
|
#
|
@@ -190,7 +189,7 @@ module ApplicationSeeds
|
|
190
189
|
# ApplicationSeeds.seed_data_exists?(:campaigns)
|
191
190
|
#
|
192
191
|
def seed_data_exists?(type)
|
193
|
-
|
192
|
+
!processed_seed_data[type.to_s].nil?
|
194
193
|
end
|
195
194
|
|
196
195
|
#
|
@@ -218,46 +217,92 @@ module ApplicationSeeds
|
|
218
217
|
|
219
218
|
private
|
220
219
|
|
221
|
-
def
|
222
|
-
|
220
|
+
def dataset_path(dataset)
|
221
|
+
Dir[File.join(seed_data_path, "**", "*")].select { |x| File.directory?(x) && File.basename(x) == dataset }.first
|
223
222
|
end
|
224
223
|
|
225
|
-
def
|
226
|
-
|
227
|
-
|
224
|
+
def store_dataset
|
225
|
+
Database.create_metadata_table
|
226
|
+
Database.connection.exec("INSERT INTO application_seeds (dataset) VALUES ('#{@dataset}');")
|
227
|
+
end
|
228
228
|
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
229
|
+
def seed_data_path
|
230
|
+
return @seed_data_path unless @seed_data_path.nil?
|
231
|
+
|
232
|
+
if data_directory
|
233
|
+
@seed_data_path = data_directory
|
234
|
+
else
|
235
|
+
spec = Gem::Specification.find_by_name(data_gem_name)
|
236
|
+
@seed_data_path = File.join(spec.gem_dir, "lib", "seeds")
|
237
|
+
end
|
238
|
+
@seed_data_path
|
239
|
+
end
|
240
|
+
|
241
|
+
def seed_data_files
|
242
|
+
return @seed_data_files unless @seed_data_files.nil?
|
243
|
+
|
244
|
+
@seed_data_files = []
|
245
|
+
path = dataset_path(@dataset)
|
246
|
+
while (seed_data_path != path) do
|
247
|
+
@seed_data_files.concat(Dir[File.join(path, "*.yml")])
|
248
|
+
path.sub!(/\/[^\/]+$/, "")
|
249
|
+
end
|
250
|
+
@seed_data_files
|
251
|
+
end
|
252
|
+
|
253
|
+
def raw_seed_data
|
254
|
+
return @raw_seed_data unless @raw_seed_data.nil?
|
255
|
+
|
256
|
+
@raw_seed_data = {}
|
257
|
+
seed_data_files.each do |seed_file|
|
258
|
+
data = YAML.load(ERB.new(File.read(seed_file)).result)
|
259
|
+
if data
|
260
|
+
@raw_seed_data[seed_file] = data
|
261
|
+
end
|
262
|
+
end
|
263
|
+
@raw_seed_data
|
264
|
+
end
|
265
|
+
|
266
|
+
def seed_labels
|
267
|
+
return @seed_labels unless @seed_labels.nil?
|
268
|
+
|
269
|
+
@seed_labels = {}
|
270
|
+
seed_data_files.each do |seed_file|
|
271
|
+
seed_type = File.basename(seed_file, ".yml")
|
272
|
+
@seed_labels[seed_type] ||= {}
|
273
|
+
|
274
|
+
data = raw_seed_data[seed_file]
|
275
|
+
if data
|
276
|
+
data.each do |label, attributes|
|
277
|
+
specified_id = attributes['id']
|
278
|
+
ids = specified_id.nil? ? generate_unique_ids(seed_type, label) : generate_ids(specified_id)
|
279
|
+
@seed_labels[seed_type][label] = ids
|
280
|
+
end
|
240
281
|
end
|
241
282
|
end
|
283
|
+
@seed_labels
|
242
284
|
end
|
243
285
|
|
244
|
-
def
|
245
|
-
@
|
246
|
-
|
286
|
+
def processed_seed_data
|
287
|
+
return @processed_seed_data unless @processed_seed_data.nil?
|
288
|
+
|
289
|
+
@processed_seed_data = {}
|
290
|
+
seed_data_files.each do |seed_file|
|
247
291
|
basename = File.basename(seed_file, ".yml")
|
248
|
-
data =
|
292
|
+
data = raw_seed_data[seed_file]
|
249
293
|
if data
|
250
294
|
data.each do |label, attributes|
|
251
295
|
data[label] = replace_labels_with_ids(attributes)
|
252
296
|
end
|
253
297
|
|
254
|
-
if
|
255
|
-
|
298
|
+
if processed_seed_data[basename].nil?
|
299
|
+
processed_seed_data[basename] = data
|
256
300
|
else
|
257
|
-
|
301
|
+
processed_seed_data[basename] = data.merge(processed_seed_data[basename])
|
258
302
|
end
|
259
303
|
end
|
260
304
|
end
|
305
|
+
@processed_seed_data
|
261
306
|
end
|
262
307
|
|
263
308
|
def replace_labels_with_ids(attributes)
|
@@ -283,8 +328,8 @@ module ApplicationSeeds
|
|
283
328
|
value = value.sub(/\((.*)\)/, "").strip
|
284
329
|
end
|
285
330
|
|
286
|
-
if
|
287
|
-
label_ids =
|
331
|
+
if seed_labels[type]
|
332
|
+
label_ids = seed_labels[type][value.to_s]
|
288
333
|
value = label_ids[id_type(type)] if label_ids
|
289
334
|
end
|
290
335
|
value
|
@@ -300,35 +345,43 @@ module ApplicationSeeds
|
|
300
345
|
value = $1.split(',').map(&:strip)
|
301
346
|
end
|
302
347
|
|
303
|
-
if
|
348
|
+
if seed_labels[type]
|
304
349
|
value = value.map do |v|
|
305
|
-
label_ids =
|
350
|
+
label_ids = seed_labels[type][v.to_s]
|
306
351
|
(label_ids && label_ids[id_type(type)]) || v
|
307
352
|
end
|
308
353
|
end
|
309
354
|
value
|
310
355
|
end
|
311
356
|
|
312
|
-
def
|
313
|
-
|
314
|
-
|
315
|
-
if data_directory
|
316
|
-
@seed_data_path = data_directory
|
317
|
-
else
|
318
|
-
spec = Gem::Specification.find_by_name(data_gem_name)
|
319
|
-
@seed_data_path = File.join(spec.gem_dir, "lib", "seeds")
|
320
|
-
end
|
357
|
+
def method_missing(method, *args)
|
358
|
+
self.send(:seed_data, method, args.shift)
|
321
359
|
end
|
322
360
|
|
323
|
-
def
|
324
|
-
|
361
|
+
def seed_data(type, options)
|
362
|
+
type = type.to_s
|
363
|
+
raise "No seed data file could be found for '#{type}'" if processed_seed_data[type].nil?
|
364
|
+
|
365
|
+
if options.nil?
|
366
|
+
fetch(type)
|
367
|
+
elsif options.is_a?(Fixnum) || options.is_a?(String)
|
368
|
+
fetch_with_id(type, options)
|
369
|
+
elsif options.is_a?(Symbol)
|
370
|
+
fetch_with_label(type, options.to_s)
|
371
|
+
elsif options.is_a? Hash
|
372
|
+
fetch(type) do |attributes|
|
373
|
+
options.stringify_keys!
|
374
|
+
options = replace_labels_with_ids(options)
|
375
|
+
(options.to_a - attributes.to_a).empty?
|
376
|
+
end
|
377
|
+
end
|
325
378
|
end
|
326
379
|
|
327
380
|
def fetch(type, &block)
|
328
381
|
result = {}
|
329
|
-
|
382
|
+
processed_seed_data[type].each do |label, attrs|
|
330
383
|
attributes = attrs.clone
|
331
|
-
id =
|
384
|
+
id = seed_labels[type][label][id_type(type)]
|
332
385
|
if !block_given? || (block_given? && yield(attributes) == true)
|
333
386
|
result[id] = Attributes.new(attributes)
|
334
387
|
end
|
@@ -338,10 +391,10 @@ module ApplicationSeeds
|
|
338
391
|
|
339
392
|
def fetch_with_id(type, id)
|
340
393
|
data = nil
|
341
|
-
|
394
|
+
seed_labels[type].each do |label, ids|
|
342
395
|
if ids.values.map(&:to_s).include?(id.to_s)
|
343
|
-
data =
|
344
|
-
data['id'] =
|
396
|
+
data = processed_seed_data[type][label]
|
397
|
+
data['id'] = seed_labels[type][label][id_type(type)]
|
345
398
|
break
|
346
399
|
end
|
347
400
|
end
|
@@ -350,45 +403,12 @@ module ApplicationSeeds
|
|
350
403
|
end
|
351
404
|
|
352
405
|
def fetch_with_label(type, label)
|
353
|
-
data =
|
406
|
+
data = processed_seed_data[type][label]
|
354
407
|
raise "No seed data could be found for '#{type}' with label #{label}" if data.nil?
|
355
|
-
data['id'] =
|
408
|
+
data['id'] = seed_labels[type][label][id_type(type)]
|
356
409
|
Attributes.new(data)
|
357
410
|
end
|
358
411
|
|
359
|
-
def store_dataset(dataset)
|
360
|
-
Database.create_metadata_table
|
361
|
-
Database.connection.exec("INSERT INTO application_seeds (dataset) VALUES ('#{dataset}');")
|
362
|
-
end
|
363
|
-
|
364
|
-
def find_seed_data_files(dataset)
|
365
|
-
@seed_data_files = []
|
366
|
-
path = dataset_path(dataset)
|
367
|
-
while (seed_data_path != path) do
|
368
|
-
@seed_data_files.concat(Dir[File.join(path, "*.yml")])
|
369
|
-
path.sub!(/\/[^\/]+$/, "")
|
370
|
-
end
|
371
|
-
end
|
372
|
-
|
373
|
-
def process_labels(dataset)
|
374
|
-
return @seed_labels unless @seed_labels.nil?
|
375
|
-
|
376
|
-
@seed_labels = {}
|
377
|
-
@seed_data_files.each do |seed_file|
|
378
|
-
seed_type = File.basename(seed_file, ".yml")
|
379
|
-
@seed_labels[seed_type] = {}
|
380
|
-
|
381
|
-
data = YAML.load(File.read(seed_file))
|
382
|
-
if data
|
383
|
-
data.each do |label, attributes|
|
384
|
-
specified_id = attributes['id']
|
385
|
-
ids = specified_id.nil? ? generate_unique_ids(seed_type, label) : generate_ids(specified_id)
|
386
|
-
@seed_labels[seed_type][label] = ids
|
387
|
-
end
|
388
|
-
end
|
389
|
-
end
|
390
|
-
end
|
391
|
-
|
392
412
|
MAX_ID = 2 ** 30 - 1
|
393
413
|
def generate_unique_ids(seed_type, label)
|
394
414
|
checksum = Zlib.crc32(seed_type + label) % MAX_ID
|
@@ -402,5 +422,12 @@ module ApplicationSeeds
|
|
402
422
|
def id_type(type)
|
403
423
|
self.config["#{type}_id_type".to_sym] || self.config[:id_type]
|
404
424
|
end
|
425
|
+
|
426
|
+
def clear_cached_data
|
427
|
+
@seed_labels = nil
|
428
|
+
@processed_seed_data = nil
|
429
|
+
@raw_seed_data = nil
|
430
|
+
@seed_data_files = nil
|
431
|
+
end
|
405
432
|
end
|
406
433
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: application_seeds
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-02-
|
12
|
+
date: 2014-02-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -118,18 +118,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
118
118
|
- - ! '>='
|
119
119
|
- !ruby/object:Gem::Version
|
120
120
|
version: '0'
|
121
|
-
segments:
|
122
|
-
- 0
|
123
|
-
hash: -4329626618656842482
|
124
121
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
122
|
none: false
|
126
123
|
requirements:
|
127
124
|
- - ! '>='
|
128
125
|
- !ruby/object:Gem::Version
|
129
126
|
version: '0'
|
130
|
-
segments:
|
131
|
-
- 0
|
132
|
-
hash: -4329626618656842482
|
133
127
|
requirements: []
|
134
128
|
rubyforge_project:
|
135
129
|
rubygems_version: 1.8.23
|
@@ -147,3 +141,4 @@ test_files:
|
|
147
141
|
- spec/seed_data/test_inheritable_data_set/level_2/departments.yml
|
148
142
|
- spec/seed_data/test_inheritable_data_set/level_2/level_3/people.yml
|
149
143
|
- spec/seed_data/test_inheritable_data_set/level_2/people.yml
|
144
|
+
has_rdoc:
|