flat_out 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.rdoc +100 -112
  2. data/lib/flat_out.rb +53 -31
  3. data/spec/flat_out_spec.rb +59 -47
  4. metadata +2 -2
data/README.rdoc CHANGED
@@ -18,28 +18,42 @@ Install flat_out like any other ruby gem:
18
18
  * Format this instance using various put methods based upon your input type.
19
19
  * The final result is available via the to_s method of the object.
20
20
 
21
+ == Important note on base
22
+ The positions specified in flat_out are 1 based, which is what most
23
+ flat file specifications I am dealing with use, and not Ruby's zero based. For example,
24
+ the first, second, and third characters in a ruby string are considered
25
+ positions 0, 1, and 2 respectively. In flat_out, these would be considered
26
+ positions 1, 2, and 3 respectively.
27
+
28
+ You can change the base through the FlatOut.base command.
29
+
21
30
  == Commands
22
31
 
23
32
  Available Commands:
24
33
 
25
- initialize(rec_length)
34
+ FlatOut.base(base)
35
+ FlatOut.new(rec_length)
36
+ put(len, pos, fld)
37
+ to_s
38
+
26
39
  reset
27
40
  reset(rec_length)
28
- put_alpha(len, pos, fld)
29
- put_integer(len, pos, fld)
30
- put_integer_part(len, pos, fld)
31
- put_digits_only(len, pos, fld)
32
- put_decimal_with_point(int_dot_dec, pos, fld)
33
- put_decimal_no_point(int_dot_dec, pos, fld)
34
- to_s
35
41
 
42
+ FlatOut.digits_only(fld)
43
+ FlatOut.phone_squeeze(fld)
44
+
45
+ Command:
46
+
47
+ FlatOut.base(base)
48
+
49
+ Description:
50
+ This command will change the base for position specifications. It is also useful while
51
+ working with layouts that are specified with an offset.
36
52
 
37
- == IMPORTANT NOTE
53
+ example:
54
+ FlatOut.base(0) # Set the base to zero.
55
+ FlatOut.base(1) # Set the base to 1(default).
38
56
 
39
- The positions specified in flat_out are 1 based, which is what most flat file specifications
40
- use, and not Ruby's zero based. For example, the first, second, and third characters in a ruby string
41
- are considered positions 0, 1, and 2 respectively. In flat_out, these would be considered
42
- positions 1, 2, and 3 respectively.
43
57
 
44
58
  Command:
45
59
 
@@ -66,137 +80,111 @@ Command:
66
80
 
67
81
  Command:
68
82
 
69
- myline.put_alpha(len, pos, fld)
70
-
71
- Description:
72
- This command puts the provided string(fld) at starting position(pos) for (len) bytes.
73
- If the fld is shorter than specified len, it is padded with trailing spaces.
74
- If the fld is longer than specified len, it is left justified and truncated.
75
-
76
- example:
77
- myline = FlatOut.new(10) # ' '
78
- # '1234567890'
79
- myline.put_alpha(4, 2, 'ABCD') # ' ABCD '
80
- myline.put_alpha(4, 2, 'AB') # ' AB '
81
- myline.put_alpha(4, 2, 'ABCDEFG') # ' ABCD '
82
-
83
- Command:
84
-
85
- myline.put_integer(len, pos, fld)
86
-
87
- Description:
88
- This command puts the provided integer(fld) at starting position(pos) for (len) bytes.
89
- If the fld is shorter than specified len, it is padded with leading zeroes.
90
- If the fld is longer than specified len, it is right justified and truncated.
91
-
92
- example:
93
- myline = FlatOut.new(10) # ' '
94
- # '1234567890'
95
- myline.put_integer(4, 2, 1234) # ' 1234 '
96
- myline.put_integer(4, 2, 12) # ' 0012 '
97
- myline.put_integer(4, 2, 123456) # ' 3456 '
98
-
99
- Command:
100
-
101
- myline.put_integer_part(len, pos, fld)
102
-
103
- Description:
104
- This command puts the provided integer part only of the provided float(fld)
105
- at starting position(pos) for (len) bytes. The decimal part is dropped.
106
- It is useful when only the dollar part of an amount is to be reported.
107
- If the fld is shorter than specified len, it is padded with leading zeroes.
108
- If the fld is longer than specified len, it is right justified and truncated.
109
-
110
- example:
111
- myline = FlatOut.new(10) # ' '
112
- # '1234567890'
113
- myline.put_integer_part(4, 2, 1234.45) # ' 1234 '
114
- myline.put_integer_part(4, 2, 12.45) # ' 0012 '
115
- myline.put_integer_part(4, 2, 123456.45) # ' 3456 '
83
+ myline.put(len, pos, fld)
84
+
85
+ Descriptions and examples:
86
+ This command puts the provided field(fld) at starting position(pos) for (len) bytes.
87
+
88
+ String fld:
89
+ If the fld is shorter than specified len, it is padded with trailing spaces.
90
+ If the fld is longer than specified len, it is left justified and truncated.
91
+
92
+ Example:
93
+ # '123456789012'
94
+ myline = FlatOut.new(12) # ' '
95
+ myline.put(4, 2, 'ABCDEFG') # ' ABCD '
96
+ myline.put(4, 2, 'ABCD') # ' ABCD '
97
+ myline.put(4, 2, 'AB') # ' AB '
98
+
99
+ Integer fld:
100
+ If the fld is shorter than specified len, it is padded with leading zeroes.
101
+ If the fld is longer than specified len, it is right justified and truncated.
102
+ myline = FlatOut.new(12) # ' '
103
+ myline.put(4, 2, 1234) # ' 1234 '
104
+ myline.put(4, 2, 12) # ' 0012 '
105
+ myline.put(4, 2, 123456) # ' 3456 '
106
+
107
+ Float fld with integer len:
108
+ The decimal part of the fld is truncated and only the integer part is processed.
109
+ It is useful when only the dollar part of an amount is to be reported.
110
+ myline = FlatOut.new(12) # ' '
111
+ myline.put(4, 2, 1234.45) # ' 1234 '
112
+ myline.put(4, 2, 12.45) # ' 0012 '
113
+ myline.put(4, 2, 123456.78) # ' 3456 '
114
+
115
+ Float fld with x.y length with explicit decimal point:
116
+ Here x is the number of integer digits and y is the number of places after the decimal.
117
+ The total length of this field in the output is going to be x + y + 1.
118
+ If the fld is shorter than specified length, it is padded with leading and trailing zeroes.
119
+ If the fld is longer than specified length, it is decimal justified and truncated.
120
+ myline = FlatOut.new(12) # ' '
121
+ myline.put(4.2, 2, 1234.56) # ' 1234.56 '
122
+ myline.put(4.2, 2, 12.3) # ' 0012.30 '
123
+ myline.put(4.2, 2, 123456.789) # ' 3456.79 '
124
+
125
+ Float fld with -x.y length with implicit decimal:
126
+ The format of the output has be specified in -x.y format where x is the number of integer
127
+ digits and y is the number of places after the decimal. The total length of this field
128
+ in the output is going to be x + y.
129
+ This is the format used for decimal numbers by most legacy systems using fixed length records.
130
+ example:
131
+ myline = FlatOut.new(12) # ' '
132
+ myline.put(-4.2, 2, 1234.56) # ' 123456 '
133
+ myline.put(-4.2, 2, 12.3) # ' 001230 '
134
+ myline.put(-4.2, 2, 123456.789) # ' 345679 '
116
135
 
117
136
  Command:
118
137
 
119
- myline.put_digits_only(len, pos, fld)
138
+ myline.to_s
120
139
 
121
140
  Description:
122
- This command puts the provided numeric digits only from alphanumeric(fld) at
123
- starting position(pos) for (len) bytes. It is useful to convert formatted
124
- SSNs, EINs, Phone numbers etc to their numeric only equivalents.
125
- If the fld is shorter than specified len, it is padded with trailing spaces.
126
- If the fld is longer than specified len, it is left justified and truncated.
127
-
128
- example:
129
- myline = FlatOut.new(12) # ' '
130
- # '123456789012'
131
- myline.put_digits_only(10, 2, '012-345-6789') # ' 0123456789 '
132
- myline.put_digits_only(10, 2, '(800) 555-1212') # ' 8005551212 '
141
+ This will return the formatted fixed length record which you can write to a file.
133
142
 
134
143
  Command:
135
144
 
136
- myline.put_decimal_with_point(int_dot_dec, pos, fld)
145
+ FlatOut.digits_only(fld)
137
146
 
138
147
  Description:
139
- This command puts the provided decimal number(fld) at starting position(pos).
140
- The format of the output has be specified in x.y format where x is the number of integer
141
- digits and y is the number of places after the decimal. The total length of this field
142
- in the output is going to be x + y + 1.
143
- If the fld is shorter than specified length, it is padded with leading and trailing zeroes.
144
- If the fld is longer than specified length, it is decimal justified and truncated.
148
+ This helper method strips all non-digits from a given string. It is useful to
149
+ convert formatted SSNs, EINs, Phone numbers etc to their numeric only equivalents.
145
150
 
146
151
  example:
147
- myline = FlatOut.new(12) # ' '
148
- # '123456789012'
149
- myline.put_decimal_with_point(4.2, 2, 1234.56) # ' 1234.56 '
150
- myline.put_decimal_with_point(4.2, 2, 12.3) # ' 0012.30 '
151
- myline.put_decimal_with_point(4.2, 2, 123456.789) # ' 3456.79 '
152
+ FlatOut.digits_only('012-345-6789') # '0123456789'
153
+ FlatOut.digits_only('(800) 555-1212') # '8005551212'
152
154
 
153
155
  Command:
154
156
 
155
- myline.put_decimal_no_point(int_dot_dec, pos, fld)
157
+ FlatOut.phone_squeeze(fld)
156
158
 
157
159
  Description:
158
- This command puts the provided decimal number(fld) without the decimal at starting position(pos).
159
- The format of the output has be specified in x.y format where x is the number of integer
160
- digits and y is the number of places after the decimal. The total length of this field
161
- in the output is going to be x + y.
162
- This is the format used for decimal numbers by most legacy systems using fixed length records.
163
- If the fld is shorter than specified length, it is padded with leading and trailing zeroes.
164
- If the fld is longer than specified length, it is decimal justified and truncated.
160
+ This helper method strips the commonly used special characters and spaces from phone
161
+ numbers, but leaves the alphabets alone. It is useful to standardize phone numbers fields.
165
162
 
166
163
  example:
167
- myline = FlatOut.new(12) # ' '
168
- # '123456789012'
169
- myline.put_decimal_no_point(4.2, 2, 1234.56) # ' 123456 '
170
- myline.put_decimal_no_point(4.2, 2, 12.3) # ' 001230 '
171
- myline.put_decimal_no_point(4.2, 2, 123456.789) # ' 345679 '
172
-
173
- Command:
174
-
175
- myline.to_s
176
-
177
- Description:
178
- This will return the formatted fixed length record which you can write to a file.
164
+ FlatOut.phone_squeeze('(800) 555-1212') == "8005551212"
165
+ FlatOut.phone_squeeze('(800) 555-1212 Ext 234') == "8005551212Ext234"
166
+ FlatOut.phone_squeeze('(800) GOT-MILK') == "800GOTMILK"
179
167
 
180
- full cycle example:
168
+ == Full Cycle Example:
181
169
 
182
170
  myline = FlatOut.new(80)
183
- myline.put_alpha(10, 1, Time.now.strftime("%m/%d/%Y"))
184
- myline.put_alpha(30, 30, "TEST REPORT PRINTED")
185
- myline.put_alpha(6, 70, "PAGE:")
186
- myline.put_integer(2, 79, 1)
171
+ myline.put(10, 1, Time.now.strftime("%m/%d/%Y"))
172
+ myline.put(30, 30, "TEST REPORT PRINTED")
173
+ myline.put(6, 70, "PAGE:")
174
+ myline.put(2, 79, 1)
187
175
  puts myline.to_s
188
176
 
189
177
  users = %w[Anil Mark Jake]
190
178
 
191
179
  users.each do |user|
192
180
  myline.reset
193
- myline.put_alpha(12, 4, "USERNAME :")
194
- myline.put_alpha(12, 16, user)
181
+ myline.put(12, 4, "USERNAME :")
182
+ myline.put(12, 16, user)
195
183
  puts myline.to_s
196
184
  end
197
185
 
198
186
  myline.reset
199
- myline.put_alpha(30, 30, "END OF REPORT")
187
+ myline.put(30, 30, "END OF REPORT")
200
188
  puts myline.to_s
201
189
 
202
190
  == Contributing to flat_out
data/lib/flat_out.rb CHANGED
@@ -2,6 +2,7 @@ class FlatOut
2
2
  def initialize(rec_length)
3
3
  @flat_out = " " * rec_length
4
4
  @flat_length = rec_length
5
+ @@base ||= 1
5
6
  end
6
7
 
7
8
  def reset(rec_length=@flat_length)
@@ -9,49 +10,70 @@ class FlatOut
9
10
  @flat_length = rec_length
10
11
  end
11
12
 
13
+ def to_s
14
+ @flat_out
15
+ end
16
+
17
+ def self.base(base)
18
+ @@base = base
19
+ end
20
+
21
+ def self.phone_squeeze(fld)
22
+ fld.tr("-)( " , "")
23
+ end
24
+
25
+ def self.digits_only(fld)
26
+ fld.gsub(/[^\d]/,"")
27
+ end
28
+
29
+ def put len, pos, fld
30
+ case fld
31
+ when String
32
+ put_alpha len, pos, fld
33
+ when Integer
34
+ put_integer len, pos, fld
35
+ when Float
36
+ if len.to_s.match(/\./)
37
+ put_float len, pos, fld
38
+ else
39
+ put_integer len, pos, fld
40
+ end
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def put_fld len, pos, fld
47
+ start_pos = pos - @@base ; end_pos = start_pos + len
48
+ @flat_out[start_pos...end_pos] = fld
49
+ end
50
+
12
51
  def put_alpha len, pos, fld
13
- start_pos = pos - 1 ; end_pos = start_pos + len
14
52
  space_fill = " " * len
15
53
  val = (fld.to_s + space_fill)[0...len]
16
- @flat_out[start_pos...end_pos] = val
54
+ put_fld len, pos, val
17
55
  end
18
56
 
19
57
  def put_integer len, pos, fld
20
- data_content = fld.to_s
58
+ data_content = fld.to_i.to_s
21
59
  data_len = data_content.length
22
60
  zero_fill = "0" * len
23
61
  val = (zero_fill + data_content)[-len,len]
24
- put_alpha len, pos, val
62
+ put_fld len, pos, val
25
63
  end
26
64
 
27
- def put_integer_part len, pos, fld
28
- val = fld.to_i
29
- put_integer len, pos, val
30
- end
65
+ def put_float int_dot_dec, pos, fld
66
+ int = int_dot_dec.to_s.split(".")[0].to_i.abs
67
+ put_integer int, pos, fld
31
68
 
32
- def put_digits_only len, pos, fld
33
- val = fld.gsub(/[^\d]/,"")
34
- put_alpha len, pos, val
35
- end
69
+ pos = pos + int
70
+ if int_dot_dec > 0
71
+ put_fld 1, pos, '.'
72
+ pos = pos + 1
73
+ end
36
74
 
37
- def put_decimal_with_point int_dot_dec, pos, fld
38
- precision = int_dot_dec.to_s.split(".")[1].to_i
39
- len = int_dot_dec.to_s.split(".")[0].to_i + precision + 1
40
- val = sprintf("%#{len}.#{precision}f", fld)[-len,len]
41
- val = val.tr(" ","0")
42
- put_alpha len, pos, val
43
- end
44
-
45
- def put_decimal_no_point int_dot_dec, pos, fld
46
- precision = int_dot_dec.to_s.split(".")[1].to_i
47
- len = int_dot_dec.to_s.split(".")[0].to_i + precision + 1
48
- val = sprintf("%#{len}.#{precision}f", fld)[-len,len]
49
- val = val.tr(" ","0")
50
- val = val.sub(".","")
51
- put_alpha len, pos, val
52
- end
53
-
54
- def to_s
55
- @flat_out
75
+ dec = int_dot_dec.to_s.split(".")[1].to_i
76
+ val = sprintf("%0.#{dec}f", fld)[-dec,dec]
77
+ put_fld dec, pos, val
56
78
  end
57
79
  end
@@ -1,11 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- #describe "FlatOut" do
4
- # it "fails" do
5
- ## fail "hey buddy, you should probably rename this file and start specing for real"
6
- # end
7
- #end
8
-
9
3
  describe "New String" do
10
4
  it "should have a blank string of correct length when initialize()" do
11
5
  f = FlatOut.new(6)
@@ -29,115 +23,133 @@ describe "Reset String with argument" do
29
23
  end
30
24
  end
31
25
 
32
- describe "put_alpha exact length" do
26
+ describe "put exact length alpha" do
33
27
  it "should replace the correct space in the right spot" do
34
28
  f = FlatOut.new(6)
35
- f.put_alpha(3,2,"ABC")
29
+ f.put(3,2,"ABC")
36
30
  f.to_s.should == " ABC "
37
31
  end
38
32
  end
39
33
 
40
- describe "put_alpha short length" do
34
+ describe "put short length alpha" do
41
35
  it "should blank fill the trailing space" do
42
36
  f = FlatOut.new(6)
43
- f.put_alpha(3,2,"AB")
37
+ f.put(3,2,"AB")
44
38
  f.to_s.should == " AB "
45
39
  end
46
40
  end
47
41
 
48
- describe "put_alpha long length" do
42
+ describe "put long length alpha" do
49
43
  it "should truncate the extra characters on the right" do
50
44
  f = FlatOut.new(6)
51
- f.put_alpha(3,2,"ABCDE")
45
+ f.put(3,2,"ABCDE")
52
46
  f.to_s.should == " ABC "
53
47
  end
54
48
  end
55
49
 
56
- describe "put_integer exact length" do
57
- it "should replace the correct space in the right spot" do
50
+ describe "put exact length integer with default base 1" do
51
+ it "should replace the correct space in the right spot using base 1" do
58
52
  f = FlatOut.new(6)
59
- f.put_integer(3,2,123)
53
+ f.put(3,2,123)
60
54
  f.to_s.should == " 123 "
61
55
  end
62
56
  end
63
57
 
64
- describe "put_integer short length" do
65
- it "should zero fill the leading spaces" do
58
+ describe "put exact length integer with base 0" do
59
+ it "should replace the correct space in the right spot using base 0" do
60
+ FlatOut.base(0)
66
61
  f = FlatOut.new(6)
67
- f.put_integer(3,2,12)
68
- f.to_s.should == " 012 "
62
+ f.put(3,2,123)
63
+ f.to_s.should == " 123 "
64
+ FlatOut.base(1)
69
65
  end
70
66
  end
71
67
 
72
- describe "put_integer long length" do
73
- it "should truncate the extra characters on the right" do
68
+ describe "put short length integer" do
69
+ it "should zero fill the leading spaces" do
74
70
  f = FlatOut.new(6)
75
- f.put_integer(3,2,12345)
76
- f.to_s.should == " 345 "
77
- end
78
- end
79
-
80
- describe "put_digits_only" do
81
- it "should remove non-digit characters" do
82
- f = FlatOut.new(11)
83
- f.put_digits_only(9,2,'01(2)-3ABC4-5^678')
84
- f.to_s.should == " 012345678 "
71
+ f.put(3,2,12)
72
+ f.to_s.should == " 012 "
85
73
  end
86
74
  end
87
75
 
88
- describe "put_integer_part of an integer" do
89
- it "should act like put_integer" do
76
+ describe "put long length integer" do
77
+ it "should truncate the extra most dignificant digits on left" do
90
78
  f = FlatOut.new(6)
91
- f.put_integer_part(3,2,12345)
79
+ f.put(3,2,12345)
92
80
  f.to_s.should == " 345 "
93
81
  end
94
82
  end
95
83
 
96
- describe "put_integer_part of a float" do
97
- it "should remove the decimal part" do
84
+ describe "put float with short length integer only" do
85
+ it "should remove the decimal part and truncate the left digits" do
98
86
  f = FlatOut.new(6)
99
- f.put_integer_part(3,2,12345.67)
87
+ f.put(3,2,12345.67)
100
88
  f.to_s.should == " 345 "
101
89
  end
102
90
  end
103
91
 
104
- describe "put_decimal_with_point for a float" do
92
+ describe "put float with decimal part and full length" do
105
93
  it "should include the decimal in result for exact size" do
106
94
  f = FlatOut.new(12)
107
- f.put_decimal_with_point(5.3, 2, 12345.678)
95
+ f.put(5.3, 2, 12345.678)
108
96
  f.to_s.should == " 12345.678 "
109
97
  end
110
98
  end
111
99
 
112
- describe "put_decimal_with_point for a float" do
100
+ describe "put float with decimal part with shorter length on both ends" do
113
101
  it "should truncate the result for smaller size" do
114
102
  f = FlatOut.new(12)
115
- f.put_decimal_with_point(4.2, 2, 12345.643)
103
+ f.put(4.2, 2, 12345.643)
116
104
  f.to_s.should == " 2345.64 "
117
105
  end
118
106
  end
119
107
 
120
- describe "put_decimal_with_point for a float" do
108
+ describe "put float with decimal part with shorter length on both ends with rounding" do
121
109
  it "should truncate the result for smaller size(rounded)" do
122
110
  f = FlatOut.new(12)
123
- f.put_decimal_with_point(4.2, 2, 12345.678)
111
+ f.put(4.2, 2, 12345.678)
124
112
  f.to_s.should == " 2345.68 "
125
113
  end
126
114
  end
127
115
 
128
- describe "put_decimal_with_point for a float" do
116
+ describe "put float with longer length on both ends" do
129
117
  it "should zero fill both sides" do
130
118
  f = FlatOut.new(12)
131
- f.put_decimal_with_point(4.2, 2, 1.2)
119
+ f.put(4.2, 2, 1.2)
132
120
  f.to_s.should == " 0001.20 "
133
121
  end
134
122
  end
135
123
 
136
- describe "put_decimal_no_point for a float" do
124
+ describe "put float with negative length" do
137
125
  it "should not have a decimal" do
138
126
  f = FlatOut.new(12)
139
- f.put_decimal_no_point(4.2, 2, 1.2)
127
+ f.put(-4.2, 2, 1.2)
140
128
  f.to_s.should == " 000120 "
141
129
  end
142
130
  end
143
131
 
132
+ describe "digits_only" do
133
+ it "should remove non-digit characters" do
134
+ FlatOut.digits_only('01(2)-3ABC4-5^678').should == "012345678"
135
+ end
136
+ end
137
+
138
+ describe "phone_squeeze" do
139
+ it "should remove phone punctuation characters" do
140
+ FlatOut.phone_squeeze('(800) 555-1212').should == "8005551212"
141
+ end
142
+ end
143
+
144
+ describe "phone_squeeze" do
145
+ it "should remove phone punctuation characters and maintain extensions" do
146
+ FlatOut.phone_squeeze('(800) 555-1212 Ext 234').should == "8005551212Ext234"
147
+ end
148
+ end
149
+
150
+ describe "phone_squeeze" do
151
+ it "should remove phone punctuation characters and maintain extensions" do
152
+ FlatOut.phone_squeeze('(800) GOT-MILK').should == "800GOTMILK"
153
+ end
154
+ end
155
+
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: flat_out
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Anil Sharma
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-05-27 00:00:00 -04:00
13
+ date: 2012-05-28 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies: []
16
16