caligrafo 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/VERSION +1 -1
- data/caligrafo.gemspec +2 -2
- data/lib/caligrafo/descriptor.rb +12 -8
- data/lib/caligrafo/reader.rb +34 -14
- data/test/caligrafo_test.rb +45 -0
- metadata +2 -2
data/.gitignore
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.5
|
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.5"
|
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{2009-11-
|
12
|
+
s.date = %q{2009-11-20}
|
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 = [
|
data/lib/caligrafo/descriptor.rb
CHANGED
@@ -111,9 +111,13 @@ module Caligrafo
|
|
111
111
|
end
|
112
112
|
end
|
113
113
|
|
114
|
-
def
|
114
|
+
def intervalo
|
115
115
|
fim = self.fim || 0
|
116
|
-
|
116
|
+
(self.inicio - 1)..(fim - 1)
|
117
|
+
end
|
118
|
+
|
119
|
+
def ler(linha)
|
120
|
+
substring = linha[self.intervalo]
|
117
121
|
|
118
122
|
if formatador
|
119
123
|
formatador.string_to_value substring
|
@@ -142,12 +146,6 @@ module Caligrafo
|
|
142
146
|
raise "Erro ao preencher valor para #{objeto.inspect} no campo #{self.inspect}: #{e.message}"
|
143
147
|
end
|
144
148
|
|
145
|
-
private
|
146
|
-
|
147
|
-
def chamar_metodo?
|
148
|
-
@chamar_metodo
|
149
|
-
end
|
150
|
-
|
151
149
|
def formatar(valor)
|
152
150
|
formatador = self.formatador
|
153
151
|
formatador ||= Converter.pesquisar_por_tipo valor.class
|
@@ -157,6 +155,12 @@ module Caligrafo
|
|
157
155
|
string
|
158
156
|
end
|
159
157
|
|
158
|
+
private
|
159
|
+
|
160
|
+
def chamar_metodo?
|
161
|
+
@chamar_metodo
|
162
|
+
end
|
163
|
+
|
160
164
|
def ajustar_inicio_e_fim(tamanho)
|
161
165
|
self.inicio ||= calcular_inicio
|
162
166
|
self.fim ||= self.inicio + tamanho - 1 if tamanho
|
data/lib/caligrafo/reader.rb
CHANGED
@@ -1,28 +1,41 @@
|
|
1
1
|
module Caligrafo
|
2
2
|
module Reader
|
3
|
-
def ler_arquivo(nome, &bloco)
|
3
|
+
def ler_arquivo(nome, opcoes={}, &bloco)
|
4
4
|
estrutura = (self.is_a?(Class) ? self.estrutura : self.class.estrutura)
|
5
5
|
raise 'A estrutura nao foi definida' unless estrutura
|
6
6
|
|
7
|
+
if output = opcoes.delete(:arquivo_retorno)
|
8
|
+
output_file = File.new(output, 'w')
|
9
|
+
secoes_retorno = opcoes.delete(:secoes_retorno)
|
10
|
+
secoes_retorno ||= estrutura.secoes.collect {|secao| secao.nome }
|
11
|
+
linha_retorno = 1
|
12
|
+
end
|
13
|
+
|
7
14
|
File.open(nome, 'r') do |file|
|
8
15
|
while linha = file.gets
|
9
16
|
linha.extend LineExtension
|
10
17
|
linha.arquivo = estrutura
|
11
18
|
linha.descobrir_secao
|
12
19
|
linha.numero = file.lineno
|
20
|
+
linha.numero_retorno = linha_retorno
|
13
21
|
|
14
22
|
bloco.call linha
|
23
|
+
|
24
|
+
if output and secoes_retorno.include? linha.secao
|
25
|
+
output_file.puts linha.chomp
|
26
|
+
linha_retorno += 1
|
27
|
+
end
|
15
28
|
end
|
16
29
|
end
|
30
|
+
|
31
|
+
output_file.close if output
|
17
32
|
end
|
18
33
|
|
19
34
|
module LineExtension
|
20
|
-
attr_accessor :arquivo, :numero
|
35
|
+
attr_accessor :arquivo, :numero, :numero_retorno
|
21
36
|
|
22
37
|
def secao
|
23
|
-
|
24
|
-
@secao.nome
|
25
|
-
end
|
38
|
+
@secao.nome
|
26
39
|
end
|
27
40
|
|
28
41
|
def secao?(nome_secao)
|
@@ -30,23 +43,30 @@ module Caligrafo
|
|
30
43
|
end
|
31
44
|
|
32
45
|
def ler(nome_campo)
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
46
|
+
self.campo(nome_campo).ler(self)
|
47
|
+
end
|
48
|
+
|
49
|
+
def preencher(nome_campo, novo_valor)
|
50
|
+
campo = self.campo(nome_campo)
|
51
|
+
self[campo.intervalo] = campo.formatar(novo_valor)
|
52
|
+
self
|
37
53
|
end
|
38
54
|
|
39
55
|
def ler_campos
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
hash
|
44
|
-
end
|
56
|
+
hash = {}
|
57
|
+
@secao.campos.each {|c| hash[c.nome] = c.ler(self) }
|
58
|
+
hash
|
45
59
|
end
|
46
60
|
|
47
61
|
def descobrir_secao
|
48
62
|
@secao = @arquivo.secoes.find {|s| s.dessa_linha?(self) }
|
49
63
|
end
|
64
|
+
|
65
|
+
def campo(nome_campo)
|
66
|
+
campo = @secao.campos.find {|c| c.nome == nome_campo }
|
67
|
+
raise ArgumentError, "campo com o nome '#{nome_campo}' nao foi encontrado." unless campo
|
68
|
+
campo
|
69
|
+
end
|
50
70
|
end
|
51
71
|
end
|
52
72
|
end
|
data/test/caligrafo_test.rb
CHANGED
@@ -68,6 +68,51 @@ class CaligrafoTest < Test::Unit::TestCase
|
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
+
def test_preencher_campo
|
72
|
+
@portifolio.ler_arquivo 'test/example.txt' do |linha|
|
73
|
+
case linha.secao
|
74
|
+
when :cabecalho
|
75
|
+
linha.preencher(:nome, 'Lucas de Castro')
|
76
|
+
assert_equal 'Lucas de Castro', linha.ler(:nome)
|
77
|
+
assert_equal "01Lucas de Castro 0259000050 1\n", linha
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_criar_arquivo_retorno
|
83
|
+
Portifolio.ler_arquivo 'test/example.txt', :arquivo_retorno => 'test/retorno.txt' do |linha|
|
84
|
+
case linha.secao
|
85
|
+
when :cabecalho
|
86
|
+
linha.preencher(:nome, 'Lucas de Castro')
|
87
|
+
end
|
88
|
+
end
|
89
|
+
assert_file_content 'test/retorno.txt', <<-EOF
|
90
|
+
01Lucas de Castro 0259000050 1
|
91
|
+
02Fone#1: 55 86 2222-3333 2
|
92
|
+
02Fone#2: 55 86 9999-1234 3
|
93
|
+
03google.com 4
|
94
|
+
03blip.tv 5
|
95
|
+
03slideshare.net 6
|
96
|
+
04FIM 7
|
97
|
+
EOF
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_criar_arquivo_retorno_limitando_secoes
|
101
|
+
Portifolio.ler_arquivo 'test/example.txt',
|
102
|
+
:arquivo_retorno => 'test/retorno.txt',
|
103
|
+
:secoes_retorno => [:cabecalho, :rodape] do |linha|
|
104
|
+
case linha.secao
|
105
|
+
when :cabecalho
|
106
|
+
linha.preencher :nome, 'Lucas de Castro'
|
107
|
+
when :rodape
|
108
|
+
linha.preencher :linha, linha.numero_retorno
|
109
|
+
end
|
110
|
+
end
|
111
|
+
assert_file_content 'test/retorno.txt', <<-EOF
|
112
|
+
01Lucas de Castro 0259000050 1
|
113
|
+
04FIM 2
|
114
|
+
EOF
|
115
|
+
end
|
71
116
|
def test_description
|
72
117
|
arquivo = Portifolio.estrutura
|
73
118
|
assert arquivo
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: caligrafo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lucas de Castro
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-11-
|
12
|
+
date: 2009-11-20 00:00:00 -03:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|