posifile 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -3
- data/README.rdoc +48 -5
- data/lib/errors.rb +6 -0
- data/lib/posifile.rb +156 -46
- data/posifile.gemspec +13 -0
- data/tests/Rakefile +1 -1
- data/tests/lines_where/tests_multi_line_files.rb +137 -0
- data/tests/lines_where/tests_one_line_files.rb +35 -0
- data/tests/test_helpers.rb +49 -0
- data/tests/tests_check_condition.rb +31 -0
- data/tests/tests_check_for_uppercase.rb +23 -0
- data/tests/tests_pos_attributes.rb +42 -0
- data/tests/tests_posifile.rb +17 -2
- data/tests/tests_set_spec_with_uppercase.rb +18 -0
- data/tests/validations/tests_validations.rb +54 -0
- metadata +13 -6
- data/tests/models/models.rb +0 -23
- data/tests/tests_validations.rb +0 -46
data/.gitignore
CHANGED
data/README.rdoc
CHANGED
@@ -4,27 +4,70 @@ This library is intended to make it easier to read/write any kind of position fi
|
|
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
6
|
=== Usage
|
7
|
-
|
8
|
-
|
7
|
+
|
8
|
+
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.
|
9
|
+
|
10
|
+
==== One line file
|
11
|
+
This is the case where there is only one line per file, and each file constains one register. You need to tell your model wich position is what, like:
|
12
|
+
# client.rb
|
13
|
+
require 'rubygems'
|
9
14
|
require 'posifile'
|
10
15
|
|
11
16
|
class Client < Posifile
|
12
|
-
set_specification
|
17
|
+
set_specification{
|
13
18
|
"name"="0..10",
|
14
19
|
"city"=>"11..31",
|
15
20
|
"country"=>"32..50"
|
16
21
|
}
|
17
|
-
#you can put here any other method you
|
22
|
+
#you can put here any other method you'd like
|
18
23
|
|
19
24
|
end
|
20
25
|
|
21
|
-
|
26
|
+
And you will get as object attributes every key you specified in the above hash:
|
22
27
|
|
23
28
|
client = Client.new("path/to/file.txt")
|
24
29
|
client.name
|
25
30
|
client.city
|
26
31
|
cliennt.country
|
27
32
|
|
33
|
+
Note that every key in the hash needs to be all downcase, because they'r gonna be method's names.
|
34
|
+
|
35
|
+
==== 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
|
+
|
38
|
+
# car
|
39
|
+
require 'rubygems'
|
40
|
+
require 'posifile'
|
41
|
+
|
42
|
+
class Client < Posifile
|
43
|
+
lines_where 0..2, "001" do # this is some header of the file, and commonly has just one register
|
44
|
+
set_specification( "file_name"=>3..20, "date"=>21..29)
|
45
|
+
set_attr_name :file_name
|
46
|
+
end
|
47
|
+
|
48
|
+
lines_where 0..2, "002" do
|
49
|
+
set_specification( "color"=>25..29,"brand"=>3..16, "model"=>17..24)
|
50
|
+
set_attr_name :model
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
Using lines_where you can have different specifications for different kinds of registers. But in this situation, the attribute's names of the object doesn't follow the same rules as before. The set_attr_name will tell what to use as attribute names. So, supposing the file is something like:
|
55
|
+
|
56
|
+
001first_file.txt 05052011
|
57
|
+
002Wolksvagen Golf red
|
58
|
+
002Renault Vivasix blue
|
59
|
+
002Porsche Boxter black
|
60
|
+
|
61
|
+
We would do:
|
62
|
+
|
63
|
+
client = Client.new("path/to/file.txt")
|
64
|
+
client.boxter['color'] # => 'black'
|
65
|
+
client.boxter['brand'] # =>'Porsche'
|
66
|
+
client.boxter['model'] # =>'Boxter'
|
67
|
+
cliennt.golf['color'] # => 'red'
|
68
|
+
|
69
|
+
Notice that in multi-lin files, the method's names will always be the downcased version of the value found in the given position.
|
70
|
+
|
28
71
|
=== feedback
|
29
72
|
|
30
73
|
Email: marcofognog@gmail.com
|
data/lib/errors.rb
ADDED
data/lib/posifile.rb
CHANGED
@@ -1,62 +1,113 @@
|
|
1
|
+
require 'errors'
|
1
2
|
class Posifile
|
2
|
-
@@
|
3
|
+
@@specifications = {}
|
4
|
+
@@conditions = {}
|
5
|
+
@@attr_names = {}
|
6
|
+
@@pos_attr = {}
|
3
7
|
|
4
8
|
attr_accessor :data_file
|
5
9
|
|
6
|
-
def
|
7
|
-
|
8
|
-
|
10
|
+
def initialize(data_file_name)
|
11
|
+
@data_file = data_file_name
|
12
|
+
|
13
|
+
check_specification_hash
|
14
|
+
file_content.each do |line|
|
15
|
+
if file_content.length == 1
|
16
|
+
build_attributes_from_hash(@@specifications[self.class][0], line, nil)
|
17
|
+
else
|
18
|
+
check_attr_names
|
19
|
+
specification_index(line) do |line_,index|
|
20
|
+
build_attributes_from_hash(@@specifications[self.class][index], line_,@@attr_names[self.class][index])
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
9
24
|
end
|
10
25
|
|
11
|
-
def self.
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
if length == higher_number+1
|
16
|
-
true
|
26
|
+
def self.set_specification(hash)
|
27
|
+
unless check_for_uppercase(hash)
|
28
|
+
@@specifications[self] ||= []
|
29
|
+
@@specifications[self] << hash
|
17
30
|
else
|
18
|
-
|
31
|
+
raise UppercaseFieldsError, "Fields names must be all downcase letters"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.lines_where(range,value,&block)
|
36
|
+
|
37
|
+
@@conditions[self] ||= []
|
38
|
+
@@conditions[self] << {range,value}
|
39
|
+
yield
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.set_attr_name(attr_name)
|
43
|
+
@@attr_names[self] ||= []
|
44
|
+
@@attr_names[self] << attr_name
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.check_for_uppercase(hash)
|
48
|
+
check = false
|
49
|
+
hash.each_key do |key|
|
50
|
+
key.split('').each do |letter|
|
51
|
+
if letter.upcase ==letter
|
52
|
+
check = true
|
53
|
+
end
|
54
|
+
end
|
19
55
|
end
|
56
|
+
|
57
|
+
return check
|
20
58
|
end
|
21
59
|
|
22
60
|
def self.valid_specification?
|
23
|
-
|
24
|
-
|
61
|
+
unless gap_in_specification?(@@specifications[self]) && overlap_in_specification?(@@specifications[self])
|
62
|
+
false
|
63
|
+
else
|
64
|
+
true
|
65
|
+
end
|
25
66
|
end
|
26
67
|
|
27
|
-
def self.overlap_in_specification?
|
68
|
+
def self.overlap_in_specification?(spec_array)
|
28
69
|
num_ar = []
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
70
|
+
spec_array.each_with_index do |spec,index|
|
71
|
+
spec.each_value do |range|
|
72
|
+
range.each do |item|
|
73
|
+
num_ar[index] ||= []
|
74
|
+
num_ar[index][item] ||= 0
|
75
|
+
num_ar[index][item] += 1
|
76
|
+
end
|
33
77
|
end
|
34
78
|
end
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
79
|
+
valid = true
|
80
|
+
num_ar.each do |array|
|
81
|
+
if array.include?(2)
|
82
|
+
valid = false
|
83
|
+
end
|
39
84
|
end
|
85
|
+
valid
|
40
86
|
end
|
41
87
|
|
42
|
-
def self.gap_in_specification?
|
88
|
+
def self.gap_in_specification?(spec_array)
|
43
89
|
num_ar = []
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
90
|
+
spec_array.each_with_index do |spec, index|
|
91
|
+
spec.each_value do |range|
|
92
|
+
range.each do |item|
|
93
|
+
num_ar[index] ||= []
|
94
|
+
num_ar[index][item] ||= 0
|
95
|
+
num_ar[index][item] += 1
|
96
|
+
end
|
48
97
|
end
|
49
98
|
end
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
99
|
+
valid = true
|
100
|
+
num_ar.each do |array|
|
101
|
+
if array.include? nil
|
102
|
+
valid = false
|
103
|
+
end
|
54
104
|
end
|
105
|
+
valid
|
55
106
|
end
|
56
107
|
|
57
108
|
def self.higher
|
58
109
|
higher_number = 0
|
59
|
-
@@
|
110
|
+
@@specifications[self][0].each_value do |range|
|
60
111
|
if range.max > higher_number
|
61
112
|
higher_number = range.max
|
62
113
|
end
|
@@ -64,21 +115,56 @@ class Posifile
|
|
64
115
|
higher_number
|
65
116
|
end
|
66
117
|
|
67
|
-
def
|
68
|
-
|
69
|
-
|
118
|
+
def check_specification_hash
|
119
|
+
if @@specifications[self.class].nil?
|
120
|
+
raise FieldsNotSpecified, "You should call set_specifications in you model, so we can build the object with the corresponding attribuites. Check documentation on how to do so."
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
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
|
+
def specification_index(line)
|
135
|
+
i = 0
|
136
|
+
unless @@conditions[self.class].nil?
|
137
|
+
@@conditions[self.class].each_with_index do |hash, num|
|
138
|
+
if check_condition(hash,line)
|
139
|
+
yield line, num
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
i
|
144
|
+
end
|
145
|
+
|
146
|
+
def check_condition(condition_hash, checked_line)
|
147
|
+
check = false
|
148
|
+
condition_hash.each do |range, value|
|
149
|
+
if checked_line[range] == value
|
150
|
+
check = true
|
151
|
+
end
|
152
|
+
end
|
153
|
+
check
|
70
154
|
end
|
71
155
|
|
72
156
|
def file_content
|
73
157
|
file = File.open(@data_file,"r")
|
74
|
-
file.
|
158
|
+
file.readlines
|
75
159
|
end
|
76
160
|
|
77
|
-
def field_value(field_name)
|
78
|
-
|
79
|
-
|
161
|
+
def field_value(field_name,specification_hash,line)
|
162
|
+
if field_name.class ==Symbol
|
163
|
+
field_name = field_name.to_s
|
164
|
+
end
|
165
|
+
content_ar = line.split('')
|
80
166
|
value_str = ''
|
81
|
-
range =
|
167
|
+
range = specification_hash[field_name]
|
82
168
|
range.each do |n|
|
83
169
|
value_str.concat content_ar[n]
|
84
170
|
end
|
@@ -91,17 +177,41 @@ class Posifile
|
|
91
177
|
ar.join(' ')
|
92
178
|
end
|
93
179
|
|
180
|
+
def pos_attributes
|
181
|
+
@@pos_attr[self.class]
|
182
|
+
end
|
183
|
+
|
184
|
+
def add_method_to_pos_attr(field)
|
185
|
+
@@pos_attr[self.class] ||= []
|
186
|
+
unless @@pos_attr[self.class].include? field
|
187
|
+
@@pos_attr[self.class] << field
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def build_attributes_from_hash(specification_hash,line,attr_name)
|
94
192
|
|
95
|
-
|
96
|
-
|
193
|
+
unless attr_name.nil?
|
194
|
+
values_hash = {}
|
195
|
+
specification_hash.each do |key, value|
|
196
|
+
values_hash[key] = field_value(key, specification_hash, line )
|
197
|
+
end
|
198
|
+
method_name = field_value(attr_name,specification_hash,line).downcase
|
199
|
+
add_method_to_pos_attr(method_name)
|
97
200
|
self.instance_eval "
|
98
|
-
def #{
|
99
|
-
|
201
|
+
def #{method_name}\n
|
202
|
+
#{values_hash.inspect}\n
|
100
203
|
end
|
101
204
|
"
|
205
|
+
else
|
206
|
+
specification_hash.each do |key, not_used|
|
207
|
+
add_method_to_pos_attr(key)
|
208
|
+
self.instance_eval "
|
209
|
+
def #{key}
|
210
|
+
\"#{field_value(key, specification_hash, line)}\"
|
211
|
+
end
|
212
|
+
"
|
213
|
+
end
|
102
214
|
end
|
103
215
|
end
|
104
216
|
|
105
|
-
|
106
|
-
|
107
217
|
end
|
data/posifile.gemspec
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
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.'
|
6
|
+
s.files = `git ls-files`.split("\n")
|
7
|
+
s.homepage = 'http://github.com/marcofognog/posifile'
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Marco Antonio Fogaça Nogueira"]
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.3.7")
|
11
|
+
s.email = ["marcofognog@gmail.com"]
|
12
|
+
s.require_paths = ["lib"]
|
13
|
+
end
|
data/tests/Rakefile
CHANGED
@@ -0,0 +1,137 @@
|
|
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
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'posifile'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'test_helpers'
|
4
|
+
|
5
|
+
class OneLineWithOneSpec < Posifile
|
6
|
+
|
7
|
+
# just one specification, for a one-line file
|
8
|
+
lines_where 0..2, "001" do
|
9
|
+
set_specification("color"=>3..12)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class OneLineWithOneSpecWithUppercase < Posifile
|
14
|
+
end
|
15
|
+
|
16
|
+
class TestLinesWhere < Test::Unit::TestCase
|
17
|
+
include TestHelpers
|
18
|
+
def setup
|
19
|
+
create_one_line_sample
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_lines_where_one_line_one_spec
|
23
|
+
car = OneLineWithOneSpec.new("samples/one_line_sample.txt")
|
24
|
+
assert_equal "yellow", car.color
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_lines_where_one_line_one_spec_with_uppercase
|
28
|
+
assert_raise(UppercaseFieldsError) do
|
29
|
+
OneLineWithOneSpecWithUppercase.lines_where 0..2, "001" do
|
30
|
+
OneLineWithOneSpecWithUppercase.set_specification("Color"=>3..12)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
data/tests/test_helpers.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
module TestHelpers
|
2
|
+
def create_folder
|
3
|
+
Dir.mkdir("samples/") unless File.directory? "samples/"
|
4
|
+
end
|
5
|
+
|
2
6
|
def create_sample_file
|
7
|
+
create_folder
|
3
8
|
content = "jose new york brazil "
|
4
9
|
sample = File.new("samples/sample.txt","wb")
|
5
10
|
sample.puts content
|
@@ -7,6 +12,7 @@ module TestHelpers
|
|
7
12
|
end
|
8
13
|
|
9
14
|
def create_sample_file2
|
15
|
+
create_folder
|
10
16
|
content = "Richard New Orleans USA "
|
11
17
|
sample = File.new("samples/sample2.txt","wb")
|
12
18
|
sample.puts content
|
@@ -14,10 +20,53 @@ module TestHelpers
|
|
14
20
|
end
|
15
21
|
|
16
22
|
def create_sample_invalid
|
23
|
+
create_folder
|
17
24
|
content = "Richard New Orleans USA "
|
18
25
|
sample = File.new("samples/invalid.txt","wb")
|
19
26
|
sample.puts content
|
20
27
|
sample.close
|
21
28
|
end
|
22
29
|
|
30
|
+
def create_one_line_sample
|
31
|
+
create_folder
|
32
|
+
unless File.exist?("samples/one_line_sample.txt")
|
33
|
+
content = "001yellow "
|
34
|
+
sample = File.new("samples/one_line_sample.txt","wb")
|
35
|
+
sample.puts content
|
36
|
+
sample.close
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def create_multi_lines_sample
|
41
|
+
create_folder
|
42
|
+
unless File.exist?("samples/multi_line_sample.txt")
|
43
|
+
content = <<END
|
44
|
+
001uno yellow
|
45
|
+
002yamaha blue
|
46
|
+
001vectra white
|
47
|
+
002harley black
|
48
|
+
001fusca bege
|
49
|
+
END
|
50
|
+
sample = File.new("samples/multi_line_sample.txt","wb")
|
51
|
+
sample.puts content
|
52
|
+
sample.close
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def create_multi_lines_sample_with_uppercase
|
57
|
+
create_folder
|
58
|
+
unless File.exist?("samples/multi_line_sample_with_uppercase.txt")
|
59
|
+
content = <<END
|
60
|
+
001Uno Yellow
|
61
|
+
002yamaha BLUE
|
62
|
+
001Vectra white
|
63
|
+
002harley black
|
64
|
+
001FUSCA bege
|
65
|
+
END
|
66
|
+
sample = File.new("samples/multi_line_sample_with_uppercase.txt","wb")
|
67
|
+
sample.puts content
|
68
|
+
sample.close
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
23
72
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'posifile'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'test_helpers'
|
4
|
+
|
5
|
+
class Person < Posifile
|
6
|
+
lines_where(0..2, "001") do
|
7
|
+
set_specification("color"=>3..9)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class TestCheckCondition < Test::Unit::TestCase
|
12
|
+
|
13
|
+
include TestHelpers
|
14
|
+
|
15
|
+
def setup
|
16
|
+
create_one_line_sample
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_check_condition_true
|
20
|
+
c = Person.new("samples/one_line_sample.txt")
|
21
|
+
|
22
|
+
assert c.check_condition({0..2=>"001"}, "001opa")
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_check_condition_false
|
26
|
+
c = Person.new("samples/one_line_sample.txt")
|
27
|
+
|
28
|
+
assert !c.check_condition({0..2=>"001"}, "003opa")
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,23 @@
|
|
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
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'posifile'
|
2
|
+
require 'test/unit'
|
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
|
11
|
+
|
12
|
+
class MultiLinesWithTwoSpecs3 < Posifile
|
13
|
+
|
14
|
+
# two specifications, for a multi-line file
|
15
|
+
lines_where 0..2, "001" do
|
16
|
+
set_specification( "color"=>13..22,"brand"=>3..12)
|
17
|
+
set_attr_name :brand
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class TestPosAttributes < Test::Unit::TestCase
|
22
|
+
# Here are general tests
|
23
|
+
|
24
|
+
include TestHelpers
|
25
|
+
|
26
|
+
def test_pos_attributes_one_line
|
27
|
+
c = Client.new("samples/sample.txt")
|
28
|
+
assert c.pos_attributes.include? "city"
|
29
|
+
assert c.pos_attributes.include? "name"
|
30
|
+
assert c.pos_attributes.include? "country"
|
31
|
+
assert_equal 3, c.pos_attributes.length
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_pos_attributes_multi_line
|
35
|
+
p = MultiLinesWithTwoSpecs3.new("samples/multi_line_sample.txt")
|
36
|
+
assert p.pos_attributes.include? "fusca"
|
37
|
+
assert p.pos_attributes.include? "uno"
|
38
|
+
assert p.pos_attributes.include? "vectra"
|
39
|
+
assert_equal 3, p.pos_attributes.length
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/tests/tests_posifile.rb
CHANGED
@@ -1,9 +1,19 @@
|
|
1
1
|
require 'posifile'
|
2
2
|
require 'test/unit'
|
3
3
|
require 'test_helpers'
|
4
|
-
|
4
|
+
|
5
|
+
class Client < Posifile
|
6
|
+
set_specification("name"=>0..10,
|
7
|
+
"city"=>11..31,
|
8
|
+
"country"=>32..42
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
class NoSpec < Posifile
|
13
|
+
end
|
5
14
|
|
6
15
|
class TestPosifile < Test::Unit::TestCase
|
16
|
+
# Here are general tests
|
7
17
|
|
8
18
|
include TestHelpers
|
9
19
|
|
@@ -37,12 +47,17 @@ class TestPosifile < Test::Unit::TestCase
|
|
37
47
|
|
38
48
|
def test_field_value
|
39
49
|
c = Client.new("samples/sample.txt")
|
40
|
-
assert_equal "jose", c.field_value("name")
|
50
|
+
assert_equal "jose", c.field_value("name",{"name"=>0..10, "city"=>11..31, "country"=>32..42}, "jose new york brazil ")
|
41
51
|
end
|
42
52
|
|
43
53
|
def test_higher
|
44
54
|
assert_equal 42, Client.higher
|
45
55
|
end
|
46
56
|
|
57
|
+
def test_no_spec_model
|
58
|
+
assert_raise(FieldsNotSpecified) do
|
59
|
+
no_spec = NoSpec.new("samples/sample.txt")
|
60
|
+
end
|
61
|
+
end
|
47
62
|
|
48
63
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'posifile'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'test_helpers'
|
4
|
+
|
5
|
+
class SpecWithUppercase < Posifile
|
6
|
+
end
|
7
|
+
|
8
|
+
class TestPosifile < Test::Unit::TestCase
|
9
|
+
|
10
|
+
include TestHelpers
|
11
|
+
|
12
|
+
def test_set_spec_with_uppercase
|
13
|
+
assert_raise(UppercaseFieldsError) do
|
14
|
+
SpecWithUppercase.set_specification("Name"=>0..10, "CITY"=>11..31,"country"=>32..42)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'posifile'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'test_helpers'
|
4
|
+
|
5
|
+
class WithGap <Posifile
|
6
|
+
set_specification("name"=>0..10, "city"=>11..31, "country"=>36..52)
|
7
|
+
end
|
8
|
+
|
9
|
+
class WithOverlap < Posifile
|
10
|
+
set_specification("name"=>0..8, "address" => 8..20, "job" => 16..30)
|
11
|
+
end
|
12
|
+
|
13
|
+
class BothGapAndOverlap < Posifile
|
14
|
+
set_specification("name"=>0..8, "address" => 10..20, "job" => 16..30)
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
class TestValidations < Test::Unit::TestCase
|
19
|
+
|
20
|
+
include TestHelpers
|
21
|
+
|
22
|
+
def test_valid_specification_gap_false
|
23
|
+
assert !WithGap.valid_specification?
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_valid_specification_true
|
27
|
+
assert Client.valid_specification?
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_valid_specification_overlap_false
|
31
|
+
assert !WithOverlap.valid_specification?
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_valid_specification_both_false
|
35
|
+
assert !BothGapAndOverlap.valid_specification?
|
36
|
+
end
|
37
|
+
|
38
|
+
# specifics -------------
|
39
|
+
def test_overlap_in_specification_false
|
40
|
+
assert !WithOverlap.overlap_in_specification?([{"name"=>0..8, "address" => 8..20, "job" => 16..30}])
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_overlap_in_specification_true
|
44
|
+
assert Client.overlap_in_specification?([{"name"=>0..10, "city"=>11..31,"country"=>32..42}])
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_gap_in_specification_false
|
48
|
+
assert !WithGap.gap_in_specification?([{"name"=>0..10, "city"=>11..31,"country"=>36..52}])
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_gap_in_specification_true
|
52
|
+
assert Client.gap_in_specification?([{"name"=>0..10, "city"=>11..31,"country"=>32..42}])
|
53
|
+
end
|
54
|
+
end
|
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:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "Marco Antonio Foga\xC3\xA7a Nogueira"
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-04-05 00:00:00 -03:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -31,12 +31,19 @@ extra_rdoc_files: []
|
|
31
31
|
files:
|
32
32
|
- .gitignore
|
33
33
|
- README.rdoc
|
34
|
+
- lib/errors.rb
|
34
35
|
- lib/posifile.rb
|
36
|
+
- posifile.gemspec
|
35
37
|
- tests/Rakefile
|
36
|
-
- tests/
|
38
|
+
- tests/lines_where/tests_multi_line_files.rb
|
39
|
+
- tests/lines_where/tests_one_line_files.rb
|
37
40
|
- tests/test_helpers.rb
|
41
|
+
- tests/tests_check_condition.rb
|
42
|
+
- tests/tests_check_for_uppercase.rb
|
43
|
+
- tests/tests_pos_attributes.rb
|
38
44
|
- tests/tests_posifile.rb
|
39
|
-
- tests/
|
45
|
+
- tests/tests_set_spec_with_uppercase.rb
|
46
|
+
- tests/validations/tests_validations.rb
|
40
47
|
has_rdoc: true
|
41
48
|
homepage: http://github.com/marcofognog/posifile
|
42
49
|
licenses: []
|
data/tests/models/models.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'posifile'
|
2
|
-
|
3
|
-
class Client < Posifile
|
4
|
-
set_specification("name"=>0..10,
|
5
|
-
"city"=>11..31,
|
6
|
-
"country"=>32..42
|
7
|
-
)
|
8
|
-
end
|
9
|
-
|
10
|
-
class WithGap <Posifile
|
11
|
-
set_specification("name"=>0..10,
|
12
|
-
"city"=>11..31,
|
13
|
-
"country"=>36..52
|
14
|
-
)
|
15
|
-
end
|
16
|
-
|
17
|
-
class WithOverlap < Posifile
|
18
|
-
set_specification("name"=>0..8, "address" => 8..20, "job" => 16..30)
|
19
|
-
end
|
20
|
-
|
21
|
-
class BothGapAndOverlap < Posifile
|
22
|
-
set_specification("name"=>0..8, "address" => 10..20, "job" => 16..30)
|
23
|
-
end
|
data/tests/tests_validations.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
require 'posifile'
|
2
|
-
require 'test/unit'
|
3
|
-
require 'test_helpers'
|
4
|
-
require 'models/models'
|
5
|
-
|
6
|
-
class TestValidations < Test::Unit::TestCase
|
7
|
-
|
8
|
-
include TestHelpers
|
9
|
-
|
10
|
-
def test_valid_false
|
11
|
-
assert !Client.valid?("samples/invalid.txt")
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_valid_true
|
15
|
-
assert Client.valid?("samples/sample.txt")
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_valid_specification_false
|
19
|
-
assert !WithGap.valid_specification?
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_valid_specification_true
|
23
|
-
assert Client.valid_specification?
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_valid_specification_false
|
27
|
-
assert !BothGapAndOverlap.valid_specification?
|
28
|
-
end
|
29
|
-
|
30
|
-
#especifics -------------
|
31
|
-
def test_overlap_in_specification_false
|
32
|
-
assert !WithOverlap.overlap_in_specification?
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_overlap_in_specification_true
|
36
|
-
assert Client.overlap_in_specification?
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_gap_in_specification_false
|
40
|
-
assert !WithGap.gap_in_specification?
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_gap_in_specification_true
|
44
|
-
assert Client.gap_in_specification?
|
45
|
-
end
|
46
|
-
end
|