Module: InitialTestData

Defined in:
lib/initial-test-data/utilities.rb,
lib/initial-test-data/initial_test_data.rb

Defined Under Namespace

Modules: Utilities

Constant Summary collapse

DIGEST_TABLE_NAME =
'_initial_data_digest'
RECORD_IDS =
HashWithIndifferentAccess.new

Class Method Summary collapse

Class Method Details

.import(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/initial-test-data/initial_test_data.rb', line 12

def import(*args)
  @options = args.extract_options!
  @dir = args[0] || 'test'

  klass = define_class

  md5_digest = generate_md5_digest
  md5_digest_cache = klass.first.try(:md5_value)

  needs_reinitialization = true
  record_ids_path = Rails.root.join('tmp', 'initial_data_record_ids.yml')
  if File.exists?(record_ids_path) && md5_digest == md5_digest_cache
    begin
      RECORD_IDS.merge! YAML.load_file(record_ids_path)
      needs_reinitialization = false
    rescue SyntaxError
    end
  end

  if needs_reinitialization
    RECORD_IDS.clear
    initialize_data

    digest = klass.first
    digest ||= klass.new
    digest.md5_value = md5_digest
    digest.save

    File.open(record_ids_path, 'w') do |f|
      f.write RECORD_IDS.to_hash.to_yaml
    end
  end
end