flat_out 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +14 -16
- data/lib/flat_out.rb +16 -11
- data/spec/flat_out_spec.rb +18 -3
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -25,36 +25,23 @@ Install flat_out like any other ruby gem:
|
|
25
25
|
positions 0, 1, and 2 respectively. In flat_out, these would be considered
|
26
26
|
positions 1, 2, and 3 respectively.
|
27
27
|
|
28
|
-
You can change the base
|
28
|
+
You can change the base by providing an option value when creating the object.
|
29
29
|
|
30
30
|
== Commands
|
31
31
|
|
32
32
|
Available Commands:
|
33
33
|
|
34
|
-
FlatOut.
|
35
|
-
FlatOut.new(rec_length)
|
34
|
+
FlatOut.new(rec_length, options)
|
36
35
|
put(len, pos, fld)
|
37
36
|
to_s
|
38
37
|
|
39
38
|
reset
|
40
39
|
reset(rec_length)
|
40
|
+
reset(rec_length, options)
|
41
41
|
|
42
42
|
FlatOut.digits_only(fld)
|
43
43
|
FlatOut.phone_squeeze(fld)
|
44
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.
|
52
|
-
|
53
|
-
example:
|
54
|
-
FlatOut.base(0) # Set the base to zero.
|
55
|
-
FlatOut.base(1) # Set the base to 1(default).
|
56
|
-
|
57
|
-
|
58
45
|
Command:
|
59
46
|
|
60
47
|
FlatOut.new(length)
|
@@ -64,6 +51,17 @@ Command:
|
|
64
51
|
example:
|
65
52
|
myline = FlatOut.new(80) # initializes an 80 character blank string in myline.
|
66
53
|
|
54
|
+
Optionally, you can provide a base. This will change the base for position specifications.
|
55
|
+
It is also useful while working with layouts that are specified with an offset.
|
56
|
+
example:
|
57
|
+
myline = FlatOut.new(80, :base => 0) # Set the base to zero.
|
58
|
+
myline = FlatOut.new(80, :base => 1) # Set the base to 1(default).
|
59
|
+
|
60
|
+
You can also change the order of the input parameters for the put statement.
|
61
|
+
All six combinations of len, pos, fld are available.
|
62
|
+
example:
|
63
|
+
myline = FlatOut.new(80, :base => 0, :format => :fld_len_pos) # default is :len_pos_fld
|
64
|
+
|
67
65
|
Command:
|
68
66
|
|
69
67
|
myline.reset
|
data/lib/flat_out.rb
CHANGED
@@ -1,23 +1,19 @@
|
|
1
1
|
class FlatOut
|
2
|
-
def initialize(rec_length)
|
3
|
-
|
4
|
-
@flat_length = rec_length
|
5
|
-
@@base ||= 1
|
2
|
+
def initialize(rec_length, options={})
|
3
|
+
self.reset(rec_length, options)
|
6
4
|
end
|
7
5
|
|
8
|
-
def reset(rec_length=@flat_length)
|
6
|
+
def reset(rec_length=@flat_length, options={})
|
9
7
|
@flat_out = " " * rec_length
|
10
8
|
@flat_length = rec_length
|
9
|
+
@base = options[:base] ||= @base ||= 1
|
10
|
+
@format = options[:format] ||= @format ||= :len_pos_fld
|
11
11
|
end
|
12
12
|
|
13
13
|
def to_s
|
14
14
|
@flat_out
|
15
15
|
end
|
16
16
|
|
17
|
-
def self.base(base)
|
18
|
-
@@base = base
|
19
|
-
end
|
20
|
-
|
21
17
|
def self.phone_squeeze(fld)
|
22
18
|
fld.tr("-)( " , "")
|
23
19
|
end
|
@@ -26,7 +22,16 @@ class FlatOut
|
|
26
22
|
fld.gsub(/[^\d]/,"")
|
27
23
|
end
|
28
24
|
|
29
|
-
def put
|
25
|
+
def put p1, p2, p3
|
26
|
+
case @format
|
27
|
+
when :len_pos_fld then (len = p1; pos = p2; fld = p3)
|
28
|
+
when :len_fld_pos then (len = p1; fld = p2; pos = p3)
|
29
|
+
when :pos_len_fld then (pos = p1; len = p2; fld = p3)
|
30
|
+
when :pos_fld_len then (pos = p1; fld = p2; len = p3)
|
31
|
+
when :fld_pos_len then (fld = p1; pos = p2; len = p3)
|
32
|
+
when :fld_len_pos then (fld = p1; len = p2; pos = p3)
|
33
|
+
end
|
34
|
+
|
30
35
|
case fld
|
31
36
|
when String
|
32
37
|
put_alpha len, pos, fld
|
@@ -44,7 +49,7 @@ class FlatOut
|
|
44
49
|
private
|
45
50
|
|
46
51
|
def put_fld len, pos, fld
|
47
|
-
start_pos = pos -
|
52
|
+
start_pos = pos - @base ; end_pos = start_pos + len
|
48
53
|
@flat_out[start_pos...end_pos] = fld
|
49
54
|
end
|
50
55
|
|
data/spec/flat_out_spec.rb
CHANGED
@@ -23,6 +23,15 @@ describe "Reset String with argument" do
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
+
describe "reset with different base" do
|
27
|
+
it "should reset to new size with new base" do
|
28
|
+
f = FlatOut.new(6, :base => 2)
|
29
|
+
f.reset(5, :base => 1)
|
30
|
+
f.put(3,2,123)
|
31
|
+
f.to_s.should == " 123 "
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
26
35
|
describe "put exact length alpha" do
|
27
36
|
it "should replace the correct space in the right spot" do
|
28
37
|
f = FlatOut.new(6)
|
@@ -31,6 +40,14 @@ describe "put exact length alpha" do
|
|
31
40
|
end
|
32
41
|
end
|
33
42
|
|
43
|
+
describe "put exact length alpha with fld_pos_len format" do
|
44
|
+
it "should replace the correct space in the right spot" do
|
45
|
+
f = FlatOut.new(6, :format => :fld_pos_len)
|
46
|
+
f.put("ABC",2,3)
|
47
|
+
f.to_s.should == " ABC "
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
34
51
|
describe "put short length alpha" do
|
35
52
|
it "should blank fill the trailing space" do
|
36
53
|
f = FlatOut.new(6)
|
@@ -57,11 +74,9 @@ end
|
|
57
74
|
|
58
75
|
describe "put exact length integer with base 0" do
|
59
76
|
it "should replace the correct space in the right spot using base 0" do
|
60
|
-
FlatOut.base
|
61
|
-
f = FlatOut.new(6)
|
77
|
+
f = FlatOut.new(6, :base => 0)
|
62
78
|
f.put(3,2,123)
|
63
79
|
f.to_s.should == " 123 "
|
64
|
-
FlatOut.base(1)
|
65
80
|
end
|
66
81
|
end
|
67
82
|
|
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.
|
5
|
+
version: 0.0.4
|
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-
|
13
|
+
date: 2012-05-29 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|