brcpfcnpj 2.0.13 → 2.0.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,74 +4,57 @@ module CpfCnpjActiveRecord #:nodoc:
4
4
  end
5
5
  module ClassMethods #:nodoc:
6
6
  def usar_como_cpf(*args) #:nodoc:
7
- unless args.size.zero?
8
- args.each do |name|
9
- composed_of name, :class_name => 'Cpf', :mapping => [name.to_s, "numero"], :allow_nil => true do
10
- Cpf.new(name[:numero])
11
- end
12
- name = name.to_s
13
- module_eval <<-ADICIONANDO_METODOS_PARA_CPF
14
- validate :#{name}_valido?
15
-
16
- def #{name}_valido?
17
- value = read_attribute('#{name}')
18
- if !value.nil? && value.strip != '' && !#{name}.nil? && !#{name}.valido?
19
- self.errors.add('#{name}', 'numero invalido')
20
- end
21
- end
22
-
23
- def #{name}=(value)
24
- if value.nil?
25
- write_attribute('#{name}', nil)
26
- elsif value.kind_of?(Cpf)
27
- write_attribute('#{name}', value.numero)
28
- else
29
- begin
30
- write_attribute('#{name}', value)
31
- rescue
32
- @#{name} = value
33
- end
34
- end
35
- end
36
- ADICIONANDO_METODOS_PARA_CPF
37
- end
38
- end
7
+ init(args, 'Cpf')
39
8
  end
40
9
 
41
10
  def usar_como_cnpj(*args) #:nodoc:
11
+ init(args, 'Cnpj')
12
+ end
13
+
14
+ def init(args, klass)
42
15
  unless args.size.zero?
43
16
  args.each do |name|
44
- composed_of name, :class_name => 'Cnpj', :mapping => [name.to_s, "numero"], :allow_nil => true do
45
- Cnpj.new(name[:numero])
46
- end
17
+ add_composed_class(name, klass)
18
+ module_eval create_code(name.to_s, klass)
19
+ end
20
+ end
21
+ end
47
22
 
48
- name = name.to_s
49
- module_eval <<-ADICIONANDO_METODOS_PARA_CNPJ
50
- validate :#{name}_valido?
23
+ def add_composed_class(name, klass)
24
+ options = {:class_name => klass, :mapping => [name.to_s, "numero"], :allow_nil => true}
25
+ constructor = Proc.new { |numero| eval(klass).new(numero) }
26
+ converter = Proc.new { |value| eval(klass).new(value) }
27
+ begin
28
+ composed_of name, options.merge( { :constructor => constructor, :converter => converter } )
29
+ rescue Exception
30
+ composed_of name, options { eval(klass).new(name[:numero]) }
31
+ end
32
+ end
51
33
 
52
- def #{name}_valido?
53
- value = read_attribute('#{name}')
54
- if !value.nil? && value.strip != '' && !#{name}.nil? && !#{name}.valido?
55
- self.errors.add('#{name}', 'numero invalido')
56
- end
34
+ def create_code(name, klass)
35
+ str = <<-CODE
36
+ validate :#{name}_valido?
37
+ def #{name}_valido?
38
+ value = read_attribute('#{name}')
39
+ if !value.nil? && value.strip != '' && !#{name}.nil? && !#{name}.valido?
40
+ self.errors.add('#{name}', 'numero invalido')
57
41
  end
58
-
59
- def #{name}=(value)
60
- if value.nil?
61
- write_attribute('#{name}', nil)
62
- elsif value.kind_of?(Cnpj)
63
- write_attribute('#{name}', value.numero)
64
- else
65
- begin
66
- write_attribute('#{name}', value)
67
- rescue
68
- @#{name} = value
69
- end
42
+ end
43
+ def #{name}=(value)
44
+ if value.nil?
45
+ write_attribute('#{name}', nil)
46
+ elsif value.kind_of?(#{eval(klass)})
47
+ write_attribute('#{name}', value.numero)
48
+ else
49
+ begin
50
+ c = #{eval(klass)}.new(value)
51
+ c.valido? ? write_attribute('#{name}', c.numero) : write_attribute('#{name}', value)
52
+ rescue
53
+ @#{name} = value
70
54
  end
71
55
  end
72
- ADICIONANDO_METODOS_PARA_CNPJ
73
56
  end
74
- end
57
+ CODE
75
58
  end
76
59
  end
77
60
  end
@@ -1,5 +1,5 @@
1
1
  module BrCpfCnpj
2
2
  module VERSION #:nodoc:
3
- STRING = "2.0.13"
3
+ STRING = "2.0.14"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brcpfcnpj
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.13
4
+ version: 2.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Marcos Tapaj\xC3\xB3s"
@@ -13,7 +13,7 @@ autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
15
 
16
- date: 2008-12-16 00:00:00 -02:00
16
+ date: 2009-01-20 00:00:00 -02:00
17
17
  default_executable:
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency