Sha256: 69e28f1af2faedf271ad53d7b56192dc5687d6202bae5103a536b9a07fe5c2b6

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module Databasion
  
  class LoadData
    
    @@config = nil
    
    def self.config=(config)
      @@config = config
    end
    
    def self.config
      @@config
    end
   
    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
    
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
databasion-0.1.1 lib/databasion/load_data.rb