module Xeroizer
  module Record
    class Base
      def to_xml(b = Builder::XmlMarkup.new(:indent => 2))
        #This monkey patch somehow causes error while pushing EmployeeFile
        # TODO: Investiate this once we implement timesheet
        optional_root_tag(parent.class.optional_xml_root_name, b) do |b|
          if parent.class.skip_xml_node_name
            attributes.each do | key, value |
              field = self.class.fields[key]
              value = self.send(key) if field[:calculated]
              xml_value_from_field(b, field, value) unless value.nil?
            end
          else
            b.tag!(parent.class.xml_node_name || parent.model_name) {
              attributes.each do | key, value |
                field = self.class.fields[key]
                value = self.send(key) if field[:calculated]
                xml_value_from_field(b, field, value) unless value.nil?
              end
            }
          end
        end
      end
    end

    class BaseModel
      class_inheritable_attributes :skip_xml_node_name

      def self.set_skip_xml_node_name(skip_xml_node_name=false)
        self.skip_xml_node_name = skip_xml_node_name
      end
    end
  end
end
