posifile 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/posifile.rb +236 -254
- data/posifile.gemspec +1 -1
- data/tests/tests_check_condition.rb +2 -2
- data/tests/tests_inheritances.rb +1 -0
- data/tests/tests_posifile.rb +0 -5
- metadata +19 -43
data/lib/posifile.rb
CHANGED
@@ -1,259 +1,241 @@
|
|
1
1
|
require 'errors'
|
2
2
|
class Posifile
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
add_method_to_pos_attr(method_name)
|
242
|
-
self.instance_eval "
|
243
|
-
def #{method_name}\n
|
244
|
-
#{values_hash.inspect}\n
|
245
|
-
end
|
246
|
-
"
|
247
|
-
else
|
248
|
-
specification_hash.each do |key, not_used|
|
249
|
-
add_method_to_pos_attr(key)
|
250
|
-
self.instance_eval "
|
251
|
-
def #{key}
|
252
|
-
\"#{field_value(key, specification_hash, line)}\"
|
253
|
-
end
|
254
|
-
"
|
255
|
-
end
|
256
|
-
end
|
257
|
-
end
|
4
|
+
INVALID_CHARS = " !@#$\%*()-\&\'\"<>.,;:[]{}\'\`^+\\/"
|
5
|
+
|
6
|
+
@@specifications = {}
|
7
|
+
@@conditions = {}
|
8
|
+
@@attr_names = {}
|
9
|
+
@@pos_attr = {}
|
10
|
+
|
11
|
+
attr_accessor :data_file, :raw_content
|
12
|
+
|
13
|
+
def initialize(data_file_name)
|
14
|
+
@data_file = data_file_name
|
15
|
+
|
16
|
+
check_specification_hash
|
17
|
+
file_content.each do |line|
|
18
|
+
if file_content.length == 1
|
19
|
+
build_attributes_from_hash(@@specifications[class_name][0], line, nil)
|
20
|
+
else
|
21
|
+
specification_index(line) do |line_, index|
|
22
|
+
build_attributes_from_hash(@@specifications[class_name][index], line_, @@attr_names[class_name][index])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.set_specification(hash)
|
29
|
+
if valid_names?(hash)
|
30
|
+
@@specifications[class_name] ||= []
|
31
|
+
@@specifications[class_name] << hash
|
32
|
+
else
|
33
|
+
raise InvalidFieldName, "Fields names contain invalid characteres for method names."
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.lines_where(range,value, &block)
|
38
|
+
@@attr_names[class_name] ||= []
|
39
|
+
length_before = @@attr_names[class_name].length
|
40
|
+
@@conditions[class_name] ||= []
|
41
|
+
@@conditions[class_name] << { range => value }
|
42
|
+
yield
|
43
|
+
length_after = @@attr_names[class_name].length
|
44
|
+
if length_before == length_after
|
45
|
+
@@attr_names[class_name] << nil
|
46
|
+
end
|
47
|
+
section_code_hash = { "section_code" => range }
|
48
|
+
if @@specifications[class_name]
|
49
|
+
@@specifications[class_name].last.merge!(section_code_hash)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.set_attr_name(attr_name)
|
54
|
+
@@attr_names[class_name] ||= []
|
55
|
+
@@attr_names[class_name] << attr_name
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.valid_names?(hash)
|
59
|
+
check = true
|
60
|
+
hash.each_key do |key|
|
61
|
+
key.split('').each do |letter|
|
62
|
+
unless letter.downcase == letter
|
63
|
+
check = false
|
64
|
+
end
|
65
|
+
if INVALID_CHARS.split("").include?(letter)
|
66
|
+
check = false
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
check
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.valid_specification?
|
75
|
+
not gap_in_specification?(@@specifications[class_name]) || overlap_in_specification?(@@specifications[class_name])
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.overlap_in_specification?(spec_array)
|
79
|
+
num_ar = []
|
80
|
+
spec_array.each_with_index do |spec, index|
|
81
|
+
spec.each_value do |range|
|
82
|
+
range.each do |item|
|
83
|
+
num_ar[index] ||= []
|
84
|
+
num_ar[index][item] ||= 0
|
85
|
+
num_ar[index][item] += 1
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
valid = false
|
90
|
+
num_ar.each do |array|
|
91
|
+
if array.include?(2)
|
92
|
+
valid = true
|
93
|
+
end
|
94
|
+
end
|
95
|
+
valid
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.gap_in_specification?(spec_array)
|
99
|
+
num_ar = []
|
100
|
+
spec_array.each_with_index do |spec, index|
|
101
|
+
spec.each_value do |range|
|
102
|
+
range.each do |item|
|
103
|
+
num_ar[index] ||= []
|
104
|
+
num_ar[index][item] ||= 0
|
105
|
+
num_ar[index][item] += 1
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
valid = false
|
110
|
+
num_ar.each do |array|
|
111
|
+
if array.include? nil
|
112
|
+
valid = true
|
113
|
+
end
|
114
|
+
end
|
115
|
+
valid
|
116
|
+
end
|
117
|
+
|
118
|
+
def self.higher
|
119
|
+
higher_number = 0
|
120
|
+
@@specifications[class_name][0].each_value do |range|
|
121
|
+
if range.max > higher_number
|
122
|
+
higher_number = range.max
|
123
|
+
end
|
124
|
+
end
|
125
|
+
higher_number
|
126
|
+
end
|
127
|
+
|
128
|
+
def self.class_name
|
129
|
+
name = self
|
130
|
+
while name.superclass != Posifile
|
131
|
+
name = name.superclass
|
132
|
+
end
|
133
|
+
|
134
|
+
name
|
135
|
+
end
|
136
|
+
|
137
|
+
def class_name
|
138
|
+
name = self.class
|
139
|
+
while name.superclass != Posifile
|
140
|
+
name = name.superclass
|
141
|
+
end
|
142
|
+
|
143
|
+
name
|
144
|
+
end
|
145
|
+
|
146
|
+
def check_specification_hash
|
147
|
+
if @@specifications[class_name].nil?
|
148
|
+
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."
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def specification_index(line)
|
153
|
+
i = 0
|
154
|
+
if @@conditions[class_name]
|
155
|
+
@@conditions[class_name].each_with_index do |hash, num|
|
156
|
+
if check_condition(hash,line)
|
157
|
+
yield line, num
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
i
|
162
|
+
end
|
163
|
+
|
164
|
+
# Checks if a given line matches the line_where condition declare in the class
|
165
|
+
def check_condition(condition_hash, line)
|
166
|
+
line[condition_hash.keys.first] == condition_hash.values.first
|
167
|
+
end
|
168
|
+
|
169
|
+
def file_content
|
170
|
+
if raw_content.nil?
|
171
|
+
file = File.open(@data_file, "r")
|
172
|
+
@raw_content = file.readlines
|
173
|
+
end
|
174
|
+
@raw_content
|
175
|
+
end
|
176
|
+
|
177
|
+
# Get the value of a specifc position declared in the specification hash
|
178
|
+
def field_value(field_name, specification_hash, line)
|
179
|
+
field_name = field_name.to_s
|
180
|
+
content_ar = line.split('')
|
181
|
+
value_str = ''
|
182
|
+
if !specification_hash.keys.include? field_name
|
183
|
+
raise InvalidAttrName, "The specified attr name was not found in the specification hash."
|
184
|
+
else
|
185
|
+
range = specification_hash[field_name]
|
186
|
+
range.each do |n|
|
187
|
+
value_str.concat content_ar[n]
|
188
|
+
end
|
189
|
+
value_str.strip
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def pos_attributes
|
194
|
+
@@pos_attr[class_name]
|
195
|
+
end
|
196
|
+
|
197
|
+
def add_method_to_pos_attr(field)
|
198
|
+
@@pos_attr[class_name] ||= []
|
199
|
+
unless @@pos_attr[class_name].include? field
|
200
|
+
@@pos_attr[class_name] << field
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
# Once the name is gonna be used as method name,
|
205
|
+
# this changes the name when it contains invalid characters.
|
206
|
+
def change_name(original)
|
207
|
+
original.gsub!(' ','_')
|
208
|
+
INVALID_CHARS.split('').each do |char|
|
209
|
+
original.gsub!(char,'')
|
210
|
+
end
|
211
|
+
original.downcase
|
212
|
+
end
|
213
|
+
|
214
|
+
def build_attributes_from_hash(specification_hash, line, attr_name)
|
215
|
+
if attr_name
|
216
|
+
values_hash = {}
|
217
|
+
specification_hash.keys.map do |key|
|
218
|
+
values_hash[key] = field_value(key, specification_hash, line )
|
219
|
+
end
|
220
|
+
|
221
|
+
method_name = change_name(field_value(attr_name, specification_hash, line))
|
222
|
+
|
223
|
+
add_method_to_pos_attr(method_name)
|
224
|
+
self.instance_eval "
|
225
|
+
def #{method_name}\n
|
226
|
+
#{values_hash.inspect}\n
|
227
|
+
end
|
228
|
+
"
|
229
|
+
else
|
230
|
+
specification_hash.each_key do |key|
|
231
|
+
add_method_to_pos_attr(key)
|
232
|
+
self.instance_eval "
|
233
|
+
def #{key}
|
234
|
+
\"#{field_value(key, specification_hash, line)}\"
|
235
|
+
end
|
236
|
+
"
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
258
240
|
|
259
241
|
end
|
data/posifile.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'posifile'
|
3
|
-
s.version = '0.2.
|
3
|
+
s.version = '0.2.4'
|
4
4
|
s.summary = 'Ruby library to make it easier to read position files. '
|
5
5
|
s.description = 'Ruby library to make it easier to read position files.'
|
6
6
|
s.files = `git ls-files`.split("\n")
|
@@ -19,13 +19,13 @@ class TestCheckCondition < Test::Unit::TestCase
|
|
19
19
|
def test_check_condition_true
|
20
20
|
c = Person.new("samples/one_line_sample.txt")
|
21
21
|
|
22
|
-
assert c.check_condition({0..2=>"001"}, "001opa")
|
22
|
+
assert c.check_condition({ 0..2 => "001" }, "001opa")
|
23
23
|
end
|
24
24
|
|
25
25
|
def test_check_condition_false
|
26
26
|
c = Person.new("samples/one_line_sample.txt")
|
27
27
|
|
28
|
-
assert !c.check_condition({0..2=>"001"}, "003opa")
|
28
|
+
assert !c.check_condition({ 0..2 => "001" }, "003opa")
|
29
29
|
end
|
30
30
|
|
31
31
|
end
|
data/tests/tests_inheritances.rb
CHANGED
data/tests/tests_posifile.rb
CHANGED
@@ -34,11 +34,6 @@ class TestPosifile < Test::Unit::TestCase
|
|
34
34
|
assert_equal "New Orleans", client2.city
|
35
35
|
end
|
36
36
|
|
37
|
-
def test_value_parse
|
38
|
-
c = Client.new("samples/sample.txt")
|
39
|
-
assert_equal "esta frase precisa estar completa !", c.value_parse("esta frase precisa estar completa ! ")
|
40
|
-
end
|
41
|
-
|
42
37
|
def test_field_value
|
43
38
|
c = Client.new("samples/sample.txt")
|
44
39
|
assert_equal "jose", c.field_value("name",{"name"=>0..10, "city"=>11..31, "country"=>32..42}, "jose new york brazil ")
|
metadata
CHANGED
@@ -1,34 +1,23 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: posifile
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.4
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 2
|
9
|
-
- 3
|
10
|
-
version: 0.2.3
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
13
|
-
-
|
7
|
+
authors:
|
8
|
+
- Marco Antonio Fogaça Nogueira
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
date: 2011-04-09 00:00:00 -03:00
|
19
|
-
default_executable:
|
12
|
+
date: 2012-02-25 00:00:00.000000000Z
|
20
13
|
dependencies: []
|
21
|
-
|
22
14
|
description: Ruby library to make it easier to read position files.
|
23
|
-
email:
|
15
|
+
email:
|
24
16
|
- marcofognog@gmail.com
|
25
17
|
executables: []
|
26
|
-
|
27
18
|
extensions: []
|
28
|
-
|
29
19
|
extra_rdoc_files: []
|
30
|
-
|
31
|
-
files:
|
20
|
+
files:
|
32
21
|
- .gitignore
|
33
22
|
- README.rdoc
|
34
23
|
- lib/errors.rb
|
@@ -51,41 +40,28 @@ files:
|
|
51
40
|
- tests/tests_valid_names.rb
|
52
41
|
- tests/validations/tests_lines_where.rb
|
53
42
|
- tests/validations/tests_validations.rb
|
54
|
-
has_rdoc: true
|
55
43
|
homepage: http://github.com/marcofognog/posifile
|
56
44
|
licenses: []
|
57
|
-
|
58
45
|
post_install_message:
|
59
46
|
rdoc_options: []
|
60
|
-
|
61
|
-
require_paths:
|
47
|
+
require_paths:
|
62
48
|
- lib
|
63
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
50
|
none: false
|
65
|
-
requirements:
|
66
|
-
- -
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
|
69
|
-
|
70
|
-
- 0
|
71
|
-
version: "0"
|
72
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
56
|
none: false
|
74
|
-
requirements:
|
75
|
-
- -
|
76
|
-
- !ruby/object:Gem::Version
|
77
|
-
hash: 21
|
78
|
-
segments:
|
79
|
-
- 1
|
80
|
-
- 3
|
81
|
-
- 7
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
82
60
|
version: 1.3.7
|
83
61
|
requirements: []
|
84
|
-
|
85
62
|
rubyforge_project:
|
86
|
-
rubygems_version: 1.6
|
63
|
+
rubygems_version: 1.8.6
|
87
64
|
signing_key:
|
88
65
|
specification_version: 3
|
89
66
|
summary: Ruby library to make it easier to read position files.
|
90
67
|
test_files: []
|
91
|
-
|