Class: Databasion::LoadData

Inherits:
Object
  • Object
show all
Defined in:
lib/databasion/load_data.rb

Constant Summary collapse

@@config =
nil

Class Method Summary collapse

Class Method Details

.configObject



11
12
13
# File 'lib/databasion/load_data.rb', line 11

def self.config
  @@config
end

.config=(config) ⇒ Object



7
8
9
# File 'lib/databasion/load_data.rb', line 7

def self.config=(config)
  @@config = config
end

.run(opts) ⇒ Object



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
45
46
# File 'lib/databasion/load_data.rb', line 15

def self.run(opts)
  Databasion.set_ar_logger
  Databasion::LOGGER.info "Updating from YAML..."

  models = Dir[opts[:env] + "/" + @@config['output']['migrations']['models'] + "/*.rb"].each { |file| load file }

  models.each do |model|
    f = model.split('/')
    normal_name = f[f.size-1].split(".")[0]
    plural_name = normal_name.pluralize
    camel_name  = normal_name.camelize

    Databasion::LOGGER.info "Loading %s into database..." % camel_name

    begin
      yaml_file = YAML.load_file('%s/%s.yml' % [opts[:env] + "/" + @@config['output']['yaml_path'], plural_name])
    rescue
      yaml_file = YAML.load_file('%s/%s.yml' % [opts[:env] + "/" + @@config['output']['yaml_path'], normal_name])
    end

    if yaml_file['data']
      camel_name.constantize.delete_all
      yaml_file['data'].each do |row|
        klass = eval("%s.new" % camel_name)
        row.each do |key, value|
          eval("klass.%s = '%s'" % [key, value])
        end
        klass.save
      end
    end
  end
end