file_generator 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/file_generator/base.rb +69 -67
- data/lib/file_generator/version.rb +1 -1
- data/spec/lib/file_generator_spec.rb +1 -2
- metadata +1 -1
data/lib/file_generator/base.rb
CHANGED
@@ -1,91 +1,93 @@
|
|
1
1
|
module FileGenerator
|
2
2
|
class Base
|
3
|
+
class << self
|
3
4
|
|
4
|
-
|
5
|
-
end
|
6
|
-
|
7
|
-
def generate_file(records, headerformat, bodyformat, footerformat)
|
8
|
-
file = []
|
9
|
-
fh = process_hf(headerformat, records)
|
10
|
-
file << fh
|
11
|
-
records.each do |record|
|
12
|
-
fb = process_body(bodyformat, record)
|
13
|
-
file << fb
|
5
|
+
def initialize()
|
14
6
|
end
|
15
|
-
ff = process_hf(footerformat, records)
|
16
|
-
file << ff
|
17
|
-
file = file.join("\n")
|
18
|
-
file
|
19
|
-
end
|
20
7
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
value = ''
|
29
|
-
case behaviors[0]
|
30
|
-
when 'nreg' then
|
31
|
-
value = records.size.to_s
|
32
|
-
when 'time' then
|
33
|
-
value = Time.now.strftime("%Y%m%d")
|
34
|
-
else
|
35
|
-
value = behaviors[2]
|
36
|
-
end
|
37
|
-
if (behaviors[3] != '')
|
38
|
-
if (behaviors[4] == 'I')
|
39
|
-
row << value.ljust(behaviors[1].to_i, behaviors[3])
|
40
|
-
else
|
41
|
-
row << value.rjust(behaviors[1].to_i, behaviors[3])
|
42
|
-
end
|
43
|
-
else
|
44
|
-
row << value
|
8
|
+
def generate_file(records, headerformat, bodyformat, footerformat)
|
9
|
+
file = []
|
10
|
+
fh = process_hf(headerformat, records)
|
11
|
+
file << fh
|
12
|
+
records.each do |record|
|
13
|
+
fb = process_body(bodyformat, record)
|
14
|
+
file << fb
|
45
15
|
end
|
16
|
+
ff = process_hf(footerformat, records)
|
17
|
+
file << ff
|
18
|
+
file = file.join("\n")
|
19
|
+
file
|
46
20
|
end
|
47
|
-
row
|
48
|
-
end
|
49
21
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
value =
|
58
|
-
else
|
22
|
+
private
|
23
|
+
|
24
|
+
def process_hf(format, records)
|
25
|
+
row = ''
|
26
|
+
attributes = split_format(format)
|
27
|
+
attributes.each do |attrib|
|
28
|
+
behaviors = split_behaviors(attrib) # 0=>nombre, 1=>longitud, 2=> valor, 3=> relleno, 4 => alineado
|
29
|
+
value = ''
|
59
30
|
case behaviors[0]
|
31
|
+
when 'nreg' then
|
32
|
+
value = records.size.to_s
|
60
33
|
when 'time' then
|
61
34
|
value = Time.now.strftime("%Y%m%d")
|
62
35
|
else
|
63
36
|
value = behaviors[2]
|
64
37
|
end
|
38
|
+
if (behaviors[3] != '')
|
39
|
+
if (behaviors[4] == 'I')
|
40
|
+
row << value.ljust(behaviors[1].to_i, behaviors[3])
|
41
|
+
else
|
42
|
+
row << value.rjust(behaviors[1].to_i, behaviors[3])
|
43
|
+
end
|
44
|
+
else
|
45
|
+
row << value
|
46
|
+
end
|
65
47
|
end
|
66
|
-
|
67
|
-
|
68
|
-
|
48
|
+
row
|
49
|
+
end
|
50
|
+
|
51
|
+
def process_body(format, record)
|
52
|
+
row = ''
|
53
|
+
attributes = split_format(format)
|
54
|
+
attributes.each do |attrib|
|
55
|
+
behaviors = split_behaviors(attrib) # 0=>nombre, 1=>longitud, 2=> valor, 3=> relleno, 4 => alineado
|
56
|
+
value = ''
|
57
|
+
if record.has_key?(behaviors[0])
|
58
|
+
value = record["#{behaviors[0]}"].to_s
|
59
|
+
else
|
60
|
+
case behaviors[0]
|
61
|
+
when 'time' then
|
62
|
+
value = Time.now.strftime("%Y%m%d")
|
63
|
+
else
|
64
|
+
value = behaviors[2]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
if (behaviors[3] != '')
|
68
|
+
if (behaviors[4] == 'I')
|
69
|
+
row << value.ljust(behaviors[1].to_i, behaviors[3])
|
70
|
+
else
|
71
|
+
row << value.rjust(behaviors[1].to_i, behaviors[3])
|
72
|
+
end
|
69
73
|
else
|
70
|
-
row << value
|
74
|
+
row << value
|
71
75
|
end
|
72
|
-
else
|
73
|
-
row << value
|
74
76
|
end
|
77
|
+
row
|
75
78
|
end
|
76
|
-
row
|
77
|
-
end
|
78
79
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
80
|
+
# split the format in each behaviors
|
81
|
+
def split_format(format)
|
82
|
+
format.split(',')
|
83
|
+
end
|
83
84
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
85
|
+
# split the line pased
|
86
|
+
# 0=>nombre, 1=>longitud, 2=> valor, 3=> relleno, 4 => alineado
|
87
|
+
def split_behaviors(behavior)
|
88
|
+
behavior.split(':')
|
89
|
+
end
|
89
90
|
|
91
|
+
end
|
90
92
|
end
|
91
93
|
end
|
@@ -6,8 +6,7 @@ describe FileGenerator do
|
|
6
6
|
let(:file_generator) do
|
7
7
|
records = [{"id"=>1, "region_id"=>7, "name"=>"les Escaldes"},{"id"=>2, "region_id"=>7, "name"=>"Lima"}]
|
8
8
|
format = load_formats.formats
|
9
|
-
|
10
|
-
f.generate_file(records,format['headerformat'],format['bodyformat'],format['footerformat'])
|
9
|
+
FileGenerator::Base.generate_file(records,format['headerformat'],format['bodyformat'],format['footerformat'])
|
11
10
|
#raise f.generate_file(records,format['headerformat'],format['bodyformat'],format['footerformat']).inspect
|
12
11
|
end
|
13
12
|
|