Sha256: 70c9348bf7b66f27948b4b7133c1eb8dc161730475aca3c8ea5714b0815cd973

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

require 'happymapper'

module Saml
  module Base
    extend ActiveSupport::Concern

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

      attr_accessor :xml_value
      has_xml_content

      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

      attr_writer :from_xml

      def from_xml?
        @from_xml
      end
    end

    module HappyMapperClassMethods
      def parse(xml, options = {})
        if xml.is_a?(String)
          ActiveSupport::XmlMini_REXML.parse(xml)
        end

        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, REXML::ParseException => e
        raise Saml::Errors::UnparseableMessage.new(e.message)
      rescue TypeError => e
        raise Saml::Errors::UnparseableMessage.new(e.message)
      rescue NoMethodError => e
        raise Saml::Errors::UnparseableMessage.new(e.message)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
libsaml-2.14.0 lib/saml/base.rb
libsaml-2.13.1 lib/saml/base.rb