fixed-width-structures 0.0.1
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/.gitignore +6 -0
- data/Gemfile +4 -0
- data/Rakefile +11 -0
- data/fixed-width-structures.gemspec +23 -0
- data/lib/fixed-width-structures/version.rb +3 -0
- data/lib/fixed-width-structures.rb +49 -0
- data/test/alphabetic.rb +30 -0
- data/test/combinations.rb +42 -0
- data/test/numeric.rb +30 -0
- metadata +126 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "fixed-width-structures/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "fixed-width-structures"
|
7
|
+
s.version = FixedWidthStructure::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Clive Crous"]
|
10
|
+
s.email = ["clive@crous.co.za"]
|
11
|
+
s.homepage = "http://www.darkarts.co.za/fixed-width-structures"
|
12
|
+
s.summary = %q{Simplify usage of fixed width text based data structures}
|
13
|
+
s.description = %q{Simplify usage of fixed width text based data structures as one generally finds used in older apps written in languages like COBOL}
|
14
|
+
|
15
|
+
s.add_development_dependency "bundler", ">= 1.0.0"
|
16
|
+
s.add_development_dependency "rake", [">= 0.8.7","< 0.9.0"]
|
17
|
+
s.add_development_dependency "rspec", ">= 1.3.0"
|
18
|
+
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
class FixedWidthStructure
|
2
|
+
|
3
|
+
def self.add_attribute( attribute, type, size )
|
4
|
+
@attributes ||= {}
|
5
|
+
@attributes[ attribute.to_sym ] = {
|
6
|
+
:type => type,
|
7
|
+
:size => size,
|
8
|
+
:position => self.size
|
9
|
+
}
|
10
|
+
increment_size( size )
|
11
|
+
attr_accessor attribute.to_sym
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.attributes
|
15
|
+
@attributes ||= {}
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.size
|
19
|
+
@size ||= 0
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.increment_size( increment )
|
23
|
+
@size = self.size + increment
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.alphabetic( attribute, length )
|
27
|
+
add_attribute( attribute, "%-#{length}.#{length}s", length )
|
28
|
+
define_method attribute do
|
29
|
+
value = instance_variable_get( "@#{attribute}" )
|
30
|
+
value ? value : ''
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.numeric( attribute, length )
|
35
|
+
add_attribute( attribute, "%#{length}.#{length}d", length )
|
36
|
+
define_method attribute do
|
37
|
+
value = instance_variable_get( "@#{attribute}" )
|
38
|
+
value ? value : 0
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_s
|
43
|
+
self.class.attributes.to_a.sort{|a,b|a.last[:position]<=>b.last[:position]}.inject('') do |result,current|
|
44
|
+
this = current.last[ :type ]%send(current.first)
|
45
|
+
result << "%-#{current.last[:size]}.#{current.last[:size]}s"%this
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
data/test/alphabetic.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'fixed-width-structures'
|
2
|
+
|
3
|
+
describe 'Alphabetics' do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@alphabetic_class = Class.new( FixedWidthStructure )
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should increment the position' do
|
10
|
+
@alphabetic_class.size.should == 0
|
11
|
+
@alphabetic_class.alphabetic( :name, 20 )
|
12
|
+
@alphabetic_class.size.should == 20
|
13
|
+
FixedWidthStructure.size.should == 0
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should pad with space' do
|
17
|
+
@alphabetic_class.alphabetic( :name, 10 )
|
18
|
+
alphabetic = @alphabetic_class.new
|
19
|
+
alphabetic.name = "Clive"
|
20
|
+
alphabetic.to_s.should == "Clive "
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should cut fields that are too long' do
|
24
|
+
@alphabetic_class.alphabetic( :name, 3 )
|
25
|
+
alphabetic = @alphabetic_class.new
|
26
|
+
alphabetic.name = "Clive"
|
27
|
+
alphabetic.to_s.should == "Cli"
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'fixed-width-structures'
|
2
|
+
|
3
|
+
describe 'Combinations' do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@my_class = Class.new( FixedWidthStructure )
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should combine the whole lot' do
|
10
|
+
@my_class.alphabetic( :name, 10 )
|
11
|
+
@my_class.alphabetic( :surname, 15 )
|
12
|
+
@my_class.numeric( :age, 3 )
|
13
|
+
@my_class.numeric( :account_number, 11 )
|
14
|
+
@my_class.alphabetic( :gender, 1 )
|
15
|
+
@my_class.numeric( :date, 8 )
|
16
|
+
|
17
|
+
clive = @my_class.new
|
18
|
+
clive.name = "Clive"
|
19
|
+
clive.surname = "Crous"
|
20
|
+
clive.age = 36
|
21
|
+
clive.account_number = 12345678901
|
22
|
+
clive.gender = 'M'
|
23
|
+
clive.date = 20110213
|
24
|
+
|
25
|
+
clive.to_s.should == 'Clive Crous 03612345678901M20110213'
|
26
|
+
|
27
|
+
bob = @my_class.new
|
28
|
+
bob.name = "Bobamalithoniasyner"
|
29
|
+
bob.age = 13
|
30
|
+
bob.surname = "Yes"
|
31
|
+
|
32
|
+
bob.to_s.should == 'BobamalithYes 01300000000000 00000000'
|
33
|
+
|
34
|
+
mary = @my_class.new
|
35
|
+
mary.age = 12345
|
36
|
+
|
37
|
+
mary.to_s.should == ' 12300000000000 00000000'
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
data/test/numeric.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'fixed-width-structures'
|
2
|
+
|
3
|
+
describe 'Numerics' do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@numeric_class = Class.new( FixedWidthStructure )
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should increment the position' do
|
10
|
+
@numeric_class.size.should == 0
|
11
|
+
@numeric_class.numeric( :age, 20 )
|
12
|
+
@numeric_class.size.should == 20
|
13
|
+
FixedWidthStructure.size.should == 0
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should pad with zeros' do
|
17
|
+
@numeric_class.numeric( :age, 10 )
|
18
|
+
numeric = @numeric_class.new
|
19
|
+
numeric.age = 37
|
20
|
+
numeric.to_s.should == "0000000037"
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should cut fields that are too long' do
|
24
|
+
@numeric_class.numeric( :age, 2 )
|
25
|
+
numeric = @numeric_class.new
|
26
|
+
numeric.age = 127
|
27
|
+
numeric.to_s.should == "12"
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fixed-width-structures
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Clive Crous
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-05-30 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: bundler
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 0
|
30
|
+
- 0
|
31
|
+
version: 1.0.0
|
32
|
+
type: :development
|
33
|
+
prerelease: false
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rake
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
- 8
|
45
|
+
- 7
|
46
|
+
version: 0.8.7
|
47
|
+
- - <
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
segments:
|
50
|
+
- 0
|
51
|
+
- 9
|
52
|
+
- 0
|
53
|
+
version: 0.9.0
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: *id002
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
name: rspec
|
59
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
segments:
|
65
|
+
- 1
|
66
|
+
- 3
|
67
|
+
- 0
|
68
|
+
version: 1.3.0
|
69
|
+
type: :development
|
70
|
+
prerelease: false
|
71
|
+
version_requirements: *id003
|
72
|
+
description: Simplify usage of fixed width text based data structures as one generally finds used in older apps written in languages like COBOL
|
73
|
+
email:
|
74
|
+
- clive@crous.co.za
|
75
|
+
executables: []
|
76
|
+
|
77
|
+
extensions: []
|
78
|
+
|
79
|
+
extra_rdoc_files: []
|
80
|
+
|
81
|
+
files:
|
82
|
+
- .gitignore
|
83
|
+
- Gemfile
|
84
|
+
- Rakefile
|
85
|
+
- fixed-width-structures.gemspec
|
86
|
+
- lib/fixed-width-structures.rb
|
87
|
+
- lib/fixed-width-structures/version.rb
|
88
|
+
- test/alphabetic.rb
|
89
|
+
- test/combinations.rb
|
90
|
+
- test/numeric.rb
|
91
|
+
has_rdoc: true
|
92
|
+
homepage: http://www.darkarts.co.za/fixed-width-structures
|
93
|
+
licenses: []
|
94
|
+
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
hash: 3481926565919442266
|
106
|
+
segments:
|
107
|
+
- 0
|
108
|
+
version: "0"
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
hash: 3481926565919442266
|
115
|
+
segments:
|
116
|
+
- 0
|
117
|
+
version: "0"
|
118
|
+
requirements: []
|
119
|
+
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 1.3.7
|
122
|
+
signing_key:
|
123
|
+
specification_version: 3
|
124
|
+
summary: Simplify usage of fixed width text based data structures
|
125
|
+
test_files: []
|
126
|
+
|