caligrafo 0.1.5 → 0.1.6
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/VERSION +1 -1
- data/caligrafo.gemspec +3 -3
- data/lib/caligrafo/descriptor.rb +7 -5
- data/lib/caligrafo/writer.rb +12 -9
- data/test/caligrafo_test.rb +89 -1
- data/test/example.rb +4 -4
- metadata +12 -5
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.6
|
data/caligrafo.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{caligrafo}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.6"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Lucas de Castro"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2010-06-17}
|
13
13
|
s.description = %q{DSL para geração de arquivos texto.}
|
14
14
|
s.email = %q{castro.lucas@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -39,7 +39,7 @@ Gem::Specification.new do |s|
|
|
39
39
|
s.homepage = %q{http://github.com/lucasdecastro/caligrafo}
|
40
40
|
s.rdoc_options = ["--charset=UTF-8"]
|
41
41
|
s.require_paths = ["lib"]
|
42
|
-
s.rubygems_version = %q{1.3.
|
42
|
+
s.rubygems_version = %q{1.3.6}
|
43
43
|
s.summary = %q{DSL para geração de arquivos texto.}
|
44
44
|
s.test_files = [
|
45
45
|
"test/test_helper.rb",
|
data/lib/caligrafo/descriptor.rb
CHANGED
@@ -56,6 +56,12 @@ module Caligrafo
|
|
56
56
|
campo
|
57
57
|
end
|
58
58
|
|
59
|
+
def size
|
60
|
+
size = 0
|
61
|
+
@campos.each {|field| size += field.tamanho }
|
62
|
+
size
|
63
|
+
end
|
64
|
+
|
59
65
|
def dessa_linha?(linha)
|
60
66
|
linha =~ /^#{campos.first.valor_padrao}/
|
61
67
|
end
|
@@ -104,11 +110,7 @@ module Caligrafo
|
|
104
110
|
end
|
105
111
|
|
106
112
|
def tamanho
|
107
|
-
|
108
|
-
self.fim - self.inicio + 1 # Inclui o ultimo elemento
|
109
|
-
else
|
110
|
-
nil
|
111
|
-
end
|
113
|
+
intervalo.count
|
112
114
|
end
|
113
115
|
|
114
116
|
def intervalo
|
data/lib/caligrafo/writer.rb
CHANGED
@@ -9,7 +9,7 @@ module Caligrafo
|
|
9
9
|
file.extend FileExtension
|
10
10
|
file.estrutura = self.class.estrutura
|
11
11
|
file.linha = ''
|
12
|
-
file.numero_linha =
|
12
|
+
file.numero_linha = 0
|
13
13
|
file.objeto = self
|
14
14
|
file.bloco = bloco
|
15
15
|
|
@@ -24,6 +24,7 @@ module Caligrafo
|
|
24
24
|
|
25
25
|
def secao(nome, &bloco)
|
26
26
|
self.secao_corrente = self.estrutura.secoes.find {|secao| secao.nome == nome }
|
27
|
+
raise "Seção #{nome.inspect} não encontrada, verifique a descrição do arquivo." unless self.secao_corrente
|
27
28
|
|
28
29
|
if self.objeto.respond_to? nome
|
29
30
|
objetos = self.objeto.send nome
|
@@ -36,23 +37,25 @@ module Caligrafo
|
|
36
37
|
self.objeto = objeto
|
37
38
|
self.indice = index
|
38
39
|
|
40
|
+
nova_linha
|
39
41
|
bloco.call objeto
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
42
|
+
for campo in self.secao_corrente.campos
|
43
|
+
valor = (campo.valor_guardado || campo.valor_para(self.objeto))
|
44
|
+
|
45
|
+
# Só preenche o espaço quando o novo valor não for vazio.
|
46
|
+
# Isso permite que a inicialização da linha inteira não seja sobrescrita com espaços em branco.
|
47
|
+
self.linha[campo.intervalo] = valor if not valor.strip.empty?
|
44
48
|
end
|
45
|
-
|
46
|
-
|
49
|
+
self.print self.linha
|
50
|
+
self.print "\n"
|
47
51
|
end
|
48
52
|
|
49
53
|
self.objeto = self.bloco.binding.send :eval, "self"
|
50
54
|
end
|
51
55
|
|
52
56
|
def nova_linha
|
53
|
-
self.linha = ''
|
57
|
+
self.linha = ' ' * self.secao_corrente.size
|
54
58
|
self.numero_linha += 1
|
55
|
-
self.print "\n"
|
56
59
|
end
|
57
60
|
|
58
61
|
# Sobrescreve a definição do arquivo.
|
data/test/caligrafo_test.rb
CHANGED
@@ -23,10 +23,87 @@ class CaligrafoTest < Test::Unit::TestCase
|
|
23
23
|
EOF
|
24
24
|
end
|
25
25
|
|
26
|
+
def test_tamanho_campo
|
27
|
+
cabecalho, telefones, sites, rodape = Portifolio.estrutura.secoes
|
28
|
+
|
29
|
+
tipo, nome, idade, salario, vazio, linha = cabecalho.campos
|
30
|
+
assert_equal 2, tipo.tamanho
|
31
|
+
assert_equal 50, nome.tamanho
|
32
|
+
assert_equal 3, idade.tamanho
|
33
|
+
assert_equal 7, salario.tamanho
|
34
|
+
assert_equal 1, linha.tamanho
|
35
|
+
|
36
|
+
tipo, descricao, numero, linha = telefones.campos
|
37
|
+
assert_equal 2, tipo.tamanho
|
38
|
+
assert_equal 8, descricao.tamanho
|
39
|
+
assert_equal 59, numero.tamanho
|
40
|
+
assert_equal 1, linha.tamanho
|
41
|
+
|
42
|
+
tipo, site, linha = sites.campos
|
43
|
+
assert_equal 2, tipo.tamanho
|
44
|
+
assert_equal 67, site.tamanho
|
45
|
+
assert_equal 1, linha.tamanho
|
46
|
+
|
47
|
+
tipo, fim, vazio, linha = rodape.campos
|
48
|
+
assert_equal 2, tipo.tamanho
|
49
|
+
assert_equal 3, fim.tamanho
|
50
|
+
assert_equal 64, vazio.tamanho
|
51
|
+
assert_equal 1, linha.tamanho
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_intervalo_campo
|
55
|
+
cabecalho, telefones, sites, rodape = Portifolio.estrutura.secoes
|
56
|
+
|
57
|
+
tipo, descricao, numero, linha = telefones.campos
|
58
|
+
assert_equal 0..1, tipo.intervalo
|
59
|
+
assert_equal 2..9, descricao.intervalo
|
60
|
+
assert_equal 10..68, numero.intervalo
|
61
|
+
assert_equal 69..69, linha.intervalo
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_tamanho_secao
|
65
|
+
cabecalho, telefones, sites, rodape = Portifolio.estrutura.secoes
|
66
|
+
assert_equal 70, cabecalho.size
|
67
|
+
assert_equal 70, telefones.size
|
68
|
+
assert_equal 70, sites.size
|
69
|
+
assert_equal 70, rodape.size
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_escrever_arquivo_com_linha_predefinida
|
73
|
+
def @portifolio.gerar_arquivo(nome_arquivo)
|
74
|
+
escrever_arquivo nome_arquivo do |f|
|
75
|
+
f.secao :cabecalho do
|
76
|
+
f.imprimir :linha, f.numero_linha
|
77
|
+
end
|
78
|
+
f.secao :telefones do |t|
|
79
|
+
f.linha = "02 ?"
|
80
|
+
f.imprimir :descricao, "Fone##{f.indice + 1}: "
|
81
|
+
end
|
82
|
+
f.secao :sites do
|
83
|
+
f.imprimir :linha, f.numero_linha
|
84
|
+
end
|
85
|
+
f.secao :rodape do
|
86
|
+
f.imprimir :linha, f.numero_linha
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
@portifolio.gerar_arquivo 'test/arquivo_gerado.txt'
|
92
|
+
assert_file_content 'test/arquivo_gerado.txt', <<-EOF
|
93
|
+
01Lucas da Silva 0259000050 1
|
94
|
+
02Fone#1: 55 86 2222-3333 ?
|
95
|
+
02Fone#2: 55 86 9999-1234 ?
|
96
|
+
03google.com 4
|
97
|
+
03blip.tv 5
|
98
|
+
03slideshare.net 6
|
99
|
+
04FIM 7
|
100
|
+
EOF
|
101
|
+
end
|
102
|
+
|
26
103
|
def test_numero_linha
|
27
104
|
counter = 1
|
28
105
|
@portifolio.ler_arquivo 'test/example.txt' do |linha|
|
29
|
-
|
106
|
+
assert_equal counter, linha.numero
|
30
107
|
counter += 1
|
31
108
|
end
|
32
109
|
assert 7, counter
|
@@ -79,6 +156,17 @@ class CaligrafoTest < Test::Unit::TestCase
|
|
79
156
|
end
|
80
157
|
end
|
81
158
|
|
159
|
+
def test_preencher_campo_com_linha_resetada
|
160
|
+
@portifolio.ler_arquivo 'test/example.txt' do |linha|
|
161
|
+
case linha.secao
|
162
|
+
when :cabecalho
|
163
|
+
linha.preencher(:nome, 'Lucas de Castro')
|
164
|
+
assert_equal 'Lucas de Castro', linha.ler(:nome)
|
165
|
+
assert_equal "01Lucas de Castro 0259000050 1\n", linha
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
82
170
|
def test_criar_arquivo_retorno
|
83
171
|
Portifolio.ler_arquivo 'test/example.txt', :arquivo_retorno => 'test/retorno.txt' do |linha|
|
84
172
|
case linha.secao
|
data/test/example.rb
CHANGED
@@ -19,24 +19,24 @@ class Portifolio < OpenStruct
|
|
19
19
|
campo :idade, :tamanho => 3, :formato => :numerico
|
20
20
|
campo :salario, :tamanho => 7
|
21
21
|
campo :vazio, 7.espacos
|
22
|
-
campo :linha
|
22
|
+
campo :linha, :tamanho => 1
|
23
23
|
end
|
24
24
|
secao :telefones do
|
25
25
|
campo :tipo, '02'
|
26
26
|
campo :descricao, :tamanho => 8
|
27
27
|
campo :to_s, :tamanho => 59, :formato => :fone
|
28
|
-
campo :linha
|
28
|
+
campo :linha, :tamanho => 1
|
29
29
|
end
|
30
30
|
secao :sites do
|
31
31
|
campo :tipo, '03'
|
32
32
|
campo :downcase, :tamanho => 67
|
33
|
-
campo :linha
|
33
|
+
campo :linha, :tamanho => 1
|
34
34
|
end
|
35
35
|
secao :rodape do
|
36
36
|
campo :tipo, '04'
|
37
37
|
campo :fim, 'FIM'
|
38
38
|
campo :vazio, 64.espacos
|
39
|
-
campo :linha
|
39
|
+
campo :linha, :tamanho => 1
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: caligrafo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 6
|
9
|
+
version: 0.1.6
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Lucas de Castro
|
@@ -9,7 +14,7 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2010-06-17 00:00:00 -03:00
|
13
18
|
default_executable:
|
14
19
|
dependencies: []
|
15
20
|
|
@@ -54,18 +59,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
54
59
|
requirements:
|
55
60
|
- - ">="
|
56
61
|
- !ruby/object:Gem::Version
|
62
|
+
segments:
|
63
|
+
- 0
|
57
64
|
version: "0"
|
58
|
-
version:
|
59
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
66
|
requirements:
|
61
67
|
- - ">="
|
62
68
|
- !ruby/object:Gem::Version
|
69
|
+
segments:
|
70
|
+
- 0
|
63
71
|
version: "0"
|
64
|
-
version:
|
65
72
|
requirements: []
|
66
73
|
|
67
74
|
rubyforge_project:
|
68
|
-
rubygems_version: 1.3.
|
75
|
+
rubygems_version: 1.3.6
|
69
76
|
signing_key:
|
70
77
|
specification_version: 3
|
71
78
|
summary: "DSL para gera\xC3\xA7\xC3\xA3o de arquivos texto."
|