posifile 0.2.3 → 0.2.4

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 CHANGED
@@ -1,259 +1,241 @@
1
1
  require 'errors'
2
2
  class Posifile
3
3
 
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
- @@attr_names[class_name] ||= []
30
- if valid_names?(hash)
31
- @@specifications[class_name] ||= []
32
- @@specifications[class_name] << hash
33
- else
34
- raise InvalidFieldName, "Fields names contain invalid characteres for method names."
35
- end
36
- end
37
-
38
- def self.lines_where(range,value,&block)
39
- @@attr_names[class_name] ||= []
40
- length_before = @@attr_names[class_name].length
41
- @@conditions[class_name] ||= []
42
- @@conditions[class_name] << {range,value}
43
- yield
44
- length_after = @@attr_names[class_name].length
45
- if length_before == length_after
46
- @@attr_names[class_name] << nil
47
- end
48
- section_code_hash = {"section_code"=>range}
49
- if @@specifications[class_name]
50
- @@specifications[class_name].last.merge!(section_code_hash)
51
- end
52
- end
53
-
54
- def self.set_attr_name(attr_name)
55
- @@attr_names[class_name] ||= []
56
- @@attr_names[class_name] << attr_name
57
- end
58
-
59
- def self.valid_names?(hash)
60
- check = true
61
- hash.each_key do |key|
62
- key.split('').each do |letter|
63
- unless letter.downcase == letter
64
- check = false
65
- end
66
- if INVALID_CHARS.split("").include?(letter)
67
- check = false
68
- end
69
- end
70
- end
71
-
72
- return check
73
- end
74
-
75
- def self.valid_specification?
76
- if gap_in_specification?(@@specifications[class_name]) || overlap_in_specification?(@@specifications[class_name])
77
- false
78
- else
79
- true
80
- end
81
- end
82
-
83
- def self.overlap_in_specification?(spec_array)
84
- num_ar = []
85
- spec_array.each_with_index do |spec,index|
86
- spec.each_value do |range|
87
- range.each do |item|
88
- num_ar[index] ||= []
89
- num_ar[index][item] ||= 0
90
- num_ar[index][item] += 1
91
- end
92
- end
93
- end
94
- valid = false
95
- num_ar.each do |array|
96
- if array.include?(2)
97
- valid = true
98
- end
99
- end
100
- valid
101
- end
102
-
103
- def self.gap_in_specification?(spec_array)
104
- num_ar = []
105
- spec_array.each_with_index do |spec, index|
106
- spec.each_value do |range|
107
- range.each do |item|
108
- num_ar[index] ||= []
109
- num_ar[index][item] ||= 0
110
- num_ar[index][item] += 1
111
- end
112
- end
113
- end
114
- valid = false
115
- num_ar.each do |array|
116
- if array.include? nil
117
- valid = true
118
- end
119
- end
120
- valid
121
- end
122
-
123
- def self.higher
124
- higher_number = 0
125
- @@specifications[class_name][0].each_value do |range|
126
- if range.max > higher_number
127
- higher_number = range.max
128
- end
129
- end
130
- higher_number
131
- end
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
-
152
- def check_specification_hash
153
- if @@specifications[class_name].nil?
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."
155
- end
156
- end
157
-
158
- def specification_index(line)
159
- i = 0
160
- unless @@conditions[class_name].nil?
161
- @@conditions[class_name].each_with_index do |hash, num|
162
- if check_condition(hash,line)
163
- yield line, num
164
- end
165
- end
166
- end
167
- i
168
- end
169
-
170
- def check_condition(condition_hash, checked_line)
171
- check = false
172
- condition_hash.each do |range, value|
173
- if checked_line[range] == value
174
- check = true
175
- end
176
- end
177
- check
178
- end
179
-
180
- def file_content
181
- if raw_content.nil?
182
- file = File.open(@data_file,"r")
183
- @raw_content = file.readlines
184
- end
185
- @raw_content
186
- end
187
-
188
- def field_value(field_name,specification_hash,line)
189
- if field_name.class == Symbol
190
- field_name = field_name.to_s
191
- end
192
- content_ar = line.split('')
193
- value_str = ''
194
- if !specification_hash.keys.include? field_name
195
- raise InvalidAttrName, "The specified attr name was not found in the specification hash."
196
- else
197
- range = specification_hash[field_name]
198
- range.each do |n|
199
- value_str.concat content_ar[n]
200
- end
201
- value_parse value_str
202
- end
203
- end
204
-
205
- # get the value ignoring white spaces in the end of the string.
206
- def value_parse(value_string)
207
- ar = value_string.split(' ')
208
- ar.join(' ')
209
- end
210
-
211
- def pos_attributes
212
- @@pos_attr[class_name]
213
- end
214
-
215
- def add_method_to_pos_attr(field)
216
- @@pos_attr[class_name] ||= []
217
- unless @@pos_attr[class_name].include? field
218
- @@pos_attr[class_name] << field
219
- end
220
- end
221
-
222
- # Once the name is gonna be used as method name,
223
- # this changes the name when it contains invalid characters.
224
- def change_name(original)
225
- original.gsub!(' ','_')
226
- INVALID_CHARS.split('').each do |char|
227
- original.gsub!(char,'')
228
- end
229
- original.downcase
230
- end
231
-
232
- def build_attributes_from_hash(specification_hash,line,attr_name)
233
- unless attr_name.nil?
234
- values_hash = {}
235
- specification_hash.each do |key, value|
236
- values_hash[key] = field_value(key, specification_hash, line )
237
- end
238
-
239
- method_name = change_name(field_value(attr_name,specification_hash,line))
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'
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
@@ -1,3 +1,4 @@
1
+ $LOAD_PATH << "."
1
2
  require 'posifile'
2
3
  require 'test/unit'
3
4
  require 'test_helpers'
@@ -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
- hash: 17
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
- - "Marco Antonio Foga\xC3\xA7a Nogueira"
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
- hash: 3
69
- segments:
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.1
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
-