class Scim::Kit::V2::AttributeType

Represents a scim Attribute type

Attributes

attributes[R]
canonical_values[RW]
case_exact[RW]
description[RW]
multi_valued[RW]
mutability[R]
name[R]
reference_types[R]
required[RW]
returned[R]
type[R]
uniqueness[R]

Public Class Methods

from(hash) click to toggle source
# File lib/scim/kit/v2/attribute_type.rb, line 79
def from(hash)
  x = new(name: hash[:name], type: hash[:type])
  %i[
    canonicalValues caseExact description multiValued mutability
    referenceTypes required returned uniqueness
  ].each do |y|
    x.public_send("#{y.to_s.underscore}=", hash[y]) if hash.key?(y)
  end
  x
end
new(name:, type: :string) click to toggle source
# File lib/scim/kit/v2/attribute_type.rb, line 14
def initialize(name:, type: :string)
  @name = name.to_s.underscore
  @type = DATATYPES[type.to_sym] ? type.to_sym : (raise TYPE_ERROR)
  @description = name.to_s.camelize(:lower)
  @multi_valued = false
  @required = false
  @case_exact = false
  @mutability = Mutability::READ_WRITE
  @returned = Returned::DEFAULT
  @uniqueness = Uniqueness::NONE
  @attributes = []
end

Public Instance Methods

add_attribute(name:, type: :string) { |attribute| ... } click to toggle source
# File lib/scim/kit/v2/attribute_type.rb, line 39
def add_attribute(name:, type: :string)
  attribute = AttributeType.new(name: name, type: type)
  yield attribute if block_given?
  @type = :complex
  attributes << attribute
end
coerce(value) click to toggle source
# File lib/scim/kit/v2/attribute_type.rb, line 55
def coerce(value)
  return value if value.nil?
  return value if complex?

  if multi_valued
    return value unless value.respond_to?(:to_a)

    value.to_a.map { |x| coerce_single(x) }
  else
    coerce_single(value)
  end
end
complex?() click to toggle source
# File lib/scim/kit/v2/attribute_type.rb, line 51
def complex?
  type_is?(:complex)
end
mutability=(value) click to toggle source
# File lib/scim/kit/v2/attribute_type.rb, line 27
def mutability=(value)
  @mutability = Mutability.find(value)
end
reference_types=(value) click to toggle source
# File lib/scim/kit/v2/attribute_type.rb, line 46
def reference_types=(value)
  @type = :reference
  @reference_types = value
end
returned=(value) click to toggle source
# File lib/scim/kit/v2/attribute_type.rb, line 31
def returned=(value)
  @returned = Returned.find(value)
end
uniqueness=(value) click to toggle source
# File lib/scim/kit/v2/attribute_type.rb, line 35
def uniqueness=(value)
  @uniqueness = Uniqueness.find(value)
end
valid?(value) click to toggle source
# File lib/scim/kit/v2/attribute_type.rb, line 68
def valid?(value)
  if multi_valued
    return false unless value.respond_to?(:to_a)

    return value.to_a.all? { |x| validate(x) }
  end

  complex? ? valid_complex?(value) : valid_simple?(value)
end

Private Instance Methods

coerce_single(value) click to toggle source
# File lib/scim/kit/v2/attribute_type.rb, line 93
def coerce_single(value)
  COERCION.fetch(type, ->(x) { x }).call(value)
rescue StandardError => error
  Scim::Kit.logger.error(error)
  value
end
reference?() click to toggle source
# File lib/scim/kit/v2/attribute_type.rb, line 125
def reference?
  type_is?(:reference)
end
string?() click to toggle source
# File lib/scim/kit/v2/attribute_type.rb, line 121
def string?
  type_is?(:string)
end
type_for(name) click to toggle source
# File lib/scim/kit/v2/attribute_type.rb, line 116
def type_for(name)
  name = name.to_s.underscore
  attributes.find { |x| x.name.to_s.underscore == name }
end
type_is?(expected_type) click to toggle source
# File lib/scim/kit/v2/attribute_type.rb, line 129
def type_is?(expected_type)
  type.to_sym == expected_type
end
valid_complex?(item) click to toggle source
# File lib/scim/kit/v2/attribute_type.rb, line 108
def valid_complex?(item)
  return false unless item.is_a?(Hash)

  item.each_key do |key|
    return false unless type_for(key)&.valid?(item[key])
  end
end
valid_simple?(value) click to toggle source
# File lib/scim/kit/v2/attribute_type.rb, line 104
def valid_simple?(value)
  VALIDATIONS[type]&.call(value)
end
validate(value) click to toggle source
# File lib/scim/kit/v2/attribute_type.rb, line 100
def validate(value)
  complex? ? valid_complex?(value) : valid_simple?(value)
end