caligrafo 0.1.0 → 0.1.1

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 CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{caligrafo}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
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-10-24}
12
+ s.date = %q{2009-10-26}
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 = [
@@ -14,20 +14,29 @@ module Caligrafo
14
14
  end
15
15
 
16
16
  module Formatador
17
- def self.formatos
18
- @@formatos
17
+ def self.formatadores
18
+ @@formatadores ||= {}
19
19
  end
20
20
 
21
21
  def self.registrar(nome, formatador)
22
- @@formatos ||= {}
23
- @@formatos[nome] = formatador.new
22
+ self.formatadores[nome] = formatador.new
24
23
  end
25
24
 
26
- def self.pesquisar(tipo)
27
- formatador = @@formatos.values.find { |f| f.tipos.include? tipo }
28
- formatador ||= @@formatos[:default]
25
+ def self.pesquisar_por_nome(nome)
26
+ self.formatadores[nome]
29
27
  end
30
28
 
29
+ def self.pesquisar_por_nome!(nome)
30
+ self.pesquisar_por_nome(nome) or raise FormatadorNaoEncontrado, "O formatador #{nome.inspect} nao foi registrado!"
31
+ end
32
+
33
+ def self.pesquisar_por_tipo(tipo)
34
+ formatador = self.formatadores.values.find { |f| f.tipos.include? tipo }
35
+ formatador ||= self.formatadores[:default]
36
+ end
37
+
38
+ class FormatadorNaoEncontrado < Exception; end
39
+
31
40
  class Base
32
41
  attr_reader :tipos, :alinhamento, :preenchimento
33
42
 
@@ -53,6 +62,15 @@ module Caligrafo
53
62
  end
54
63
  end
55
64
 
65
+ class Data < Base
66
+ def tipos
67
+ [Date]
68
+ end
69
+ def formatar(valor, opcoes={})
70
+ valor.strftime('%Y%m%d')
71
+ end
72
+ end
73
+
56
74
  class Numerico < Base
57
75
  def initialize
58
76
  @tipos = [Fixnum]
@@ -77,9 +95,11 @@ module Caligrafo
77
95
  end
78
96
  end
79
97
 
80
- self.registrar :default, Base
98
+ self.registrar :default, Base
99
+ self.registrar :alpha, Base
81
100
  self.registrar :numerico, Numerico
82
- self.registrar :decimal, Decimal
101
+ self.registrar :decimal, Decimal
102
+ self.registrar :data, Caligrafo::Formatador::Data
83
103
  end
84
104
 
85
105
  class Arquivo
@@ -190,9 +210,9 @@ module Caligrafo
190
210
  end
191
211
  def formatar(valor)
192
212
  if self.formato
193
- formatador = Caligrafo::Formatador.formatos[self.formato]
213
+ formatador = Formatador.pesquisar_por_nome self.formato
194
214
  else
195
- formatador = Formatador.pesquisar valor.class
215
+ formatador = Formatador.pesquisar_por_tipo valor.class
196
216
  end
197
217
 
198
218
  string = formatador.formatar(valor, self.opcoes_para_formatador)
@@ -48,6 +48,7 @@ class CaligrafoTest < Test::Unit::TestCase
48
48
  def setup
49
49
  @target = 'test/arquivo_gerado.txt'
50
50
  end
51
+
51
52
  def teardown
52
53
  File.delete @target rescue nil
53
54
  end
@@ -75,4 +76,13 @@ FIM
75
76
 
76
77
  assert_equal_files nome_arquivo_esperado, @target
77
78
  end
79
+
80
+ def test_pesquisar_formatador_inexistente
81
+ assert_raise Caligrafo::Formatador::FormatadorNaoEncontrado do
82
+ Caligrafo::Formatador.pesquisar_por_nome! :bolinha
83
+ end
84
+ assert_nothing_raised do
85
+ Caligrafo::Formatador.pesquisar_por_nome! :numerico
86
+ end
87
+ end
78
88
  end
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.0
4
+ version: 0.1.1
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-10-24 00:00:00 -03:00
12
+ date: 2009-10-26 00:00:00 -03:00
13
13
  default_executable:
14
14
  dependencies: []
15
15