Sha256: 2cfc4e291d49c26959ed9ec761d66a26a1c30fcbfdfaa9be259eca3ddb5d9c06

Contents?: true

Size: 1.39 KB

Versions: 4

Compression:

Stored size: 1.39 KB

Contents

require 'saml2/base'

module SAML2
  module IndexedObject
    attr_reader :index

    def initialize(*args)
      @is_default = nil
      super
    end

    def eql?(rhs)
      index == rhs.index &&
          default? == rhs.default? &&
          super
    end

    def default?
      !!@is_default
    end

    def default_defined?
      !@is_default.nil?
    end

    def from_xml(node)
      @index = node['index'] && node['index'].to_i
      @is_default = node['isDefault'] && node['isDefault'] == 'true'
      super
    end

    class Array < ::Array
      attr_reader :default

      def self.from_xml(nodes)
        new(nodes.map { |node| name.split('::')[1..-2].inject(SAML2) { |mod, klass| mod.const_get(klass) }.from_xml(node) })
      end

      def initialize(objects)
        replace(objects.sort_by { |object| object.index || 0 })
        @index = {}
        each { |object| @index[object.index] = object }
        @default = find { |object| object.default? } || first

        freeze
      end

      def [](index)
        @index[index]
      end

      def resolve(index)
        index ? self[index] : default
      end
    end

    def build(builder, *)
      super
      builder.parent.children.last['index'] = index
      builder.parent.children.last['isDefault'] = default? if default_defined?
    end

    private
    def self.included(klass)
      klass.const_set(:Array, Array.dup)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
saml2-1.1.3 lib/saml2/indexed_object.rb
saml2-1.1.2 lib/saml2/indexed_object.rb
saml2-1.1.1 lib/saml2/indexed_object.rb
saml2-1.1.0 lib/saml2/indexed_object.rb