posifile 0.1.0
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/.gitignore +4 -0
- data/README.rdoc +32 -0
- data/lib/posifile.rb +107 -0
- data/tests/Rakefile +9 -0
- data/tests/models/models.rb +23 -0
- data/tests/test_helpers.rb +23 -0
- data/tests/tests_posifile.rb +48 -0
- data/tests/tests_validations.rb +46 -0
- metadata +77 -0
data/.gitignore
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
== Posifile
|
2
|
+
|
3
|
+
This library is intended to make it easier to read/write any kind of position file.
|
4
|
+
All you have to do is write in the model how each position will be associated to each objects attributes.
|
5
|
+
|
6
|
+
=== Usage
|
7
|
+
Example of usage:
|
8
|
+
#client
|
9
|
+
require 'posifile'
|
10
|
+
|
11
|
+
class Client < Posifile
|
12
|
+
set_specification {
|
13
|
+
"name"="0..10",
|
14
|
+
"city"=>"11..31",
|
15
|
+
"country"=>"32..50"
|
16
|
+
}
|
17
|
+
#you can put here any other method you 'd like
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
Client.valid?("path_to/other_file.txt") #check
|
22
|
+
|
23
|
+
client = Client.new("path/to/file.txt")
|
24
|
+
client.name
|
25
|
+
client.city
|
26
|
+
cliennt.country
|
27
|
+
|
28
|
+
=== feedback
|
29
|
+
|
30
|
+
Email: marcofognog@gmail.com
|
31
|
+
Any ideas on how we can make this better will be appreciated.
|
32
|
+
|
data/lib/posifile.rb
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
class Posifile
|
2
|
+
@@specification = {}
|
3
|
+
|
4
|
+
attr_accessor :data_file
|
5
|
+
|
6
|
+
def self.set_specification(hash)
|
7
|
+
@@specification[self] ||= {}
|
8
|
+
@@specification[self] = hash
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.valid?(file_name)
|
12
|
+
file = File.open(file_name, "r")
|
13
|
+
length = file.readline.length
|
14
|
+
higher_number = higher
|
15
|
+
if length == higher_number+1
|
16
|
+
true
|
17
|
+
else
|
18
|
+
false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.valid_specification?
|
23
|
+
is_valid = gap_in_specification?
|
24
|
+
# add mor validations here
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.overlap_in_specification?
|
28
|
+
num_ar = []
|
29
|
+
@@specification[self].each_value do |range|
|
30
|
+
range.each do |item|
|
31
|
+
num_ar[item] ||= 0
|
32
|
+
num_ar[item] += 1
|
33
|
+
end
|
34
|
+
end
|
35
|
+
if num_ar.include?(2)
|
36
|
+
return false
|
37
|
+
else
|
38
|
+
return true
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.gap_in_specification?
|
43
|
+
num_ar = []
|
44
|
+
@@specification[self].each_value do |range|
|
45
|
+
range.each do |item|
|
46
|
+
num_ar[item] ||= 0
|
47
|
+
num_ar[item] += 1
|
48
|
+
end
|
49
|
+
end
|
50
|
+
if num_ar.include? nil
|
51
|
+
return false
|
52
|
+
else
|
53
|
+
return true
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.higher
|
58
|
+
higher_number = 0
|
59
|
+
@@specification[self].each_value do |range|
|
60
|
+
if range.max > higher_number
|
61
|
+
higher_number = range.max
|
62
|
+
end
|
63
|
+
end
|
64
|
+
higher_number
|
65
|
+
end
|
66
|
+
|
67
|
+
def initialize(data_file_name)
|
68
|
+
@data_file = data_file_name
|
69
|
+
build_attriubutes_from_hash
|
70
|
+
end
|
71
|
+
|
72
|
+
def file_content
|
73
|
+
file = File.open(@data_file,"r")
|
74
|
+
file.readline
|
75
|
+
end
|
76
|
+
|
77
|
+
def field_value(field_name)
|
78
|
+
content = file_content
|
79
|
+
content_ar = content.split('')
|
80
|
+
value_str = ''
|
81
|
+
range = @@specification[self.class][field_name]
|
82
|
+
range.each do |n|
|
83
|
+
value_str.concat content_ar[n]
|
84
|
+
end
|
85
|
+
value_parse value_str
|
86
|
+
end
|
87
|
+
|
88
|
+
# get the value ignoring white spaces in th end of the string.
|
89
|
+
def value_parse(value_string)
|
90
|
+
ar = value_string.split(' ')
|
91
|
+
ar.join(' ')
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
def build_attriubutes_from_hash
|
96
|
+
@@specification[self.class].each do |key, not_used|
|
97
|
+
self.instance_eval "
|
98
|
+
def #{key}
|
99
|
+
\"#{field_value(key)}\"
|
100
|
+
end
|
101
|
+
"
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
end
|
data/tests/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
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
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module TestHelpers
|
2
|
+
def create_sample_file
|
3
|
+
content = "jose new york brazil "
|
4
|
+
sample = File.new("samples/sample.txt","wb")
|
5
|
+
sample.puts content
|
6
|
+
sample.close
|
7
|
+
end
|
8
|
+
|
9
|
+
def create_sample_file2
|
10
|
+
content = "Richard New Orleans USA "
|
11
|
+
sample = File.new("samples/sample2.txt","wb")
|
12
|
+
sample.puts content
|
13
|
+
sample.close
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_sample_invalid
|
17
|
+
content = "Richard New Orleans USA "
|
18
|
+
sample = File.new("samples/invalid.txt","wb")
|
19
|
+
sample.puts content
|
20
|
+
sample.close
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'posifile'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'test_helpers'
|
4
|
+
require 'models/models'
|
5
|
+
|
6
|
+
class TestPosifile < Test::Unit::TestCase
|
7
|
+
|
8
|
+
include TestHelpers
|
9
|
+
|
10
|
+
def setup
|
11
|
+
create_sample_file
|
12
|
+
create_sample_file2
|
13
|
+
create_sample_invalid
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_posifile
|
17
|
+
client = Client.new("samples/sample.txt")
|
18
|
+
assert_equal "jose", client.name
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_posifile2
|
22
|
+
client = Client.new("samples/sample2.txt")
|
23
|
+
assert_equal "New Orleans", client.city
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_posifile_with_two_objects
|
27
|
+
client1 = Client.new("samples/sample.txt")
|
28
|
+
client2 = Client.new("samples/sample2.txt")
|
29
|
+
assert_equal "jose", client1.name
|
30
|
+
assert_equal "New Orleans", client2.city
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_value_parse
|
34
|
+
c = Client.new("samples/sample.txt")
|
35
|
+
assert_equal "esta frase precisa estar completa !", c.value_parse("esta frase precisa estar completa ! ")
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_field_value
|
39
|
+
c = Client.new("samples/sample.txt")
|
40
|
+
assert_equal "jose", c.field_value("name")
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_higher
|
44
|
+
assert_equal 42, Client.higher
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,46 @@
|
|
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
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: posifile
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- "Marco Antonio Foga\xC3\xA7a Nogueira"
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-03-29 00:00:00 -03:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Inteds to make easier to read position files. Still very imature.
|
23
|
+
email:
|
24
|
+
- marcofognog@gmail.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- README.rdoc
|
34
|
+
- lib/posifile.rb
|
35
|
+
- tests/Rakefile
|
36
|
+
- tests/models/models.rb
|
37
|
+
- tests/test_helpers.rb
|
38
|
+
- tests/tests_posifile.rb
|
39
|
+
- tests/tests_validations.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/marcofognog/posifile
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
hash: 3
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 21
|
64
|
+
segments:
|
65
|
+
- 1
|
66
|
+
- 3
|
67
|
+
- 7
|
68
|
+
version: 1.3.7
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.6.1
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Inteds to make easier to read position files.
|
76
|
+
test_files: []
|
77
|
+
|