Sha256: 32096d8362f1c3351abb63805863d55e13181be3a36d94abf850c25d2d53d98a

Contents?: true

Size: 1.13 KB

Versions: 10

Compression:

Stored size: 1.13 KB

Contents

require "happymapper"

module Xmlenc
  module Builder
    module Base
      extend ActiveSupport::Concern

      included do
        include ::HappyMapper
        include ::ActiveModel::Validations

        extend HappyMapperClassMethods
        include HappyMapperInstanceMethods
      end

      module HappyMapperInstanceMethods
        def initialize(attributes = {})
          attributes.each do |key, value|
            send("#{key}=", value) if respond_to?("#{key}=") && value.present?
          end
        end

        def from_xml=(bool)
          @from_xml = bool
        end

        def from_xml?
          @from_xml
        end
      end

      module HappyMapperClassMethods
        def parse(xml, options = {})
          object = super
          if object.is_a?(Array)
            object.map { |x| x.from_xml = true }
          elsif object
            object.from_xml = true
          end
          object
        rescue Nokogiri::XML::SyntaxError => e
          raise Xmlenc::UnparseableMessage.new(e.message)
        rescue NoMethodError => e
          raise Xmlenc::UnparseableMessage.new(e.message)
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
xmlenc-0.2.1 lib/xmlenc/builder/base.rb
xmlenc-0.2.0 lib/xmlenc/builder/base.rb
xmlenc-0.1.7 lib/xmlenc/builder/base.rb
xmlenc-0.1.6 lib/xmlenc/builder/base.rb
xmlenc-0.1.5 lib/xmlenc/builder/base.rb
xmlenc-0.1.4 lib/xmlenc/builder/base.rb
xmlenc-0.1.3 lib/xmlenc/builder/base.rb
xmlenc-0.1.2 lib/xmlenc/builder/base.rb
xmlenc-0.1.1 lib/xmlenc/builder/base.rb
xmlenc-0.1.0 lib/xmlenc/builder/base.rb