flat_out 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Anil Sharma
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,215 @@
1
+ = flat_out
2
+
3
+ Create fixed length records with fixed length data fields to write a flat file.
4
+
5
+ == Install
6
+
7
+ Install flat_out like any other ruby gem:
8
+
9
+ gem install flat_out
10
+
11
+ == Overview of usage
12
+
13
+ flat_out creates a fixed length record with desired formatting of variable fields.
14
+ It was created to export a fixed length format file to send to an organization
15
+ using legacy computers and softwares.
16
+
17
+ * Create a new FlatOut object of desired length using FlatOut.new.
18
+ * Format this instance using various put methods based upon your input type.
19
+ * The final result is available via the to_s method of the object.
20
+
21
+ == Commands
22
+
23
+ Available Commands:
24
+
25
+ initialize(rec_length)
26
+ reset
27
+ 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
+
36
+
37
+ == IMPORTANT NOTE
38
+
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
+
44
+ Command:
45
+
46
+ FlatOut.new(length)
47
+
48
+ Description:
49
+ This command will create a blank string of specified length.
50
+ example:
51
+ myline = FlatOut.new(80) # initializes an 80 character blank string in myline.
52
+
53
+ Command:
54
+
55
+ myline.reset
56
+ myline.reset(new_length)
57
+
58
+ Description:
59
+ This command will blank out the string in the myline object. If no length is provided, the string's
60
+ length remains unchanged. You can change the length of the string in myline by specifying it here.
61
+
62
+ example:
63
+ myline = FlatOut.new(80) # initializes an 80 character blank string in myline.
64
+ myline.reset # re-initializes an 80 character blank string in myline.
65
+ myline.reset(132) # myline now has a 132 character blank string.
66
+
67
+ Command:
68
+
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 '
116
+
117
+ Command:
118
+
119
+ myline.put_digits_only(len, pos, fld)
120
+
121
+ 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 '
133
+
134
+ Command:
135
+
136
+ myline.put_decimal_with_point(int_dot_dec, pos, fld)
137
+
138
+ 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.
145
+
146
+ 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
+
153
+ Command:
154
+
155
+ myline.put_decimal_no_point(int_dot_dec, pos, fld)
156
+
157
+ 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.
165
+
166
+ 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.
179
+
180
+ full cycle example:
181
+
182
+ 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)
187
+ puts myline.to_s
188
+
189
+ users = %w[Anil Mark Jake]
190
+
191
+ users.each do |user|
192
+ myline.reset
193
+ myline.put_alpha(12, 4, "USERNAME :")
194
+ myline.put_alpha(12, 16, user)
195
+ puts myline.to_s
196
+ end
197
+
198
+ myline.reset
199
+ myline.put_alpha(30, 30, "END OF REPORT")
200
+ puts myline.to_s
201
+
202
+ == Contributing to flat_out
203
+
204
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
205
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
206
+ * Fork the project.
207
+ * Start a feature/bugfix branch.
208
+ * Commit and push until you are happy with your contribution.
209
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
210
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
211
+
212
+ == Copyright
213
+
214
+ Copyright (c) 2012 Anil Sharma. See LICENSE for further details.
215
+
data/lib/flat_out.rb CHANGED
@@ -0,0 +1,57 @@
1
+ class FlatOut
2
+ def initialize(rec_length)
3
+ @flat_out = " " * rec_length
4
+ @flat_length = rec_length
5
+ end
6
+
7
+ def reset(rec_length=@flat_length)
8
+ @flat_out = " " * rec_length
9
+ @flat_length = rec_length
10
+ end
11
+
12
+ def put_alpha len, pos, fld
13
+ start_pos = pos - 1 ; end_pos = start_pos + len
14
+ space_fill = " " * len
15
+ val = (fld.to_s + space_fill)[0...len]
16
+ @flat_out[start_pos...end_pos] = val
17
+ end
18
+
19
+ def put_integer len, pos, fld
20
+ data_content = fld.to_s
21
+ data_len = data_content.length
22
+ zero_fill = "0" * len
23
+ val = (zero_fill + data_content)[-len,len]
24
+ put_alpha len, pos, val
25
+ end
26
+
27
+ def put_integer_part len, pos, fld
28
+ val = fld.to_i
29
+ put_integer len, pos, val
30
+ end
31
+
32
+ def put_digits_only len, pos, fld
33
+ val = fld.gsub(/[^\d]/,"")
34
+ put_alpha len, pos, val
35
+ end
36
+
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
56
+ end
57
+ end
@@ -0,0 +1,143 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
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
+ describe "New String" do
10
+ it "should have a blank string of correct length when initialize()" do
11
+ f = FlatOut.new(6)
12
+ f.to_s.should == " "
13
+ end
14
+ end
15
+
16
+ describe "Reset String without argument" do
17
+ it "should have a blank string of unchanged length when reset()" do
18
+ f = FlatOut.new(6)
19
+ f.reset
20
+ f.to_s.should == " "
21
+ end
22
+ end
23
+
24
+ describe "Reset String with argument" do
25
+ it "should have a blank string of new length when reset()" do
26
+ f = FlatOut.new(6)
27
+ f.reset(4)
28
+ f.to_s.should == " "
29
+ end
30
+ end
31
+
32
+ describe "put_alpha exact length" do
33
+ it "should replace the correct space in the right spot" do
34
+ f = FlatOut.new(6)
35
+ f.put_alpha(3,2,"ABC")
36
+ f.to_s.should == " ABC "
37
+ end
38
+ end
39
+
40
+ describe "put_alpha short length" do
41
+ it "should blank fill the trailing space" do
42
+ f = FlatOut.new(6)
43
+ f.put_alpha(3,2,"AB")
44
+ f.to_s.should == " AB "
45
+ end
46
+ end
47
+
48
+ describe "put_alpha long length" do
49
+ it "should truncate the extra characters on the right" do
50
+ f = FlatOut.new(6)
51
+ f.put_alpha(3,2,"ABCDE")
52
+ f.to_s.should == " ABC "
53
+ end
54
+ end
55
+
56
+ describe "put_integer exact length" do
57
+ it "should replace the correct space in the right spot" do
58
+ f = FlatOut.new(6)
59
+ f.put_integer(3,2,123)
60
+ f.to_s.should == " 123 "
61
+ end
62
+ end
63
+
64
+ describe "put_integer short length" do
65
+ it "should zero fill the leading spaces" do
66
+ f = FlatOut.new(6)
67
+ f.put_integer(3,2,12)
68
+ f.to_s.should == " 012 "
69
+ end
70
+ end
71
+
72
+ describe "put_integer long length" do
73
+ it "should truncate the extra characters on the right" do
74
+ 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 "
85
+ end
86
+ end
87
+
88
+ describe "put_integer_part of an integer" do
89
+ it "should act like put_integer" do
90
+ f = FlatOut.new(6)
91
+ f.put_integer_part(3,2,12345)
92
+ f.to_s.should == " 345 "
93
+ end
94
+ end
95
+
96
+ describe "put_integer_part of a float" do
97
+ it "should remove the decimal part" do
98
+ f = FlatOut.new(6)
99
+ f.put_integer_part(3,2,12345.67)
100
+ f.to_s.should == " 345 "
101
+ end
102
+ end
103
+
104
+ describe "put_decimal_with_point for a float" do
105
+ it "should include the decimal in result for exact size" do
106
+ f = FlatOut.new(12)
107
+ f.put_decimal_with_point(5.3, 2, 12345.678)
108
+ f.to_s.should == " 12345.678 "
109
+ end
110
+ end
111
+
112
+ describe "put_decimal_with_point for a float" do
113
+ it "should truncate the result for smaller size" do
114
+ f = FlatOut.new(12)
115
+ f.put_decimal_with_point(4.2, 2, 12345.643)
116
+ f.to_s.should == " 2345.64 "
117
+ end
118
+ end
119
+
120
+ describe "put_decimal_with_point for a float" do
121
+ it "should truncate the result for smaller size(rounded)" do
122
+ f = FlatOut.new(12)
123
+ f.put_decimal_with_point(4.2, 2, 12345.678)
124
+ f.to_s.should == " 2345.68 "
125
+ end
126
+ end
127
+
128
+ describe "put_decimal_with_point for a float" do
129
+ it "should zero fill both sides" do
130
+ f = FlatOut.new(12)
131
+ f.put_decimal_with_point(4.2, 2, 1.2)
132
+ f.to_s.should == " 0001.20 "
133
+ end
134
+ end
135
+
136
+ describe "put_decimal_no_point for a float" do
137
+ it "should not have a decimal" do
138
+ f = FlatOut.new(12)
139
+ f.put_decimal_no_point(4.2, 2, 1.2)
140
+ f.to_s.should == " 000120 "
141
+ end
142
+ end
143
+
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'flat_out'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
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.1
5
+ version: 0.0.2
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-26 00:00:00 -04:00
13
+ date: 2012-05-27 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -24,6 +24,11 @@ extra_rdoc_files: []
24
24
 
25
25
  files:
26
26
  - lib/flat_out.rb
27
+ - spec/spec_helper.rb
28
+ - spec/flat_out_spec.rb
29
+ - README.rdoc
30
+ - LICENSE
31
+ - spec//spec_helper.rb
27
32
  has_rdoc: true
28
33
  homepage: http://rubygems.org/gems/flat_out
29
34
  licenses: []
@@ -52,5 +57,6 @@ rubygems_version: 1.5.3
52
57
  signing_key:
53
58
  specification_version: 3
54
59
  summary: Create fixed length records to write to a flat file.
55
- test_files: []
56
-
60
+ test_files:
61
+ - spec//spec_helper.rb
62
+ - spec/flat_out_spec.rb