Sha256: cb855927c01756d53933a21e4a955828fa536baa1854cf109b26b4b05981c04d

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

require 'data_seeder/config'
require 'data_seeder/engine'
require 'data_seeder/loader'
require 'data_seeder/logger'

module DataSeeder
  class << self
    attr_writer :config
  end

  @@mutex = Mutex.new

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

  def self.reset
    @config = Config.new
  end

  def self.configure
    yield(config)
  end

  def self.logger
    config.logger
  end

  def self.run(new_config={})
    @@mutex.synchronize do
      msec = Benchmark.ms do
        new_config.each do |key, value|
          self.config.send("#{key}=", value)
        end
        Dir.chdir(config.seed_dir) do
          Dir['**/*'].each do |path|
            next if path.end_with?('.cfg')
            SeedFile.load(path) if File.file?(path)
          end
        end
      end
      logger.info { "DataSeeder.run took #{msec.to_i} msec" }
    end
  end

  def self.test_run(new_config={})
    self.config.logger = Rails.logger
    run(new_config)
  end

  @@a_ord         = ?A.ord
  @@zero_ord      = ?0.ord
  @@numeric_range = (?0.ord)..(?9.ord)

  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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
data_seeder-0.0.5 lib/data_seeder.rb
data_seeder-0.0.4 lib/data_seeder.rb
data_seeder-0.0.3 lib/data_seeder.rb
data_seeder-0.0.2 lib/data_seeder.rb