Module: DataSeeder

Defined in:
lib/data_seeder.rb,
lib/data_seeder/config.rb,
lib/data_seeder/engine.rb,
lib/data_seeder/loader.rb,
lib/data_seeder/version.rb,
lib/data_seeder/loader/csv.rb,
lib/data_seeder/loader/txt.rb,
lib/data_seeder/loader/json.rb,
lib/data_seeder/loader/yaml.rb,
app/models/data_seeder/seed_file.rb

Defined Under Namespace

Modules: Loader Classes: Config, Engine, SeedFile

Constant Summary collapse

VERSION =
'1.1.0'
@@mutex =
Mutex.new
@@a_ord =
?A.ord
@@zero_ord =
?0.ord
@@numeric_range =
(?0.ord)..(?9.ord)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject



12
13
14
# File 'lib/data_seeder.rb', line 12

def self.config
  @config ||= Config.new
end

Class Method Details

.configure {|config| ... } ⇒ Object

Yields:



21
22
23
# File 'lib/data_seeder.rb', line 21

def self.configure
  yield(config)
end

.quiet_run(new_config = {}) ⇒ Object



72
73
74
# File 'lib/data_seeder.rb', line 72

def self.quiet_run(new_config={})
  run({logger: Rails.logger}.merge(new_config))
end

.resetObject



16
17
18
19
# File 'lib/data_seeder.rb', line 16

def self.reset
  @config = Config.new
  SeedFile.reset
end

.run(new_config = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
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
# File 'lib/data_seeder.rb', line 25

def self.run(new_config={})
  @@mutex.synchronize do
    msec = Benchmark.ms do
      new_config.each do |key, value|
        self.config.send("#{key}=", value)
      end
      # Keep track of the seed files that have dependencies that aren't fulfilled
      pending = []
      config.seed_dirs.each do |seed_dir|
        Dir.chdir(seed_dir) do
          Dir['**/*'].each do |path|
            next if path.end_with?('.cfg')
            if File.file?(path)
              unless SeedFile.load(path)
                pending << [seed_dir, path]
              end
            end
          end
        end
      end
      # Loop thru the ones that couldn't be processed previously because they depended on another seed being loaded first
      until pending.empty?
        new_pending = []
        pending.each do |seed_dir, path|
          Dir.chdir(seed_dir) do
            unless SeedFile.load(path)
              new_pending << [seed_dir, path]
            end
          end
        end
        if pending.size == new_pending.size
          msg = "Error: Circular dependency in DataSeeder, seeds=#{pending.inspect}"
          config.logger.error msg
          raise msg
        end
        pending = new_pending
      end
    end
    config.logger.info "DataSeeder.run took #{msec.to_i} msec"
  end
end

.test_run(new_config = {}) ⇒ Object

Deprecated



68
69
70
# File 'lib/data_seeder.rb', line 68

def self.test_run(new_config={})
  quiet_run(new_config)
end

.to_id(len, str) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/data_seeder.rb', line 80

def self.to_id(len, str)
  id = 0
  str = str.upcase.gsub(/[^A-Z0-9]/, '')
  len.times do |i|
    char = str[i]
    if char
      ord = char.ord
      if @@numeric_range.include?(ord)
        id = id * 37 + ord - @@zero_ord
      else
        id = id * 37 + ord - @@a_ord + 10
      end
    else
      id = id * 37 + 36
    end
  end
  return id
end