posifile 0.2.2 → 0.2.3
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/lib/posifile.rb +43 -24
- data/posifile.gemspec +1 -1
- data/tests/tests_inheritances.rb +27 -0
- data/tests/validations/tests_validations.rb +12 -0
- metadata +4 -3
data/lib/posifile.rb
CHANGED
@@ -16,44 +16,44 @@ class Posifile
|
|
16
16
|
check_specification_hash
|
17
17
|
file_content.each do |line|
|
18
18
|
if file_content.length == 1
|
19
|
-
build_attributes_from_hash(@@specifications[
|
19
|
+
build_attributes_from_hash(@@specifications[class_name][0], line, nil)
|
20
20
|
else
|
21
21
|
specification_index(line) do |line_,index|
|
22
|
-
build_attributes_from_hash(@@specifications[
|
22
|
+
build_attributes_from_hash(@@specifications[class_name][index], line_,@@attr_names[class_name][index])
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
def self.set_specification(hash)
|
29
|
-
@@attr_names[
|
29
|
+
@@attr_names[class_name] ||= []
|
30
30
|
if valid_names?(hash)
|
31
|
-
@@specifications[
|
32
|
-
@@specifications[
|
31
|
+
@@specifications[class_name] ||= []
|
32
|
+
@@specifications[class_name] << hash
|
33
33
|
else
|
34
34
|
raise InvalidFieldName, "Fields names contain invalid characteres for method names."
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
38
|
def self.lines_where(range,value,&block)
|
39
|
-
@@attr_names[
|
40
|
-
length_before = @@attr_names[
|
41
|
-
@@conditions[
|
42
|
-
@@conditions[
|
39
|
+
@@attr_names[class_name] ||= []
|
40
|
+
length_before = @@attr_names[class_name].length
|
41
|
+
@@conditions[class_name] ||= []
|
42
|
+
@@conditions[class_name] << {range,value}
|
43
43
|
yield
|
44
|
-
length_after = @@attr_names[
|
44
|
+
length_after = @@attr_names[class_name].length
|
45
45
|
if length_before == length_after
|
46
|
-
@@attr_names[
|
46
|
+
@@attr_names[class_name] << nil
|
47
47
|
end
|
48
48
|
section_code_hash = {"section_code"=>range}
|
49
|
-
if @@specifications[
|
50
|
-
@@specifications[
|
49
|
+
if @@specifications[class_name]
|
50
|
+
@@specifications[class_name].last.merge!(section_code_hash)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
54
|
def self.set_attr_name(attr_name)
|
55
|
-
@@attr_names[
|
56
|
-
@@attr_names[
|
55
|
+
@@attr_names[class_name] ||= []
|
56
|
+
@@attr_names[class_name] << attr_name
|
57
57
|
end
|
58
58
|
|
59
59
|
def self.valid_names?(hash)
|
@@ -73,7 +73,7 @@ class Posifile
|
|
73
73
|
end
|
74
74
|
|
75
75
|
def self.valid_specification?
|
76
|
-
if gap_in_specification?(@@specifications[
|
76
|
+
if gap_in_specification?(@@specifications[class_name]) || overlap_in_specification?(@@specifications[class_name])
|
77
77
|
false
|
78
78
|
else
|
79
79
|
true
|
@@ -122,7 +122,7 @@ class Posifile
|
|
122
122
|
|
123
123
|
def self.higher
|
124
124
|
higher_number = 0
|
125
|
-
@@specifications[
|
125
|
+
@@specifications[class_name][0].each_value do |range|
|
126
126
|
if range.max > higher_number
|
127
127
|
higher_number = range.max
|
128
128
|
end
|
@@ -130,16 +130,35 @@ class Posifile
|
|
130
130
|
higher_number
|
131
131
|
end
|
132
132
|
|
133
|
+
def self.class_name
|
134
|
+
name = self
|
135
|
+
while name.superclass != Posifile
|
136
|
+
name = name.superclass
|
137
|
+
end
|
138
|
+
|
139
|
+
name
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
def class_name
|
144
|
+
name = self.class
|
145
|
+
while name.superclass != Posifile
|
146
|
+
name = name.superclass
|
147
|
+
end
|
148
|
+
|
149
|
+
name
|
150
|
+
end
|
151
|
+
|
133
152
|
def check_specification_hash
|
134
|
-
if @@specifications[
|
153
|
+
if @@specifications[class_name].nil?
|
135
154
|
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."
|
136
155
|
end
|
137
156
|
end
|
138
157
|
|
139
158
|
def specification_index(line)
|
140
159
|
i = 0
|
141
|
-
unless @@conditions[
|
142
|
-
@@conditions[
|
160
|
+
unless @@conditions[class_name].nil?
|
161
|
+
@@conditions[class_name].each_with_index do |hash, num|
|
143
162
|
if check_condition(hash,line)
|
144
163
|
yield line, num
|
145
164
|
end
|
@@ -190,13 +209,13 @@ class Posifile
|
|
190
209
|
end
|
191
210
|
|
192
211
|
def pos_attributes
|
193
|
-
@@pos_attr[
|
212
|
+
@@pos_attr[class_name]
|
194
213
|
end
|
195
214
|
|
196
215
|
def add_method_to_pos_attr(field)
|
197
|
-
@@pos_attr[
|
198
|
-
unless @@pos_attr[
|
199
|
-
@@pos_attr[
|
216
|
+
@@pos_attr[class_name] ||= []
|
217
|
+
unless @@pos_attr[class_name].include? field
|
218
|
+
@@pos_attr[class_name] << field
|
200
219
|
end
|
201
220
|
end
|
202
221
|
|
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.3'
|
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")
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'posifile'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'test_helpers'
|
4
|
+
require 'client'
|
5
|
+
|
6
|
+
class MyClass < Client
|
7
|
+
end
|
8
|
+
|
9
|
+
class OtherClass < MyClass
|
10
|
+
end
|
11
|
+
|
12
|
+
class TestInheritance < Test::Unit::TestCase
|
13
|
+
# Here we test how deep in subclasses the library is gonna work (beacase there are self and self.class references in the code)
|
14
|
+
|
15
|
+
include TestHelpers
|
16
|
+
|
17
|
+
def test_inheritance_one
|
18
|
+
car = MyClass.new("samples/sample.txt")
|
19
|
+
assert_equal "jose", car.name
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_inheritance_one
|
23
|
+
car = OtherClass.new("samples/sample.txt")
|
24
|
+
assert_equal "jose", car.name
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -15,6 +15,10 @@ class BothGapAndOverlap < Posifile
|
|
15
15
|
set_specification("name"=>0..8, "address" => 10..20, "job" => 16..30)
|
16
16
|
end
|
17
17
|
|
18
|
+
class ValidClass < Posifile
|
19
|
+
set_specification("name"=>0..8, "address" => 10..20, "job" => 16..30, "number"=>31..31)
|
20
|
+
end
|
21
|
+
|
18
22
|
class TestValidations < Test::Unit::TestCase
|
19
23
|
|
20
24
|
include TestHelpers
|
@@ -51,4 +55,12 @@ class TestValidations < Test::Unit::TestCase
|
|
51
55
|
def test_gap_in_specification_true
|
52
56
|
assert !Client.gap_in_specification?([{"name"=>0..10, "city"=>11..31,"country"=>32..42}])
|
53
57
|
end
|
58
|
+
|
59
|
+
def test_valid_class1
|
60
|
+
assert !ValidClass.gap_in_specification?([{"name"=>0..10, "address" => 11..20, "job" => 21..30,"number"=>31..31}])
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_valid_class2
|
64
|
+
assert !ValidClass.gap_in_specification?([{"name"=>0..10, "address" => 11..20, "job" => 21..30,"number"=>31..31}])
|
65
|
+
end
|
54
66
|
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: 17
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 3
|
10
|
+
version: 0.2.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "Marco Antonio Foga\xC3\xA7a Nogueira"
|
@@ -44,6 +44,7 @@ files:
|
|
44
44
|
- tests/test_helpers.rb
|
45
45
|
- tests/tests_change_name.rb
|
46
46
|
- tests/tests_check_condition.rb
|
47
|
+
- tests/tests_inheritances.rb
|
47
48
|
- tests/tests_pos_attributes.rb
|
48
49
|
- tests/tests_posifile.rb
|
49
50
|
- tests/tests_set_spec_with_uppercase.rb
|