posifile 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -3,6 +3,10 @@
3
3
  This library is intended to make it easier to read/write any kind of position file.
4
4
  All you have to do is write in the model how each position will be associated to each objects attributes.
5
5
 
6
+ === Instalation
7
+
8
+ gem install posifile
9
+
6
10
  === Usage
7
11
 
8
12
  The most difficult thing to do is figure out how the file is laid out. Once you can 'see' the fields, using this library is easy.
@@ -14,11 +18,8 @@ This is the case where there is only one line per file, and each file constains
14
18
  require 'posifile'
15
19
 
16
20
  class Client < Posifile
17
- set_specification{
18
- "name"="0..10",
19
- "city"=>"11..31",
20
- "country"=>"32..50"
21
- }
21
+ set_specification("name"="0..10", "city"=>"11..31", "country"=>"32..50")
22
+
22
23
  #you can put here any other method you'd like
23
24
 
24
25
  end
@@ -33,7 +34,7 @@ And you will get as object attributes every key you specified in the above hash:
33
34
  Note that every key in the hash needs to be all downcase, because they'r gonna be method's names.
34
35
 
35
36
  ==== Multi-line file
36
- When the file you need to read has many sections and many registers in only one file, you can use some code (generally specified in some kind of document layout) to be able to tell to wich section some register belongs to. Here this code is in first three positions of the file.
37
+ When the file you need to read has many sections and many registers in only one file, you can use some code (generally specified in some kind of document layout, and known as "section code") to be able to tell to wich section some register belongs to. Here this code is in first three positions of the file.
37
38
 
38
39
  # car
39
40
  require 'rubygems'
@@ -67,8 +68,17 @@ We would do:
67
68
  cliennt.golf['color'] # => 'red'
68
69
 
69
70
  Notice that in multi-lin files, the method's names will always be the downcased version of the value found in the given position.
71
+ Whenever set_attr_name is not specified, the method creation rules will be the same as in a one-line file.
72
+
73
+ To list every attribute created for your object, do:
74
+ client.pos_attributes # => ['boxter', 'golf', 'vivasix']
75
+
76
+ The code used to identify between the two specifications (in the example above they are 001 and 002) are called "section code", and is automatically included in tha specification hash, so if you need it:
77
+
78
+ client.boxter['section_code']
79
+
70
80
 
71
- === feedback
81
+ === Feedback
72
82
 
73
83
  Email: marcofognog@gmail.com
74
84
  Any ideas on how we can make this better will be appreciated.
data/lib/errors.rb CHANGED
@@ -1,6 +1,6 @@
1
- class UppercaseFieldsError < Exception
2
- end
3
1
  class FieldsNotSpecified < Exception
4
2
  end
5
- class AttrNameNotSpecified < Exception
3
+ class InvalidFieldName < Exception
4
+ end
5
+ class InvalidAttrName < Exception
6
6
  end
data/lib/posifile.rb CHANGED
@@ -1,11 +1,14 @@
1
1
  require 'errors'
2
2
  class Posifile
3
+
4
+ INVALID_CHARS = " !@#$\%*()-\&\'\"<>.,;:[]{}\'\`^+\\/"
5
+
3
6
  @@specifications = {}
4
7
  @@conditions = {}
5
8
  @@attr_names = {}
6
9
  @@pos_attr = {}
7
10
 
8
- attr_accessor :data_file
11
+ attr_accessor :data_file, :raw_content
9
12
 
10
13
  def initialize(data_file_name)
11
14
  @data_file = data_file_name
@@ -15,7 +18,6 @@ class Posifile
15
18
  if file_content.length == 1
16
19
  build_attributes_from_hash(@@specifications[self.class][0], line, nil)
17
20
  else
18
- check_attr_names
19
21
  specification_index(line) do |line_,index|
20
22
  build_attributes_from_hash(@@specifications[self.class][index], line_,@@attr_names[self.class][index])
21
23
  end
@@ -24,19 +26,29 @@ class Posifile
24
26
  end
25
27
 
26
28
  def self.set_specification(hash)
27
- unless check_for_uppercase(hash)
29
+ @@attr_names[self] ||= []
30
+ if valid_names?(hash)
28
31
  @@specifications[self] ||= []
29
32
  @@specifications[self] << hash
30
33
  else
31
- raise UppercaseFieldsError, "Fields names must be all downcase letters"
34
+ raise InvalidFieldName, "Fields names contain invalid characteres for method names."
32
35
  end
33
36
  end
34
37
 
35
38
  def self.lines_where(range,value,&block)
36
-
39
+ @@attr_names[self] ||= []
40
+ length_before = @@attr_names[self].length
37
41
  @@conditions[self] ||= []
38
42
  @@conditions[self] << {range,value}
39
43
  yield
44
+ length_after = @@attr_names[self].length
45
+ if length_before == length_after
46
+ @@attr_names[self] << nil
47
+ end
48
+ section_code_hash = {"section_code"=>range}
49
+ if @@specifications[self]
50
+ @@specifications[self].last.merge!(section_code_hash)
51
+ end
40
52
  end
41
53
 
42
54
  def self.set_attr_name(attr_name)
@@ -44,12 +56,15 @@ class Posifile
44
56
  @@attr_names[self] << attr_name
45
57
  end
46
58
 
47
- def self.check_for_uppercase(hash)
48
- check = false
59
+ def self.valid_names?(hash)
60
+ check = true
49
61
  hash.each_key do |key|
50
62
  key.split('').each do |letter|
51
- if letter.upcase ==letter
52
- check = true
63
+ unless letter.downcase == letter
64
+ check = false
65
+ end
66
+ if INVALID_CHARS.split("").include?(letter)
67
+ check = false
53
68
  end
54
69
  end
55
70
  end
@@ -121,16 +136,6 @@ class Posifile
121
136
  end
122
137
  end
123
138
 
124
- def check_attr_names
125
- unless @@attr_names[self.class].nil?
126
- if @@specifications[self.class].length != @@attr_names[self.class].length
127
- raise AttrNameNotSpecified, "Every lines_where method must have one set_attr_name call."
128
- end
129
- else
130
- raise AttrNameNotSpecified, "Every lines_where method must have one set_attr_name call."
131
- end
132
- end
133
-
134
139
  def specification_index(line)
135
140
  i = 0
136
141
  unless @@conditions[self.class].nil?
@@ -154,24 +159,31 @@ class Posifile
154
159
  end
155
160
 
156
161
  def file_content
157
- file = File.open(@data_file,"r")
158
- file.readlines
162
+ if raw_content.nil?
163
+ file = File.open(@data_file,"r")
164
+ @raw_content = file.readlines
165
+ end
166
+ @raw_content
159
167
  end
160
168
 
161
169
  def field_value(field_name,specification_hash,line)
162
- if field_name.class ==Symbol
170
+ if field_name.class == Symbol
163
171
  field_name = field_name.to_s
164
172
  end
165
173
  content_ar = line.split('')
166
174
  value_str = ''
167
- range = specification_hash[field_name]
168
- range.each do |n|
169
- value_str.concat content_ar[n]
175
+ if !specification_hash.keys.include? field_name
176
+ raise InvalidAttrName, "The specified attr name was not found in the specification hash."
177
+ else
178
+ range = specification_hash[field_name]
179
+ range.each do |n|
180
+ value_str.concat content_ar[n]
181
+ end
182
+ value_parse value_str
170
183
  end
171
- value_parse value_str
172
184
  end
173
185
 
174
- # get the value ignoring white spaces in th end of the string.
186
+ # get the value ignoring white spaces in the end of the string.
175
187
  def value_parse(value_string)
176
188
  ar = value_string.split(' ')
177
189
  ar.join(' ')
@@ -188,14 +200,25 @@ class Posifile
188
200
  end
189
201
  end
190
202
 
191
- def build_attributes_from_hash(specification_hash,line,attr_name)
203
+ # Once the name is gonna be used as method name,
204
+ # this changes the name when it contains invalid characters.
205
+ def change_name(original)
206
+ original.gsub!(' ','_')
207
+ INVALID_CHARS.split('').each do |char|
208
+ original.gsub!(char,'')
209
+ end
210
+ original.downcase
211
+ end
192
212
 
213
+ def build_attributes_from_hash(specification_hash,line,attr_name)
193
214
  unless attr_name.nil?
194
215
  values_hash = {}
195
216
  specification_hash.each do |key, value|
196
217
  values_hash[key] = field_value(key, specification_hash, line )
197
218
  end
198
- method_name = field_value(attr_name,specification_hash,line).downcase
219
+
220
+ method_name = change_name(field_value(attr_name,specification_hash,line))
221
+
199
222
  add_method_to_pos_attr(method_name)
200
223
  self.instance_eval "
201
224
  def #{method_name}\n
data/posifile.gemspec CHANGED
@@ -1,8 +1,8 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'posifile'
3
- s.version = '0.2.0'
4
- s.summary = 'Inteds to make easier to read position files.'
5
- s.description = 'Inteds to make easier to read position files. Still very imature.'
3
+ s.version = '0.2.1'
4
+ s.summary = 'Ruby library to make it easier to read position files.'
5
+ s.description = 'Ruby library to make it easier to read position files.'
6
6
  s.files = `git ls-files`.split("\n")
7
7
  s.homepage = 'http://github.com/marcofognog/posifile'
8
8
  s.platform = Gem::Platform::RUBY
data/tests/Rakefile CHANGED
@@ -5,5 +5,5 @@ task :default => [:test]
5
5
  desc "Run basic tasks"
6
6
  Rake::TestTask.new("test") do |t|
7
7
  t.libs << ["../lib","models"]
8
- t.test_files = FileList['test*.rb', 'lines_where/test*.rb','validations/test*.rb']
8
+ t.test_files = FileList['test*.rb', 'lines_where/test*.rb','validations/test*.rb', 'bov/test*.rb']
9
9
  end
data/tests/client.rb ADDED
@@ -0,0 +1,6 @@
1
+ class Client < Posifile
2
+ set_specification("name"=>0..10,
3
+ "city"=>11..31,
4
+ "country"=>32..42
5
+ )
6
+ end
@@ -0,0 +1,94 @@
1
+ require 'posifile'
2
+ require 'test/unit'
3
+ require 'test_helpers'
4
+
5
+ class NoSpecModel < Posifile
6
+ lines_where 0..2, "001" do
7
+ end
8
+ end
9
+
10
+ class MultiLinesWithTwoSpecsWithOneSetAttrNameMissing1 < Posifile
11
+ # two specifications, for a multi-line file, but one them doesn't have the set_attr_name. It should behave like in the one-line file.
12
+ lines_where 0..2, "001" do
13
+ set_specification( "color"=>13..22,"brand"=>3..12)
14
+ set_attr_name :brand
15
+ end
16
+
17
+ lines_where 0..2, "002" do
18
+ set_specification("brand"=>3..12, "color"=>13..22)
19
+ end
20
+ end
21
+
22
+ class MultiLinesWithTwoSpecsWithOneSetAttrNameMissing2 < Posifile
23
+ # one specification, for a multi-line file, but doesn't have the set_attr_name. It should behave like in the one-line file.
24
+ lines_where(0..2, "001") do
25
+ set_specification( "color"=>13..22,"brand"=>3..12)
26
+ end
27
+ end
28
+
29
+ class MultiLinesWithTwoSpecsWithOneSetAttrNameMissing3 < Posifile
30
+ # this is excatly the same as MultiLinesWithTwoSpecsWithOneSetAttrNameMissing1 but in different order. And with different attr's
31
+ lines_where(0..2, "001") do
32
+ set_specification("brand"=>3..12, "color"=>13..22)
33
+ end
34
+
35
+ lines_where 0..2, "002" do
36
+ set_specification( "color1"=>13..22,"brand1"=>3..12)
37
+ set_attr_name :brand1
38
+ end
39
+ end
40
+
41
+
42
+ class MultiLinesWithTwoSpecsWithOneSetAttrNameMissing4 < Posifile
43
+ # this class is used to test if order in the declariations matter
44
+ lines_where(0..2, "001") do
45
+ set_specification("brand"=>3..12, "color"=>13..22)
46
+ end
47
+
48
+ lines_where 0..2, "002" do
49
+ set_specification( "color1"=>13..22,"brand1"=>3..12)
50
+ set_attr_name :brand1
51
+ end
52
+
53
+ lines_where(0..2, "003") do
54
+ set_specification("brand2"=>3..12, "color2"=>13..22)
55
+ end
56
+ end
57
+
58
+ class TestAttrNotSpecified < Test::Unit::TestCase
59
+ include TestHelpers
60
+
61
+ def setup
62
+ create_multi_lines_sample
63
+ end
64
+
65
+ def test_no_spec_model
66
+ assert_raise(FieldsNotSpecified) do
67
+ no_spec = NoSpecModel.new("samples/sample.txt")
68
+ end
69
+ end
70
+
71
+ def test_attr_name_not_specified
72
+ car = MultiLinesWithTwoSpecsWithOneSetAttrNameMissing1.new("samples/multi_line_sample.txt")
73
+ assert_equal String, car.color.class
74
+ end
75
+
76
+ def test_attr_name_not_specified2
77
+ car = MultiLinesWithTwoSpecsWithOneSetAttrNameMissing2.new("samples/multi_line_sample.txt")
78
+ assert_equal String, car.brand.class
79
+ end
80
+
81
+ def test_attr_name_not_specified3
82
+ car = MultiLinesWithTwoSpecsWithOneSetAttrNameMissing3.new("samples/multi_line_sample.txt")
83
+ assert_equal Hash, car.harley.class
84
+ assert_equal String, car.color.class
85
+ end
86
+
87
+ def test_attr_name_not_specified4
88
+ car = MultiLinesWithTwoSpecsWithOneSetAttrNameMissing4.new("samples/multi_line_sample.txt")
89
+ assert_equal String, car.color.class
90
+ assert_equal Hash, car.harley.class
91
+ assert_equal String, car.color2.class
92
+ end
93
+
94
+ end
@@ -0,0 +1,81 @@
1
+ require 'posifile'
2
+ require 'test/unit'
3
+ require 'test_helpers'
4
+
5
+ class MultiLinesWithTwoSpecs1 < Posifile
6
+
7
+ # two specifications, for a multi-line file
8
+ lines_where 0..2, "001" do
9
+ set_specification( "color"=>13..22,"brand"=>3..12)
10
+ set_attr_name :color
11
+ end
12
+
13
+ lines_where 0..2, "002" do
14
+ set_specification("brand"=>3..12)
15
+ set_attr_name :brand
16
+ end
17
+ end
18
+
19
+ class MultiLinesWithTwoSpecs2 < Posifile
20
+
21
+ # two specifications, for a multi-line file
22
+ lines_where 0..2, "001" do
23
+ set_specification( "color"=>13..22,"brand"=>3..12)
24
+ set_attr_name :brand
25
+ end
26
+
27
+ lines_where 0..2, "002" do
28
+ set_specification("brand"=>3..12)
29
+ set_attr_name :brand
30
+ end
31
+ end
32
+
33
+
34
+ class MultiLinesWithOneSpec < Posifile
35
+
36
+ # just one specification, for a two-line file, should raise exception on initialize
37
+ # but it doesn't do that yet, and dont have tests for this, but seems to work.
38
+ lines_where 0..2, "001" do
39
+ set_specification("brand"=>3..12)
40
+ set_attr_name :brand
41
+ end
42
+ end
43
+
44
+ class TestBasics < Test::Unit::TestCase
45
+
46
+ include TestHelpers
47
+
48
+ def setup
49
+ create_multi_lines_sample
50
+ end
51
+
52
+ def test_lines_where_two_lines_two_specs_1
53
+ # with :color as attr_name
54
+ car = MultiLinesWithTwoSpecs1.new("samples/multi_line_sample.txt")
55
+
56
+ assert_equal "vectra", car.white['brand']
57
+ assert_equal "uno", car.yellow['brand']
58
+ assert_equal "white", car.white['color']
59
+ assert_equal "yamaha", car.yamaha["brand"]
60
+
61
+ end
62
+
63
+ def test_lines_where_two_lines_two_specs_2
64
+ # with :brand as attr_name
65
+ car = MultiLinesWithTwoSpecs2.new("samples/multi_line_sample.txt")
66
+
67
+ assert_equal Hash, car.fusca.class
68
+ assert_equal "bege",car.fusca['color']
69
+ end
70
+
71
+ def test_lines_where_two_lines_one_spec
72
+ car = MultiLinesWithOneSpec.new("samples/multi_line_sample.txt")
73
+ assert_equal "fusca", car.fusca["brand"]
74
+ end
75
+
76
+ def test_section_code
77
+ car = MultiLinesWithTwoSpecs1.new("samples/multi_line_sample.txt")
78
+ assert_equal "001", car.white["section_code"]
79
+ end
80
+
81
+ end
@@ -0,0 +1,23 @@
1
+ require 'posifile'
2
+ require 'test/unit'
3
+ require 'test_helpers'
4
+
5
+ class MultiLinesWithOneSpecAndAbsetAttrName < Posifile
6
+ # two specifications, for a multi-line file, but one them doesn't have the set_attr_name. It should behave like in the one-line file.
7
+ lines_where 0..2, "001" do
8
+ set_specification( "color"=>13..22,"brand"=>3..12)
9
+ set_attr_name :los_cabaleros_del_zodiaco
10
+ end
11
+ end
12
+
13
+ class TestInvalidAttrName < Test::Unit::TestCase
14
+ include TestHelpers
15
+ def setup
16
+ end
17
+
18
+ def test_invalid_attr_name
19
+ assert_raise(InvalidAttrName) do
20
+ car = MultiLinesWithOneSpecAndAbsetAttrName.new("samples/multi_line_sample.txt")
21
+ end
22
+ end
23
+ end
@@ -25,7 +25,7 @@ class TestLinesWhere < Test::Unit::TestCase
25
25
  end
26
26
 
27
27
  def test_lines_where_one_line_one_spec_with_uppercase
28
- assert_raise(UppercaseFieldsError) do
28
+ assert_raise(InvalidFieldName) do
29
29
  OneLineWithOneSpecWithUppercase.lines_where 0..2, "001" do
30
30
  OneLineWithOneSpecWithUppercase.set_specification("Color"=>3..12)
31
31
  end
@@ -0,0 +1,31 @@
1
+ require 'posifile'
2
+ require 'test/unit'
3
+ require 'test_helpers'
4
+
5
+ class MultiLinesWithUppercaseFields < Posifile
6
+
7
+ # two specifications, for a multi-line file
8
+ lines_where 0..2, "001" do
9
+ set_specification( "color"=>13..22,"brand"=>3..12)
10
+ set_attr_name :brand
11
+ end
12
+ end
13
+
14
+ class TestUppercaseFields < Test::Unit::TestCase
15
+
16
+ include TestHelpers
17
+
18
+ def setup
19
+ create_multi_lines_sample
20
+ create_multi_lines_sample_with_uppercase
21
+ end
22
+
23
+ def test_lines_where_with_uppercase_fields
24
+ car = MultiLinesWithUppercaseFields.new("samples/multi_line_sample_with_uppercase.txt")
25
+
26
+ assert_equal "bege", car.fusca['color']
27
+ assert_equal "Yellow", car.uno['color']
28
+ end
29
+
30
+ end
31
+
@@ -46,6 +46,7 @@ module TestHelpers
46
46
  001vectra white
47
47
  002harley black
48
48
  001fusca bege
49
+ 003berlineta rosa
49
50
  END
50
51
  sample = File.new("samples/multi_line_sample.txt","wb")
51
52
  sample.puts content
@@ -0,0 +1,14 @@
1
+ require 'posifile'
2
+ require 'test/unit'
3
+ require 'test_helpers'
4
+
5
+ class TestsChangeName < Test::Unit::TestCase
6
+
7
+ include TestHelpers
8
+
9
+ def test_um
10
+ c = Client.new("samples/sample.txt")
11
+ assert_equal "parangaricotirimirru_aro",c.change_name("parangaricotirIMi*rr\&u a-r.o")
12
+ end
13
+
14
+ end
@@ -1,13 +1,7 @@
1
1
  require 'posifile'
2
2
  require 'test/unit'
3
3
  require 'test_helpers'
4
-
5
- class Client < Posifile
6
- set_specification("name"=>0..10,
7
- "city"=>11..31,
8
- "country"=>32..42
9
- )
10
- end
4
+ require 'client'
11
5
 
12
6
  class MultiLinesWithTwoSpecs3 < Posifile
13
7
 
@@ -23,6 +17,10 @@ class TestPosAttributes < Test::Unit::TestCase
23
17
 
24
18
  include TestHelpers
25
19
 
20
+ def setup
21
+ create_sample_file
22
+ end
23
+
26
24
  def test_pos_attributes_one_line
27
25
  c = Client.new("samples/sample.txt")
28
26
  assert c.pos_attributes.include? "city"
@@ -1,13 +1,7 @@
1
1
  require 'posifile'
2
2
  require 'test/unit'
3
3
  require 'test_helpers'
4
-
5
- class Client < Posifile
6
- set_specification("name"=>0..10,
7
- "city"=>11..31,
8
- "country"=>32..42
9
- )
10
- end
4
+ require 'client'
11
5
 
12
6
  class NoSpec < Posifile
13
7
  end
@@ -10,7 +10,7 @@ class TestPosifile < Test::Unit::TestCase
10
10
  include TestHelpers
11
11
 
12
12
  def test_set_spec_with_uppercase
13
- assert_raise(UppercaseFieldsError) do
13
+ assert_raise(InvalidFieldName) do
14
14
  SpecWithUppercase.set_specification("Name"=>0..10, "CITY"=>11..31,"country"=>32..42)
15
15
  end
16
16
  end
@@ -0,0 +1,39 @@
1
+ require 'posifile'
2
+ require 'test/unit'
3
+ require 'test_helpers'
4
+
5
+ class UppercasedSpec < Posifile
6
+ end
7
+
8
+ class TestsCheckForUppercase < Test::Unit::TestCase
9
+
10
+ include TestHelpers
11
+
12
+ def tests_valid_names_true
13
+ assert UppercasedSpec.valid_names?({"name"=>1..10, "city"=>11..20, "country"=>21..32})
14
+ end
15
+
16
+ def tests_valid_names_true2
17
+ assert UppercasedSpec.valid_names?("nome_do_arquivo"=>2..14,"codigo_de_origem"=>15..22,"data_de_geracao_do_arquivo"=>23..30)
18
+ end
19
+
20
+ def tests_valid_names_false
21
+ assert !UppercasedSpec.valid_names?({"Name"=>1..10, "CITY"=>11..20, "country"=>21..32})
22
+ end
23
+
24
+ def tests_valid_names_false2
25
+ assert !UppercasedSpec.valid_names?("nome do arquivo"=>2..14,"codigo"=>15..22,"data"=>23..30)
26
+ end
27
+
28
+ def tests_valid_names_false3
29
+ assert !UppercasedSpec.valid_names?({"Name"=>1..10, "last name"=>11..20, "country"=>21..32})
30
+ end
31
+
32
+ def tests_valid_names_false4
33
+ assert !UppercasedSpec.valid_names?({"name"=>1..10, "last-name"=>11..20, "country"=>21..32})
34
+ end
35
+
36
+ def tests_valid_names_false5
37
+ assert !UppercasedSpec.valid_names?({"name*"=>1..10})
38
+ end
39
+ end
@@ -0,0 +1,65 @@
1
+ require 'posifile'
2
+ require 'test/unit'
3
+ require 'test_helpers'
4
+
5
+ class WithLinesWhere < Posifile
6
+ # this class is used to test if order in the declariations matter
7
+ lines_where(0..2, "001") do
8
+ set_specification("brand"=>3..12, "color"=>13..22)
9
+ end
10
+
11
+ lines_where 0..2, "002" do
12
+ set_specification( "color1"=>13..22,"brand1"=>3..12)
13
+ set_attr_name :brand1
14
+ end
15
+
16
+ lines_where(0..2, "003") do
17
+ set_specification("brand2"=>3..12, "color2"=>13..22)
18
+ end
19
+ end
20
+
21
+ class WithLinesWhereWithGap < Posifile
22
+ # this class is used to test if order in the declariations matter
23
+ lines_where(0..2, "001") do
24
+ set_specification("brand"=>3..12, "color"=>15..22)
25
+ end
26
+
27
+ lines_where 0..2, "002" do
28
+ set_specification( "color1"=>13..22,"brand1"=>3..12)
29
+ set_attr_name :brand1
30
+ end
31
+
32
+ lines_where(0..2, "003") do
33
+ set_specification("brand2"=>3..12, "color2"=>13..22)
34
+ end
35
+ end
36
+
37
+ class WithLinesWhereWithOverlap < Posifile
38
+ # this class is used to test if order in the declariations matter
39
+ lines_where(0..2, "001") do
40
+ set_specification("brand"=>3..12, "color"=>13..22)
41
+ end
42
+
43
+ lines_where 0..2, "002" do
44
+ set_specification( "color1"=>13..22,"brand1"=>3..12)
45
+ set_attr_name :brand1
46
+ end
47
+
48
+ lines_where(0..2, "003") do
49
+ set_specification("brand2"=>3..12, "color2"=>10..22)
50
+ end
51
+ end
52
+
53
+ class TestLinesWhere < Test::Unit::TestCase
54
+ def test_valid_true
55
+ assert WithLinesWhere.valid_specification?
56
+ end
57
+
58
+ def test_with_gap
59
+ assert !WithLinesWhereWithGap.valid_specification?
60
+ end
61
+
62
+ def test_with_overlap
63
+ assert !WithLinesWhereWithGap.valid_specification?
64
+ end
65
+ end
@@ -1,6 +1,7 @@
1
1
  require 'posifile'
2
2
  require 'test/unit'
3
3
  require 'test_helpers'
4
+ require 'client'
4
5
 
5
6
  class WithGap <Posifile
6
7
  set_specification("name"=>0..10, "city"=>11..31, "country"=>36..52)
@@ -14,7 +15,6 @@ class BothGapAndOverlap < Posifile
14
15
  set_specification("name"=>0..8, "address" => 10..20, "job" => 16..30)
15
16
  end
16
17
 
17
-
18
18
  class TestValidations < Test::Unit::TestCase
19
19
 
20
20
  include TestHelpers
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: posifile
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Marco Antonio Foga\xC3\xA7a Nogueira"
@@ -15,11 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-05 00:00:00 -03:00
18
+ date: 2011-04-09 00:00:00 -03:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
22
- description: Inteds to make easier to read position files. Still very imature.
22
+ description: Ruby library to make it easier to read position files.
23
23
  email:
24
24
  - marcofognog@gmail.com
25
25
  executables: []
@@ -35,14 +35,20 @@ files:
35
35
  - lib/posifile.rb
36
36
  - posifile.gemspec
37
37
  - tests/Rakefile
38
- - tests/lines_where/tests_multi_line_files.rb
38
+ - tests/client.rb
39
+ - tests/lines_where/tests_attr_not_specified.rb
40
+ - tests/lines_where/tests_basics.rb
41
+ - tests/lines_where/tests_invalid_attr_name.rb
39
42
  - tests/lines_where/tests_one_line_files.rb
43
+ - tests/lines_where/tests_uppercase_fields.rb
40
44
  - tests/test_helpers.rb
45
+ - tests/tests_change_name.rb
41
46
  - tests/tests_check_condition.rb
42
- - tests/tests_check_for_uppercase.rb
43
47
  - tests/tests_pos_attributes.rb
44
48
  - tests/tests_posifile.rb
45
49
  - tests/tests_set_spec_with_uppercase.rb
50
+ - tests/tests_valid_names.rb
51
+ - tests/validations/tests_lines_where.rb
46
52
  - tests/validations/tests_validations.rb
47
53
  has_rdoc: true
48
54
  homepage: http://github.com/marcofognog/posifile
@@ -79,6 +85,6 @@ rubyforge_project:
79
85
  rubygems_version: 1.6.1
80
86
  signing_key:
81
87
  specification_version: 3
82
- summary: Inteds to make easier to read position files.
88
+ summary: Ruby library to make it easier to read position files.
83
89
  test_files: []
84
90
 
@@ -1,137 +0,0 @@
1
- require 'posifile'
2
- require 'test/unit'
3
- require 'test_helpers'
4
-
5
- class MultiLinesWithTwoSpecs1 < Posifile
6
-
7
- # two specifications, for a multi-line file
8
- lines_where 0..2, "001" do
9
- set_specification( "color"=>13..22,"brand"=>3..12)
10
- set_attr_name :color
11
- end
12
-
13
- lines_where 0..2, "002" do
14
- set_specification("brand"=>3..12)
15
- set_attr_name :brand
16
- end
17
- end
18
-
19
- class MultiLinesWithTwoSpecs2 < Posifile
20
-
21
- # two specifications, for a multi-line file
22
- lines_where 0..2, "001" do
23
- set_specification( "color"=>13..22,"brand"=>3..12)
24
- set_attr_name :brand
25
- end
26
-
27
- lines_where 0..2, "002" do
28
- set_specification("brand"=>3..12)
29
- set_attr_name :brand
30
- end
31
- end
32
-
33
- class MultiLinesWithTwoSpecsWithOneSetAttrNameMissing1 < Posifile
34
- # two specifications, for a multi-line file, but one them doesn't have the set_attr_name, wich is obligatory
35
- lines_where 0..2, "001" do
36
- set_specification( "color"=>13..22,"brand"=>3..12)
37
- set_attr_name :brand
38
- end
39
-
40
- lines_where 0..2, "002" do
41
- set_specification("brand"=>3..12, "color"=>13..22)
42
- end
43
- end
44
-
45
- class MultiLinesWithTwoSpecsWithOneSetAttrNameMissing2 < Posifile
46
- # one specifications, for a multi-line file, but doesn't have the set_attr_name, wich is obligatory
47
- lines_where 0..2, "001" do
48
- set_specification( "color"=>13..22,"brand"=>3..12)
49
- end
50
- end
51
-
52
- class MultiLinesWithOneSpec < Posifile
53
-
54
- # just one specification, for a two-line file, should raise exception on initialize
55
- # but it doesn't do that yet, and dont have tests for this, but seems to work.
56
- lines_where 0..2, "001" do
57
- set_specification("brand"=>3..12)
58
- set_attr_name :brand
59
- end
60
- end
61
-
62
- class MultiLinesWithUppercaseFields < Posifile
63
-
64
- # two specifications, for a multi-line file
65
- lines_where 0..2, "001" do
66
- set_specification( "color"=>13..22,"brand"=>3..12)
67
- set_attr_name :brand
68
- end
69
- end
70
-
71
-
72
-
73
- class NoSpecModel < Posifile
74
- lines_where 0..2, "001" do
75
- end
76
- end
77
-
78
- class TestLinesWhere < Test::Unit::TestCase
79
-
80
- include TestHelpers
81
-
82
- def setup
83
- create_multi_lines_sample
84
- create_multi_lines_sample_with_uppercase
85
- end
86
-
87
- def test_lines_where_two_lines_two_specs_1
88
- # with :color as attr_name
89
- car = MultiLinesWithTwoSpecs1.new("samples/multi_line_sample.txt")
90
-
91
- assert_equal "vectra", car.white['brand']
92
- assert_equal "uno", car.yellow['brand']
93
- assert_equal "white", car.white['color']
94
- assert_equal "yamaha", car.yamaha["brand"]
95
-
96
- end
97
-
98
- def test_lines_where_two_lines_two_specs_2
99
- # with :brand as attr_name
100
- car = MultiLinesWithTwoSpecs2.new("samples/multi_line_sample.txt")
101
-
102
- assert_equal Hash, car.fusca.class
103
- assert_equal "bege",car.fusca['color']
104
- end
105
-
106
- def test_lines_where_with_uppercase_fields
107
- car = MultiLinesWithUppercaseFields.new("samples/multi_line_sample_with_uppercase.txt")
108
-
109
- assert_equal "bege", car.fusca['color']
110
- assert_equal "Yellow", car.uno['color']
111
- end
112
-
113
- def test_lines_where_two_lines_one_spec_yellow
114
- car = MultiLinesWithOneSpec.new("samples/multi_line_sample.txt")
115
- assert_equal "fusca", car.fusca["brand"]
116
- end
117
-
118
- def test_raise_error_attr_name_not_specified
119
- assert_raise(AttrNameNotSpecified) do
120
- car = MultiLinesWithTwoSpecsWithOneSetAttrNameMissing1.new("samples/multi_line_sample.txt")
121
- end
122
- end
123
-
124
- def test_raise_error_attr_name_not_specified2
125
- assert_raise(AttrNameNotSpecified) do
126
- car = MultiLinesWithTwoSpecsWithOneSetAttrNameMissing2.new("samples/multi_line_sample.txt")
127
- end
128
- end
129
-
130
- def test_no_spec_model
131
- assert_raise(FieldsNotSpecified) do
132
- no_spec = NoSpecModel.new("samples/sample.txt")
133
- end
134
- end
135
-
136
- end
137
-
@@ -1,23 +0,0 @@
1
- require 'posifile'
2
- require 'test/unit'
3
- require 'test_helpers'
4
-
5
- class UppercasedSpec < Posifile
6
- end
7
-
8
- class TestsCheckForUppercase < Test::Unit::TestCase
9
-
10
- include TestHelpers
11
-
12
- def setup
13
- end
14
-
15
- def tests_check_for_uppercase_false
16
- assert UppercasedSpec.check_for_uppercase({"Name"=>1..10, "CITY"=>11..20, "country"=>21..32})
17
- end
18
-
19
- def tests_check_for_uppercase_true
20
- assert !UppercasedSpec.check_for_uppercase({"name"=>1..10, "city"=>11..20, "country"=>21..32})
21
- end
22
-
23
- end