Class: Inscriber::Inserter

Inherits:
Object
  • Object
show all
Extended by:
TableHelpers
Defined in:
lib/inscriber/inserter.rb

Class Method Summary collapse

Methods included from TableHelpers

original_column_name, original_table_name

Class Method Details

.insert(database) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/inscriber/inserter.rb', line 7

def insert(database)
  database.locales.each do |locale|
    file_path = "#{database.input_dir}/#{database.file_name}"
    if File.exist? "#{file_path}.#{locale}.yml"
      data = YAML.load_file("#{file_path}.#{locale}.yml")

      data[locale].each do |table, records|
        records_array = []
        records.each do |k,v|
          records_array << v.merge(original_column_name(table) => k)
        end
        opts = { database: database, table: table, records: records_array, locale: locale }
        upload_data_to_db(opts)
      end
    else
      { status: false, error: 'File not found' }
    end
  end
  { status: true } 
end