Class: Scimitar::Schema::Attribute

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, Errors
Defined in:
app/models/scimitar/schema/attribute.rb

Overview

Represents an attribute of a SCIM resource that is declared in its schema.

Attributes can be simple or complex. A complex attribute needs to have its own schema that is passed to the initialize method when the attribute is instantiated.

Examples:

Attribute.new(name: 'userName', type: 'string', uniqueness: 'server')
Attribute.new(name: 'name', complexType: Scimitar::ComplexTypes::Name)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Errors

#add_errors_from_hash

Constructor Details

#initialize(options = {}) ⇒ Attribute

options

Hash of values to be used for instantiating the attribute object. Some of the instance variables of the objects will have default values if this hash does not contain anything for them.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/scimitar/schema/attribute.rb', line 37

def initialize(options = {})
  defaults = {
    multiValued:     false,
    required:        false,
    caseExact:       false,
    mutability:      'readWrite',
    uniqueness:      'none',
    returned:        'default',
    canonicalValues: []
  }

  if options[:complexType]
    defaults.merge!(type: 'complex', subAttributes: options[:complexType].schema.scim_attributes)
  end

  super(defaults.merge(options || {}))
end

Instance Attribute Details

#canonicalValuesObject

Returns the value of attribute canonicalValues.



20
21
22
# File 'app/models/scimitar/schema/attribute.rb', line 20

def canonicalValues
  @canonicalValues
end

#caseExactObject

Returns the value of attribute caseExact.



20
21
22
# File 'app/models/scimitar/schema/attribute.rb', line 20

def caseExact
  @caseExact
end

#complexTypeObject

Returns the value of attribute complexType.



20
21
22
# File 'app/models/scimitar/schema/attribute.rb', line 20

def complexType
  @complexType
end

#multiValuedObject

Returns the value of attribute multiValued.



20
21
22
# File 'app/models/scimitar/schema/attribute.rb', line 20

def multiValued
  @multiValued
end

#mutabilityObject

Returns the value of attribute mutability.



20
21
22
# File 'app/models/scimitar/schema/attribute.rb', line 20

def mutability
  @mutability
end

#nameObject

Returns the value of attribute name.



20
21
22
# File 'app/models/scimitar/schema/attribute.rb', line 20

def name
  @name
end

#requiredObject

Returns the value of attribute required.



20
21
22
# File 'app/models/scimitar/schema/attribute.rb', line 20

def required
  @required
end

#returnedObject

Returns the value of attribute returned.



20
21
22
# File 'app/models/scimitar/schema/attribute.rb', line 20

def returned
  @returned
end

#subAttributesObject

Returns the value of attribute subAttributes.



20
21
22
# File 'app/models/scimitar/schema/attribute.rb', line 20

def subAttributes
  @subAttributes
end

#typeObject

Returns the value of attribute type.



20
21
22
# File 'app/models/scimitar/schema/attribute.rb', line 20

def type
  @type
end

#uniquenessObject

Returns the value of attribute uniqueness.



20
21
22
# File 'app/models/scimitar/schema/attribute.rb', line 20

def uniqueness
  @uniqueness
end

Instance Method Details

#all_valid?(complex_type, value) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
# File 'app/models/scimitar/schema/attribute.rb', line 119

def all_valid?(complex_type, value)
  validations = value.map {|value_in_array| valid_complex_type?(value_in_array)}
  validations.all?
end

#as_json(options = {}) ⇒ Object



124
125
126
127
128
# File 'app/models/scimitar/schema/attribute.rb', line 124

def as_json(options = {})
  options[:except] ||= ['complexType']
  options[:except] << 'canonicalValues' if canonicalValues.empty?
  super.except(options)
end

#simple_type?(value) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
# File 'app/models/scimitar/schema/attribute.rb', line 106

def simple_type?(value)
  (type == 'string' && value.is_a?(String)) ||
  (type == 'boolean' && (value.is_a?(TrueClass) || value.is_a?(FalseClass))) ||
  (type == 'integer' && (value.is_a?(Integer))) ||
  (type == 'dateTime' && valid_date_time?(value))
end

#valid?(value) ⇒ Boolean

Validates a value against this attribute object. For simple attributes, it checks if blank is valid or not and if the type matches. For complex attributes, it delegates it to the valid? method of the complex type schema.

If the value is not valid, validation message(s) are added to the #errors attribute of this object.

value

Value to check.

Returns true if value is valid for this attribute, else false.

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
# File 'app/models/scimitar/schema/attribute.rb', line 67

def valid?(value)
  return valid_blank? if value.blank? && !value.is_a?(FalseClass)

  if type == 'complex'
    return all_valid?(complexType, value) if multiValued
    valid_complex_type?(value)
  else
    valid_simple_type?(value)
  end
end

#valid_blank?Boolean

Returns:

  • (Boolean)


78
79
80
81
82
# File 'app/models/scimitar/schema/attribute.rb', line 78

def valid_blank?
  return true unless self.required
  errors.add(self.name, 'is required')
  false
end

#valid_complex_type?(value) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
# File 'app/models/scimitar/schema/attribute.rb', line 84

def valid_complex_type?(value)
  if !value.class.respond_to?(:schema) || value.class.schema != complexType.schema
    errors.add(self.name, 'has to follow the complexType format.')
    return false
  end
  value.class.schema.valid?(value)
  return true if value.errors.empty?
  add_errors_from_hash(errors_hash: value.errors.to_hash, prefix: self.name)
  false
end

#valid_date_time?(value) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
# File 'app/models/scimitar/schema/attribute.rb', line 113

def valid_date_time?(value)
  !!Time.iso8601(value)
rescue ArgumentError
  false
end

#valid_simple_type?(value) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
101
102
103
104
# File 'app/models/scimitar/schema/attribute.rb', line 95

def valid_simple_type?(value)
  if multiValued
    valid = value.is_a?(Array) && value.all? { |v| simple_type?(v) }
    errors.add(self.name, "or one of its elements has the wrong type. It has to be an array of #{self.type}s.") unless valid
  else
    valid = simple_type?(value)
    errors.add(self.name, "has the wrong type. It has to be a(n) #{self.type}.") unless valid
  end
  valid
end