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

Returns a new instance of AttributeType.



14
15
16
17
18
19
20
21
22
23
24
25
# 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

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

#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



79
80
81
82
83
84
85
86
87
88
# 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

Instance Method Details

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

Yields:

  • (attribute)


39
40
41
42
43
44
# 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) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# 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?Boolean

Returns:

  • (Boolean)


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

def complex?
  type_is?(:complex)
end

#valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
# 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