Class: DataSeeder::SeedFile

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/data_seeder/seed_file.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_processed(path) ⇒ Object



24
25
26
# File 'app/models/data_seeder/seed_file.rb', line 24

def self.add_processed(path)
  self.processed_set.add(path)
end

.file_hashObject



5
6
7
8
9
10
11
12
13
# File 'app/models/data_seeder/seed_file.rb', line 5

def self.file_hash
  @file_hash ||= begin
    hash = Hash.new { |h, path| h[path] = new(path: path) }
    all.each do |seed_file|
      hash[seed_file.path] = seed_file
    end
    hash
  end
end

.load(path) ⇒ Object



15
16
17
18
# File 'app/models/data_seeder/seed_file.rb', line 15

def self.load(path)
  seed_file = self.file_hash[path]
  return seed_file.load
end

.processed?(paths) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'app/models/data_seeder/seed_file.rb', line 28

def self.processed?(paths)
  self.processed_set.proper_superset?(Array(paths).to_set)
end

.processed_setObject



20
21
22
# File 'app/models/data_seeder/seed_file.rb', line 20

def self.processed_set
  @processed_set ||= Set.new
end

.resetObject



32
33
34
# File 'app/models/data_seeder/seed_file.rb', line 32

def self.reset
  @file_hash = nil
end

Instance Method Details

#loadObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/data_seeder/seed_file.rb', line 36

def load
  new_sha256 = Digest::SHA256.file(path).hexdigest
  dot_index      = path.rindex('.')
  path_minus_ext = path[0, dot_index]
  if self.sha256 != new_sha256
    cfg_file       = "#{path_minus_ext}.cfg"
    config         = {}
    if File.exist?(cfg_file)
      config = eval(File.read(cfg_file))
    end
    begin
      config[:klass] ||= path_minus_ext.classify.constantize
    rescue NameError => _
    end
    ext = File.extname(self.path)[1..-1]
    return true unless ext
    loader_klass = config[:loader] || DataSeeder.config.loaders[ext]
    unless loader_klass
      DataSeeder.config.logger.warn "Warning: No loader for #{path}"
      return true
    end
    # This is deprecated.  Use instance method instead so seeds can be manually loaded
    if loader_klass.respond_to?(:default_config)
      config = loader_klass.default_config.merge(config)
    end
    config[:path]           = path
    config[:path_minus_ext] = path_minus_ext
    loader                  = loader_klass.new(config)
    depends                 = loader.config[:depends]
    return false if depends && !self.class.processed?(depends)
    DataSeeder.config.logger.info { "Loading #{path}" }
    DataSeeder.config.log_indent do
      File.open(path, 'r', **(config[:open_options] || {})) do |io|
        loader.process(io)
      end
      self.sha256 = new_sha256
      save!
    end
  end
  self.class.add_processed(path_minus_ext)
  return true
end