Class: Scim::Kit::V2::AttributeType

Inherits:
Object
  • Object
show all
Includes:
Templatable
Defined in:
lib/scim/kit/v2/attribute_type.rb

Overview

Represents a scim Attribute type

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Templatable

#as_json, #render, #template_name, #to_h, #to_json

Constructor Details

#initialize(name:, type: :string) ⇒ AttributeType

Returns a new instance of AttributeType.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/scim/kit/v2/attribute_type.rb', line 20

def initialize(name:, type: :string)
  @name = name.to_s.underscore
  @type = type.to_sym
  @description = name
  @multi_valued = false
  @required = false
  @case_exact = false
  @mutability = Mutability::READ_WRITE
  @returned = Returned::DEFAULT
  @uniqueness = Uniqueness::NONE
  raise ArgumentError, :type unless DATATYPES[@type]
end

Instance Attribute Details

#canonical_valuesObject

Returns the value of attribute canonical_values.



9
10
11
# File 'lib/scim/kit/v2/attribute_type.rb', line 9

def canonical_values
  @canonical_values
end

#case_exactObject

Returns the value of attribute case_exact.



10
11
12
# File 'lib/scim/kit/v2/attribute_type.rb', line 10

def case_exact
  @case_exact
end

#descriptionObject

Returns the value of attribute description.



11
12
13
# File 'lib/scim/kit/v2/attribute_type.rb', line 11

def description
  @description
end

#multi_valuedObject

Returns the value of attribute multi_valued.



12
13
14
# File 'lib/scim/kit/v2/attribute_type.rb', line 12

def multi_valued
  @multi_valued
end

#mutabilityObject

Returns the value of attribute mutability.



14
15
16
# File 'lib/scim/kit/v2/attribute_type.rb', line 14

def mutability
  @mutability
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/scim/kit/v2/attribute_type.rb', line 15

def name
  @name
end

#reference_typesObject

Returns the value of attribute reference_types.



16
17
18
# File 'lib/scim/kit/v2/attribute_type.rb', line 16

def reference_types
  @reference_types
end

#requiredObject

Returns the value of attribute required.



13
14
15
# File 'lib/scim/kit/v2/attribute_type.rb', line 13

def required
  @required
end

#returnedObject

Returns the value of attribute returned.



17
18
19
# File 'lib/scim/kit/v2/attribute_type.rb', line 17

def returned
  @returned
end

#typeObject (readonly)

Returns the value of attribute type.



15
16
17
# File 'lib/scim/kit/v2/attribute_type.rb', line 15

def type
  @type
end

#uniquenessObject

Returns the value of attribute uniqueness.



18
19
20
# File 'lib/scim/kit/v2/attribute_type.rb', line 18

def uniqueness
  @uniqueness
end

Instance Method Details

#add_attribute(name:, type: :string) {|attribute| ... } ⇒ Object

Yields:

  • (attribute)


45
46
47
48
49
50
# File 'lib/scim/kit/v2/attribute_type.rb', line 45

def add_attribute(name:, type: :string)
  attribute = AttributeType.new(name: name, type: type)
  yield attribute if block_given?
  @type = :complex
  attributes << attribute
end

#attributesObject



57
58
59
# File 'lib/scim/kit/v2/attribute_type.rb', line 57

def attributes
  @attributes ||= []
end

#coerce(value) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/scim/kit/v2/attribute_type.rb', line 65

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 do |x|
      COERCION.fetch(type, ->(y) { y }).call(x)
    end
  else
    COERCION.fetch(type, ->(x) { x }).call(value)
  end
end

#complex?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/scim/kit/v2/attribute_type.rb', line 61

def complex?
  type_is?(:complex)
end

#valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
# File 'lib/scim/kit/v2/attribute_type.rb', line 80

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