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[@@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' % [@@config['output']['yaml_path'], plural_name])
    rescue
      yaml_file = YAML.load_file('%s/%s.yml' % [@@config['output']['yaml_path'], normal_name])
    end

    for row in yaml_file['data']
      klass = eval("%s.new" % camel_name)
      model = camel_name.constantize.find(:first, :conditions => ['id = ?', row['id']])
      if model
        camel_name.constantize.update(model.id, row)
      else
        klass.id = row['id']
        klass.update_attributes(row)
      end
    end if yaml_file['data']
  end
end