Module: Dunlop::TargetFileModel

Extended by:
ActiveSupport::Concern, Memoist
Included in:
TargetFile
Defined in:
app/models/dunlop/target_file_model.rb

Defined Under Namespace

Modules: ClassMethods Classes: GenericBuilder

Instance Method Summary collapse

Instance Method Details

#attach_target_fileObject



84
85
86
87
88
89
90
# File 'app/models/dunlop/target_file_model.rb', line 84

def attach_target_file
  %x[cp #{Shellwords.escape(working_file.path)} #{Shellwords.escape(local_target_filename)}]
  self.original_file = File.open(local_target_filename,'r')
  self.save!
ensure
  FileUtils.rm_f(local_target_filename)
end

#builderObject



68
69
70
# File 'app/models/dunlop/target_file_model.rb', line 68

def builder
  builder_class.new(resource)
end

#date_numberObject



137
138
139
# File 'app/models/dunlop/target_file_model.rb', line 137

def date_number
  Date.today.to_s(:number)
end

#durationObject



34
35
36
37
38
39
40
41
42
# File 'app/models/dunlop/target_file_model.rb', line 34

def duration
  if start_time and end_time
    end_time - start_time
  elsif start_time
    Time.zone.now - start_time
  else
    0
  end
end

#execute_generation_processObject



126
127
128
129
130
131
132
133
134
135
# File 'app/models/dunlop/target_file_model.rb', line 126

def execute_generation_process
  with_nested_logger_and_catch_failed do
    scheduled! unless scheduled?
    generating!
    generate_and_post_process_file
    completed!
  end
  #FIXME: save to ensure set_end_time is presisted
  save!
end

#file_sizeObject



113
114
115
# File 'app/models/dunlop/target_file_model.rb', line 113

def file_size
  File.size(working_file.path)
end

#generate_and_post_process_fileObject



44
45
46
47
48
49
50
51
# File 'app/models/dunlop/target_file_model.rb', line 44

def generate_and_post_process_file
  self.working_file = new_working_file
  generate_file
  post_process_file
  attach_target_file
ensure
  working_file.close! rescue SystemCallError # container permission issue
end

#generate_fileObject

Implement in Subclass. Example:

CsvBuilder::BladiBla.new(resource).data


64
65
66
# File 'app/models/dunlop/target_file_model.rb', line 64

def generate_file
  working_file.puts builder.data
end

#gzip_fileObject



102
103
104
105
106
107
# File 'app/models/dunlop/target_file_model.rb', line 102

def gzip_file
  local_working_file = new_working_file
  Dunlop::FileZip.gzip(working_file.path,local_working_file.path)
  working_file.unlink rescue SystemCallError # container permission issue
  self.working_file = local_working_file
end

#gzip_file?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'app/models/dunlop/target_file_model.rb', line 98

def gzip_file?
  false #override in subclass
end

#line_countObject



109
110
111
# File 'app/models/dunlop/target_file_model.rb', line 109

def line_count
  %x[wc -l #{Shellwords.escape(working_file.path)}].to_i
end

#local_target_filenameObject



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

def local_target_filename
  result = File.join(Rails.application.config.working_path, target_basename)
  gzip_file? ? result + ".gz" : result
end

#new_working_fileObject



92
93
94
95
96
# File 'app/models/dunlop/target_file_model.rb', line 92

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

#post_process_fileObject



75
76
77
78
79
80
81
82
# File 'app/models/dunlop/target_file_model.rb', line 75

def post_process_file
  self.working_file.close
  self.number_of_records = line_count
  self.number_of_records -= 1 if builder.headers.present?
  gzip_file if gzip_file?
  self.size = file_size
  self.save!
end

#prepare_working_file_for_sql_generationObject



121
122
123
124
# File 'app/models/dunlop/target_file_model.rb', line 121

def prepare_working_file_for_sql_generation
  working_file.close
  FileUtils.rm_f(working_file.path)
end

#resourceObject



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

def resource
end

#set_end_timeObject



30
31
32
# File 'app/models/dunlop/target_file_model.rb', line 30

def set_end_time
  self.end_time = Time.zone.now
end

#set_start_timeObject



26
27
28
# File 'app/models/dunlop/target_file_model.rb', line 26

def set_start_time
  self.start_time = Time.zone.now
end

#sql_template_pathObject



117
118
119
# File 'app/models/dunlop/target_file_model.rb', line 117

def sql_template_path
  Rails.root.join("app/models/target_file/sql_templates")
end

#target_basenameObject



58
59
60
# File 'app/models/dunlop/target_file_model.rb', line 58

def target_basename
  "#{self.class.name.demodulize.underscore}-#{date_number}.csv"
end