Module: Dunlop::SourceFileModel

Extended by:
ActiveSupport::Concern
Includes:
ConversionHelpers
Included in:
SourceFile
Defined in:
app/models/dunlop/source_file_model.rb

Defined Under Namespace

Modules: ClassMethods, ConversionHelpers

Instance Method Summary collapse

Methods included from ConversionHelpers

#percent_to_decimal, #true_or_false

Instance Method Details

#after_activate_hookObject

Empty hook placeholder to be overwritten by subclass



88
89
# File 'app/models/dunlop/source_file_model.rb', line 88

def after_activate_hook
end

#assert_first_line(expected, target_path = working_file.path) ⇒ Object



61
62
63
64
# File 'app/models/dunlop/source_file_model.rb', line 61

def assert_first_line(expected, target_path = working_file.path)
  first_line = File.open(target_path, &:readline).to_s.strip
  raise "Header of #{self.class.model_name.human} is not as expected:\n\n #{expected}\n\nActual value was:\n#{first_line}" unless first_line == expected
end

#assert_first_line_starts_with(expected, target_path = working_file.path) ⇒ Object



66
67
68
69
# File 'app/models/dunlop/source_file_model.rb', line 66

def assert_first_line_starts_with(expected, target_path = working_file.path)
  first_line = File.open(target_path, &:readline).to_s.strip[0...expected.length]
  raise "First header part of file #{self.class.model_name.human} is not as expected:\n\n #{expected}\n\nActual value was:\n#{first_line}" unless first_line == expected
end

#autoload!Object



57
58
59
# File 'app/models/dunlop/source_file_model.rb', line 57

def autoload!
  #optionally override in subclass
end

#deactivate_loaded_source_fileObject



23
24
25
26
27
# File 'app/models/dunlop/source_file_model.rb', line 23

def deactivate_loaded_source_file
  self.class.find_each do |source_file|
    source_file.inactive! if source_file.active?
  end
end

#execute_loading_processObject



75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/dunlop/source_file_model.rb', line 75

def execute_loading_process
  with_nested_logger_and_catch_failed(nil, benchmark: true) do
    Dunlop::NestedLogger.info I18n.t('dunlop.source_file.start_loading')
    loading!
    deactivate_loaded_source_file
    load_source_file
    active!
    after_activate_hook
    Dunlop::NestedLogger.info I18n.t('dunlop.source_file.finished_loading')
  end
end

#execute_sql_erb_script(name, template_binding = binding) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'app/models/dunlop/source_file_model.rb', line 91

def execute_sql_erb_script(name, template_binding=binding)
  sql_erb_filename = File.join(sql_template_path, "#{name}.sql.erb")
  sql_erb_script = File.read(sql_erb_filename)
  template = ERB.new(sql_erb_script)
  sql_script = template.result(template_binding)
  sql_script.split(";\n").each do |sql_statement|
    ActiveRecord::Base.connection.execute(sql_statement) unless sql_statement.strip.empty?
  end
end

#load_source_fileObject



29
30
31
32
33
34
35
36
# File 'app/models/dunlop/source_file_model.rb', line 29

def load_source_file
  self.working_file = new_working_file
  unzip_working_file
  pre_process_working_file
  load_source_records
ensure
  working_file.try(:unlink)
end

#load_source_recordsObject



53
54
55
# File 'app/models/dunlop/source_file_model.rb', line 53

def load_source_records
  raise 'override is subclass'
end

#new_working_fileObject



38
39
40
41
42
# File 'app/models/dunlop/source_file_model.rb', line 38

def new_working_file
  local_working_file = Tempfile.new("source_file", Rails.application.config.working_path)
  FileUtils.chmod(0644, local_working_file.path)
  local_working_file
end

#pre_process_working_file(target_path = working_file.path) ⇒ Object



48
49
50
51
# File 'app/models/dunlop/source_file_model.rb', line 48

def pre_process_working_file(target_path = working_file.path)
  Dunlop::FileSed.fix_line_endings(target_path)
  Dunlop::FileSed.in_place(target_path, %Q{s/"//g})
end

#sql_template_pathObject



71
72
73
# File 'app/models/dunlop/source_file_model.rb', line 71

def sql_template_path
  Rails.root.join('app/models/source_file/sql_templates')
end

#unzip_working_fileObject



44
45
46
# File 'app/models/dunlop/source_file_model.rb', line 44

def unzip_working_file
  Dunlop::FileZip.unzip(original_file.current_path, working_file.path)
end