Created
July 23, 2012 14:09
-
-
Save jnwelzel/3163800 to your computer and use it in GitHub Desktop.
Super classe usada no projeto CT-e para definir um objeto que possa ser tanto persistido como serializado para XML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
class EntidadeCte | |
include Mongoid::Document | |
include ROXML | |
class << self | |
# apelida o xml_accessor do ROXML para cte_attr | |
# alias :cte_attr :xml_accessor | |
# define um accessor ruby comum para os atributos do cte | |
def xml_accessor(*attrs) | |
attr_accessor *attrs | |
end | |
# todos os atributos da entidade | |
def cte_attrs | |
roxml_attrs.map(&:attr_name) | |
end | |
def cte_attr(*syms, &block) | |
xml_attr(*syms, &block).each do |attr| | |
add_reader(attr) | |
attr_writer(attr.attr_name) | |
class_eval <<-ATTR | |
def #{attr.attr_name}=(value) | |
@#{attr.attr_name} = value | |
super | |
end | |
ATTR | |
end | |
end | |
end | |
# todos os atributos da entidade do objeto | |
def cte_attrs | |
self.class.cte_attrs | |
end | |
# retorna o xml representando a entidade | |
def to_cte | |
doc = Nokogiri::XML::Document.new | |
doc.root = to_xml | |
doc.serialize | |
end | |
# retorna o xml ou delega a classe herdada | |
def to_s | |
self.respond_to? :to_cte ? to_cte : super | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment