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_file ⇒ Object
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
|
#builder ⇒ Object
68
69
70
|
# File 'app/models/dunlop/target_file_model.rb', line 68
def builder
builder_class.new(resource)
end
|
#date_number ⇒ Object
137
138
139
|
# File 'app/models/dunlop/target_file_model.rb', line 137
def date_number
Date.today.to_s(:number)
end
|
#duration ⇒ Object
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_process ⇒ Object
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
save!
end
|
#file_size ⇒ Object
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_file ⇒ Object
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 end
|
#generate_file ⇒ Object
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_file ⇒ Object
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 self.working_file = local_working_file
end
|
#gzip_file? ⇒ Boolean
98
99
100
|
# File 'app/models/dunlop/target_file_model.rb', line 98
def gzip_file?
false end
|
#line_count ⇒ Object
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_filename ⇒ Object
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_file ⇒ Object
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_file ⇒ Object
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..present?
gzip_file if gzip_file?
self.size = file_size
self.save!
end
|
#prepare_working_file_for_sql_generation ⇒ Object
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
|
#resource ⇒ Object
72
73
|
# File 'app/models/dunlop/target_file_model.rb', line 72
def resource
end
|
#set_end_time ⇒ Object
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_time ⇒ Object
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_path ⇒ Object
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_basename ⇒ Object
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
|