fixed-width-structures 0.0.1 → 0.9.0

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/Rakefile CHANGED
@@ -7,5 +7,5 @@ task :default => :test
7
7
 
8
8
  desc "unit tests"
9
9
  task :test do
10
- exec 'bundle exec rspec test/**/*.rb'
10
+ exec 'bundle exec rspec -P test/**/*.rb ./'
11
11
  end
@@ -1,6 +1,6 @@
1
1
  class FixedWidthStructure
2
2
 
3
- def self.add_attribute( attribute, type, size )
3
+ def self.add_attribute( attribute, type, size, default_value = nil )
4
4
  @attributes ||= {}
5
5
  @attributes[ attribute.to_sym ] = {
6
6
  :type => type,
@@ -8,7 +8,15 @@ class FixedWidthStructure
8
8
  :position => self.size
9
9
  }
10
10
  increment_size( size )
11
- attr_accessor attribute.to_sym
11
+ if default_value
12
+ define_method attribute do
13
+ value = instance_variable_get( "@#{attribute}" )
14
+ value ? value : default_value
15
+ end
16
+ attr_writer attribute.to_sym
17
+ else
18
+ attr_accessor attribute.to_sym
19
+ end
12
20
  end
13
21
 
14
22
  def self.attributes
@@ -23,20 +31,20 @@ class FixedWidthStructure
23
31
  @size = self.size + increment
24
32
  end
25
33
 
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
34
+ def self.alphabetic( attribute, length, default_value = '' )
35
+ add_attribute( attribute, "%-#{length}.#{length}s", length, default_value )
32
36
  end
33
37
 
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
38
+ def self.numeric( attribute, length, default_value = 0 )
39
+ add_attribute( attribute, "%#{length}.#{length}d", length, default_value )
40
+ end
41
+
42
+ def self.filler( length, character = 'X' )
43
+ alphabetic( "filler_#{size}".to_sym, length, character*length )
44
+ end
45
+
46
+ def self.space( length )
47
+ filler( length, ' ' )
40
48
  end
41
49
 
42
50
  def to_s
@@ -1,3 +1,3 @@
1
1
  class FixedWidthStructure
2
- VERSION = "0.0.1"
2
+ VERSION = "0.9.0"
3
3
  end
data/test/alphabetic.rb CHANGED
@@ -27,4 +27,16 @@ describe 'Alphabetics' do
27
27
  alphabetic.to_s.should == "Cli"
28
28
  end
29
29
 
30
+ it "should use an empty string as it's default" do
31
+ @alphabetic_class.alphabetic( :name, 3 )
32
+ @alphabetic_class.new.name.should == ''
33
+ end
34
+
35
+ it "should use an populate it's default when provided" do
36
+ @alphabetic_class.alphabetic( :name, 5, "bob" )
37
+ alphabetic = @alphabetic_class.new
38
+ alphabetic.name.should == 'bob'
39
+ alphabetic.to_s.should == 'bob '
40
+ end
41
+
30
42
  end
data/test/combinations.rb CHANGED
@@ -9,10 +9,13 @@ describe 'Combinations' do
9
9
  it 'should combine the whole lot' do
10
10
  @my_class.alphabetic( :name, 10 )
11
11
  @my_class.alphabetic( :surname, 15 )
12
+ @my_class.filler( 1, ':' )
12
13
  @my_class.numeric( :age, 3 )
13
14
  @my_class.numeric( :account_number, 11 )
14
- @my_class.alphabetic( :gender, 1 )
15
+ @my_class.filler( 3 )
16
+ @my_class.alphabetic( :gender, 1, '-' )
15
17
  @my_class.numeric( :date, 8 )
18
+ @my_class.space( 7 )
16
19
 
17
20
  clive = @my_class.new
18
21
  clive.name = "Clive"
@@ -22,19 +25,19 @@ describe 'Combinations' do
22
25
  clive.gender = 'M'
23
26
  clive.date = 20110213
24
27
 
25
- clive.to_s.should == 'Clive Crous 03612345678901M20110213'
28
+ clive.to_s.should == 'Clive Crous :03612345678901XXXM20110213 '
26
29
 
27
30
  bob = @my_class.new
28
31
  bob.name = "Bobamalithoniasyner"
29
32
  bob.age = 13
30
33
  bob.surname = "Yes"
31
34
 
32
- bob.to_s.should == 'BobamalithYes 01300000000000 00000000'
35
+ bob.to_s.should == 'BobamalithYes :01300000000000XXX-00000000 '
33
36
 
34
37
  mary = @my_class.new
35
38
  mary.age = 12345
36
39
 
37
- mary.to_s.should == ' 12300000000000 00000000'
40
+ mary.to_s.should == ' :12300000000000XXX-00000000 '
38
41
 
39
42
  end
40
43
 
data/test/numeric.rb CHANGED
@@ -27,4 +27,15 @@ describe 'Numerics' do
27
27
  numeric.to_s.should == "12"
28
28
  end
29
29
 
30
+ it "should use zero as it's default" do
31
+ @numeric_class.numeric( :age, 3 )
32
+ @numeric_class.new.age.should == 0
33
+ end
34
+
35
+ it "should use an populate it's default when provided" do
36
+ @numeric_class.numeric( :age, 3, 11 )
37
+ numeric = @numeric_class.new
38
+ numeric.age.should == 11
39
+ numeric.to_s.should == '011'
40
+ end
30
41
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 9
7
8
  - 0
8
- - 1
9
- version: 0.0.1
9
+ version: 0.9.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Clive Crous
@@ -102,7 +102,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
102
102
  requirements:
103
103
  - - ">="
104
104
  - !ruby/object:Gem::Version
105
- hash: 3481926565919442266
105
+ hash: 1678197979565188390
106
106
  segments:
107
107
  - 0
108
108
  version: "0"
@@ -111,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  requirements:
112
112
  - - ">="
113
113
  - !ruby/object:Gem::Version
114
- hash: 3481926565919442266
114
+ hash: 1678197979565188390
115
115
  segments:
116
116
  - 0
117
117
  version: "0"