Sha256: d916295892308795017225a81faa1b504103e327ac58703fcadfcd6ada0776c0

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module Scim
  module Kit
    module V2
      # Represents a SCIM Resource
      class Resource
        include ::ActiveModel::Validations
        include Attributable
        include Templatable

        attr_reader :meta
        attr_reader :schemas
        attr_reader :raw_attributes

        validate :schema_validations

        def initialize(schemas:, location: nil, attributes: {})
          @meta = Meta.new(schemas[0]&.name, location)
          @meta.disable_timestamps
          @schemas = schemas
          @raw_attributes = attributes
          schemas.each { |x| define_attributes_for(self, x.attributes) }
          attribute(AttributeType.new(name: :id), self)
          attribute(AttributeType.new(name: :external_id), self)
          assign_attributes(attributes)
          yield self if block_given?
        end

        def mode?(type)
          case type.to_sym
          when :server
            meta&.location
          else
            meta&.location.nil?
          end
        end

        def template_name
          'resource.json.jbuilder'
        end

        private

        def schema_validations
          schemas.each do |schema|
            schema.attributes.each do |type|
              validate_attribute(type)
            end
          end
        end

        def validate_attribute(type)
          attribute = attribute_for(type.name)
          errors.copy!(attribute.errors) unless attribute.valid?
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scim-kit-0.2.15 lib/scim/kit/v2/resource.rb