module Nfe::Helpers::XmlModel::NfeModel

Public Class Methods

included(base) click to toggle source
# File lib/nfe/helpers/xml_model/nfe_model.rb, line 5
def self.included(base)
  base.extend ClassMethods
end

Public Instance Methods

add_elements(xml, *elements) click to toggle source
# File lib/nfe/helpers/xml_model/nfe_model.rb, line 71
def add_elements(xml, *elements)
  elements = get_elements(elements)

  Array(elements).each { |attribute| add_element xml, attribute }
end
check_present() click to toggle source
# File lib/nfe/helpers/xml_model/nfe_model.rb, line 43
def check_present
  present = false
  self.class.xml_attributes.each do |key, value|
    present ||= value_present? key, value
  end
  present
end
parse!(xml) click to toggle source
# File lib/nfe/helpers/xml_model/nfe_model.rb, line 51
def parse!(xml)
  parse_elements xml
end
parse_elements(doc, *elements) click to toggle source
# File lib/nfe/helpers/xml_model/nfe_model.rb, line 63
def parse_elements(doc, *elements)
  doc = Nokogiri::XML(doc) if doc.is_a? String

  elements = get_elements(elements)

  Array(elements).each { |attribute| parse_value doc, attribute }
end
present?() click to toggle source
# File lib/nfe/helpers/xml_model/nfe_model.rb, line 39
def present?
  check_present
end
to_xml() click to toggle source
# File lib/nfe/helpers/xml_model/nfe_model.rb, line 55
def to_xml
  xml = Builder::XmlMarkup.new

  add_elements xml

  xml.target!
end
validate_optional_attributes(*attributes) click to toggle source
# File lib/nfe/helpers/xml_model/nfe_model.rb, line 77
def validate_optional_attributes(*attributes)
  attributes.each do |attribute|
    Array(send(attribute)).each do |item|
      if item.present? && !item.valid?
        errors.add(attribute, :invalid) unless errors.include?(attribute)
      end
    end
  end
end
validate_required_attributes(*attributes) click to toggle source
# File lib/nfe/helpers/xml_model/nfe_model.rb, line 87
def validate_required_attributes(*attributes)
  attributes.each do |attribute|
    if !send(attribute).valid?
      errors.add(attribute, :invalid) unless errors.include?(attribute)
    end
  end
end

Private Instance Methods

add_element(xml, attribute) click to toggle source
# File lib/nfe/helpers/xml_model/nfe_model.rb, line 100
def add_element(xml, attribute)
  self.class.xml_attribute(attribute).add_to_xml self, xml, attribute
end
get_elements(elements) click to toggle source
# File lib/nfe/helpers/xml_model/nfe_model.rb, line 104
def get_elements(elements)
  elements = self.class.xml_attributes.keys if elements.empty?

  elements
end
parse_value(doc, attribute) click to toggle source
# File lib/nfe/helpers/xml_model/nfe_model.rb, line 96
def parse_value(doc, attribute)
  self.class.xml_attribute(attribute).parse self, doc
end
value_present?(key, value) click to toggle source
# File lib/nfe/helpers/xml_model/nfe_model.rb, line 110
def value_present?(key, value)
  if value.type.equal? :bigdecimal
    send(key) > 0
  else
    send(key).present?
  end
end