Sha256: 9479eeed57c863661c0acdb4fd4e265758853b0622bab534b031efba319c0abb

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module Scim
  module Kit
    module V2
      # Represents a SCIM Attribute
      class Attribute
        include ::ActiveModel::Validations
        include Attributable
        include Templatable
        attr_reader :type
        attr_reader :_value

        validate :presence_of_value, if: proc { |x| x.type.required }
        validate :inclusion_of_value, if: proc { |x| x.type.canonical_values }
        validate :validate_type

        def initialize(type:, value: nil)
          @type = type
          @_value = value
          define_attributes_for(type.attributes)
        end

        def _assign(new_value, coerce: true)
          @_value = coerce ? type.coerce(new_value) : new_value
        end

        def _value=(new_value)
          _assign(new_value, coerce: true)
        end

        private

        def presence_of_value
          return unless type.required && _value.blank?

          errors.add(type.name, I18n.t('errors.messages.blank'))
        end

        def inclusion_of_value
          return if type.canonical_values.include?(_value)

          errors.add(type.name, I18n.t('errors.messages.inclusion'))
        end

        def validate_type
          return if type.valid?(_value)

          errors.add(type.name, I18n.t('errors.messages.invalid'))
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
scim-kit-0.2.9 lib/scim/kit/v2/attribute.rb
scim-kit-0.2.8 lib/scim/kit/v2/attribute.rb
scim-kit-0.2.7 lib/scim/kit/v2/attribute.rb