Module: CpfCnpjActiveRecord::ClassMethods
- Defined in:
- lib/flexa_lib/brcpfcnpj/cpf_cnpj_activerecord.rb
Overview
:nodoc:
Instance Method Summary collapse
- #add_composed_class(name, klass) ⇒ Object
- #create_code(name, klass) ⇒ Object
- #init(args, klass) ⇒ Object
-
#usar_como_cnpj(*args) ⇒ Object
:nodoc:.
-
#usar_como_cpf(*args) ⇒ Object
:nodoc:.
Instance Method Details
#add_composed_class(name, klass) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/flexa_lib/brcpfcnpj/cpf_cnpj_activerecord.rb', line 24 def add_composed_class(name, klass) = {:class_name => klass, :mapping => [name.to_s, "numero"], :allow_nil => true} constructor = Proc.new { |numero| eval(klass).new(numero) } converter = Proc.new { |value| eval(klass).new(value) } begin composed_of name, .merge( { :constructor => constructor, :converter => converter } ) rescue Exception composed_of name, { eval(klass).new(name[:numero]) } end end |
#create_code(name, klass) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/flexa_lib/brcpfcnpj/cpf_cnpj_activerecord.rb', line 35 def create_code(name, klass) str = <<-CODE validate :#{name}_valido? def #{name}_valido? value = read_attribute('#{name}') if !value.nil? && value.strip != '' && !#{name}.nil? && !#{name}.valido? self.errors.add('#{name}', :invalid) end end def #{name}=(value) if value.blank? write_attribute('#{name}', nil) elsif value.kind_of?(#{eval(klass)}) write_attribute('#{name}', value.numero) else begin c = #{eval(klass)}.new(value) c.valido? ? write_attribute('#{name}', c.numero) : write_attribute('#{name}', value) rescue @#{name} = value end end end CODE end |
#init(args, klass) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/flexa_lib/brcpfcnpj/cpf_cnpj_activerecord.rb', line 15 def init(args, klass) unless args.size.zero? args.each do |name| add_composed_class(name, klass) module_eval create_code(name.to_s, klass) end end end |
#usar_como_cnpj(*args) ⇒ Object
:nodoc:
11 12 13 |
# File 'lib/flexa_lib/brcpfcnpj/cpf_cnpj_activerecord.rb', line 11 def usar_como_cnpj(*args) #:nodoc: init(args, 'Cnpj') end |
#usar_como_cpf(*args) ⇒ Object
:nodoc:
7 8 9 |
# File 'lib/flexa_lib/brcpfcnpj/cpf_cnpj_activerecord.rb', line 7 def usar_como_cpf(*args) #:nodoc: init(args, 'Cpf') end |