flat_out 0.0.5 → 0.0.6
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/README.rdoc +39 -4
- data/lib/flat_out.rb +52 -6
- data/spec/flat_out_spec.rb +34 -1
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -88,7 +88,7 @@ Command:
|
|
88
88
|
If the fld is longer than specified len, it is left justified and truncated.
|
89
89
|
|
90
90
|
Example:
|
91
|
-
|
91
|
+
# '123456789012'
|
92
92
|
myline = FlatOut.new(12) # ' '
|
93
93
|
myline.put(4, 2, 'ABCDEFG') # ' ABCD '
|
94
94
|
myline.put(4, 2, 'ABCD') # ' ABCD '
|
@@ -131,13 +131,34 @@ Command:
|
|
131
131
|
myline.put(-4.2, 2, 12.3) # ' 001230 '
|
132
132
|
myline.put(-4.2, 2, 123456.789) # ' 345679 '
|
133
133
|
|
134
|
+
Entire record in an array:
|
135
|
+
To specify the entire record in a single array, you have to use option :len_fld or :fld_len,
|
136
|
+
and the array should contain the fields in strict sequence. Also see 'Working With Templates'.
|
137
|
+
example 1:
|
138
|
+
myline = FlatOut.new(21, :format => :len_fld)
|
139
|
+
val = [5, 'ABCD',
|
140
|
+
5, 10,
|
141
|
+
4.2, 10.23,
|
142
|
+
3, 'EFG']
|
143
|
+
myline.put(val) # 'ABCD 000100010.23EFG '
|
144
|
+
|
145
|
+
example 2:
|
146
|
+
myline = FlatOut.new(21, :format => :fld_len)
|
147
|
+
val = ['ABCD', 5,
|
148
|
+
10, 5,
|
149
|
+
10.23, 4.2,
|
150
|
+
'EFG', 3]
|
151
|
+
myline.put(val) # 'ABCD 000100010.23EFG '
|
152
|
+
|
153
|
+
|
134
154
|
Command:
|
135
155
|
|
136
156
|
myline.to_s
|
137
157
|
|
138
158
|
Description:
|
139
159
|
This will return the formatted fixed length record which you can write to a file.
|
140
|
-
It will blank fill or truncate the output to the correct length.
|
160
|
+
It will blank fill or truncate the output to the correct length. Reset and put also
|
161
|
+
return myline.to_s.
|
141
162
|
|
142
163
|
Command:
|
143
164
|
|
@@ -167,13 +188,20 @@ Command:
|
|
167
188
|
== Working with Templates
|
168
189
|
|
169
190
|
You can also specify a template during initialization, and then put values in the template
|
170
|
-
with an array.
|
191
|
+
with an array. Templates can have length and positions as follows:
|
171
192
|
|
172
193
|
example:
|
173
194
|
f = FlatOut.new(18, :template => [[5,1],[5,6],[4.2,11]]) # '123456789012345678'
|
174
195
|
val = ['ABCD',10,10.23]
|
175
196
|
f.put(val) # 'ABCD 000100010.23 '
|
176
197
|
|
198
|
+
You can also skip the position specification if the fields are specified in strict sequence
|
199
|
+
in the template.
|
200
|
+
example:
|
201
|
+
f = FlatOut.new(18, :template => [5,5,4.2]) # '123456789012345678'
|
202
|
+
val = ['ABCD',10,10.23]
|
203
|
+
f.put(val) # 'ABCD 000100010.23 '
|
204
|
+
|
177
205
|
== Full Cycle Example:
|
178
206
|
|
179
207
|
myline = FlatOut.new(80)
|
@@ -196,8 +224,15 @@ Command:
|
|
196
224
|
myline.put(30, 30, "END OF REPORT")
|
197
225
|
puts myline.to_s
|
198
226
|
|
227
|
+
== Issues, Suggestions
|
228
|
+
|
229
|
+
* https://github.com/aksharma/flat_out/issues/
|
230
|
+
* or email me directly at (sharma.rubyonrails)
|
231
|
+
* this address ( @ )
|
232
|
+
* (sharmail_dot_com )
|
233
|
+
|
199
234
|
== Contributing to flat_out
|
200
|
-
|
235
|
+
|
201
236
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
202
237
|
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
203
238
|
* Fork the project.
|
data/lib/flat_out.rb
CHANGED
@@ -1,14 +1,18 @@
|
|
1
1
|
class FlatOut
|
2
|
+
|
3
|
+
#attr_accessor :flat_out, :flat_length, :base, :format, :template
|
4
|
+
|
2
5
|
def initialize(rec_length, options={})
|
3
6
|
self.reset(rec_length, options)
|
4
7
|
end
|
5
8
|
|
6
9
|
def reset(rec_length=@flat_length, options={})
|
7
|
-
@flat_out = " " * rec_length
|
8
|
-
@flat_length = rec_length
|
9
10
|
@base = options[:base] ||= @base ||= 1
|
10
11
|
@format = options[:format] ||= @format ||= :len_pos_fld
|
11
12
|
@template = options[:template] ||= @template ||= []
|
13
|
+
@flat_length = rec_length
|
14
|
+
@flat_out = " " * rec_length
|
15
|
+
return @flat_out
|
12
16
|
end
|
13
17
|
|
14
18
|
def to_s
|
@@ -34,16 +38,45 @@ class FlatOut
|
|
34
38
|
when :pos_fld_len then (pos = p1; fld = p2; len = p3)
|
35
39
|
when :fld_pos_len then (fld = p1; pos = p2; len = p3)
|
36
40
|
when :fld_len_pos then (fld = p1; len = p2; pos = p3)
|
41
|
+
when :fld_len then (fld = p1)
|
42
|
+
when :len_fld then (fld = p1)
|
37
43
|
else (len = p1; pos = p2; fld = p3)
|
38
44
|
end
|
39
45
|
end
|
40
46
|
|
41
47
|
case fld
|
42
48
|
when Array
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
49
|
+
if @template.size > 0 and @template[0].class == Array # :template => [[5,1],[5,6],[4.2,11]]
|
50
|
+
fld.each_with_index do |field,idx|
|
51
|
+
len = @template[idx][0]
|
52
|
+
pos = @template[idx][1]
|
53
|
+
put len, pos, field
|
54
|
+
end
|
55
|
+
elsif @template.size > 0 # :template => [5,5,4.2,3]
|
56
|
+
pos = @base
|
57
|
+
fld.each_with_index do |field, idx|
|
58
|
+
len = @template[idx]
|
59
|
+
put len, pos, field
|
60
|
+
pos = pos + real_len(len)
|
61
|
+
end
|
62
|
+
elsif @format == :fld_len # val = ['ABCD', 5, 10, 5]
|
63
|
+
@format = :len_pos_fld
|
64
|
+
pos = @base
|
65
|
+
fld.each_slice(2) do |arr|
|
66
|
+
field = arr[0]
|
67
|
+
len = arr[1]
|
68
|
+
put len, pos, field
|
69
|
+
pos = pos + real_len(len)
|
70
|
+
end
|
71
|
+
elsif @format == :len_fld # val = [5, 'ABCD', 4.2, 10.23]
|
72
|
+
@format = :len_pos_fld
|
73
|
+
pos = @base
|
74
|
+
fld.each_slice(2) do |arr|
|
75
|
+
len = arr[0]
|
76
|
+
field = arr[1]
|
77
|
+
put len, pos, field
|
78
|
+
pos = pos + real_len(len)
|
79
|
+
end
|
47
80
|
end
|
48
81
|
when String
|
49
82
|
put_alpha len, pos, fld
|
@@ -56,6 +89,7 @@ class FlatOut
|
|
56
89
|
put_integer len, pos, fld
|
57
90
|
end
|
58
91
|
end
|
92
|
+
return self.to_s
|
59
93
|
end
|
60
94
|
|
61
95
|
private
|
@@ -93,4 +127,16 @@ class FlatOut
|
|
93
127
|
val = sprintf("%0.#{dec}f", fld)[-dec,dec]
|
94
128
|
put_fld dec, pos, val
|
95
129
|
end
|
130
|
+
|
131
|
+
def real_len(len)
|
132
|
+
return len if len.integer?
|
133
|
+
|
134
|
+
int = len.to_s.split(".")[0].to_i.abs
|
135
|
+
dec = len.to_s.split(".")[1].to_i
|
136
|
+
if len > 0
|
137
|
+
return int + 1 + dec
|
138
|
+
else
|
139
|
+
return int + dec
|
140
|
+
end
|
141
|
+
end
|
96
142
|
end
|
data/spec/flat_out_spec.rb
CHANGED
@@ -48,6 +48,30 @@ describe "put exact length alpha with fld_pos_len format" do
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
+
describe "put whole record specified in an array with fld_len format" do
|
52
|
+
it "should build the format sequentially" do
|
53
|
+
f = FlatOut.new(21, :format => :fld_len)
|
54
|
+
val = ['ABCD', 5,
|
55
|
+
10, 5,
|
56
|
+
10.23, 4.2,
|
57
|
+
'EFG', 3]
|
58
|
+
f.put(val)
|
59
|
+
f.to_s.should == 'ABCD 000100010.23EFG '
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "put whole record specified in an array with len_fld format" do
|
64
|
+
it "should build the format sequentially" do
|
65
|
+
f = FlatOut.new(21, :format => :len_fld)
|
66
|
+
val = [5, 'ABCD',
|
67
|
+
5, 10,
|
68
|
+
4.2, 10.23,
|
69
|
+
3, 'EFG']
|
70
|
+
f.put(val)
|
71
|
+
f.to_s.should == 'ABCD 000100010.23EFG '
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
51
75
|
describe "put short length alpha" do
|
52
76
|
it "should blank fill the trailing space" do
|
53
77
|
f = FlatOut.new(6)
|
@@ -144,7 +168,7 @@ describe "put float with negative length" do
|
|
144
168
|
end
|
145
169
|
end
|
146
170
|
|
147
|
-
describe "put with a template" do
|
171
|
+
describe "put with a template specifying len and pos" do
|
148
172
|
it "should put multiple fields" do
|
149
173
|
f = FlatOut.new(18, :template => [[5,1],[5,6],[4.2,11]])
|
150
174
|
val = ['ABCD',10,10.23]
|
@@ -153,6 +177,15 @@ describe "put with a template" do
|
|
153
177
|
end
|
154
178
|
end
|
155
179
|
|
180
|
+
describe "put with a template specifying len only" do
|
181
|
+
it "should build the format sequentially" do
|
182
|
+
f = FlatOut.new(21, :template => [5,5,4.2,3])
|
183
|
+
val = ['ABCD',10,10.23,'EFG']
|
184
|
+
f.put(val)
|
185
|
+
f.to_s.should == 'ABCD 000100010.23EFG '
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
156
189
|
describe "digits_only" do
|
157
190
|
it "should remove non-digit characters" do
|
158
191
|
FlatOut.digits_only('01(2)-3ABC4-5^678').should == "012345678"
|