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

Class Method 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, schema: nil) ⇒ AttributeType

Returns a new instance of AttributeType.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/scim/kit/v2/attribute_type.rb', line 14

def initialize(name:, type: :string, schema: nil)
  @name = name.to_s.underscore
  @fully_qualified_name = [schema&.id, @name].compact.join('#')
  @type = DATATYPES[type.to_sym] ? type.to_sym : (raise TYPE_ERROR)
  @description = name.to_s.camelize(:lower)
  @multi_valued = @required = @case_exact = false
  @mutability = Mutability::READ_WRITE
  @returned = Returned::DEFAULT
  @uniqueness = Uniqueness::NONE
  @attributes = []
end

Instance Attribute Details

#attributesObject (readonly)



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

def attributes
  @attributes
end

#canonical_valuesObject



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

def canonical_values
  @canonical_values
end

#case_exactObject



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

def case_exact
  @case_exact
end

#descriptionObject



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

def description
  @description
end

#fully_qualified_nameObject (readonly)



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

def fully_qualified_name
  @fully_qualified_name
end

#multi_valuedObject



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

def multi_valued
  @multi_valued
end

#mutabilityObject



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

def mutability
  @mutability
end

#nameObject (readonly)



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

def name
  @name
end

#reference_typesObject



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

def reference_types
  @reference_types
end

#requiredObject



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

def required
  @required
end

#returnedObject



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

def returned
  @returned
end

#typeObject (readonly)



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

def type
  @type
end

#uniquenessObject



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

def uniqueness
  @uniqueness
end

Class Method Details

.from(hash) ⇒ Object



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

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

Instance Method Details

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

Yields:

  • (attribute)


38
39
40
41
42
43
# File 'lib/scim/kit/v2/attribute_type.rb', line 38

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) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/scim/kit/v2/attribute_type.rb', line 54

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?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/scim/kit/v2/attribute_type.rb', line 50

def complex?
  type_is?(:complex)
end

#valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

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