Class: ScimEngine::Schema::Attribute

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, Errors
Defined in:
app/models/scim_engine/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 initilize method when the attribute is instantiated.

Examples:

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Errors

#add_errors_from_hash

Constructor Details

#initialize(options = {}) ⇒ Attribute

Returns a new instance of Attribute.

Parameters:

  • options (Hash) (defaults to: {})

    a 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.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/scim_engine/schema/attribute.rb', line 14

def initialize(options = {})
  defaults = {
    multiValued: false,
    required: true,
    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.



11
12
13
# File 'app/models/scim_engine/schema/attribute.rb', line 11

def canonicalValues
  @canonicalValues
end

#caseExactObject

Returns the value of attribute caseExact.



11
12
13
# File 'app/models/scim_engine/schema/attribute.rb', line 11

def caseExact
  @caseExact
end

#complexTypeObject

Returns the value of attribute complexType.



11
12
13
# File 'app/models/scim_engine/schema/attribute.rb', line 11

def complexType
  @complexType
end

#multiValuedObject

Returns the value of attribute multiValued.



11
12
13
# File 'app/models/scim_engine/schema/attribute.rb', line 11

def multiValued
  @multiValued
end

#mutabilityObject

Returns the value of attribute mutability.



11
12
13
# File 'app/models/scim_engine/schema/attribute.rb', line 11

def mutability
  @mutability
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'app/models/scim_engine/schema/attribute.rb', line 11

def name
  @name
end

#requiredObject

Returns the value of attribute required.



11
12
13
# File 'app/models/scim_engine/schema/attribute.rb', line 11

def required
  @required
end

#returnedObject

Returns the value of attribute returned.



11
12
13
# File 'app/models/scim_engine/schema/attribute.rb', line 11

def returned
  @returned
end

#subAttributesObject

Returns the value of attribute subAttributes.



11
12
13
# File 'app/models/scim_engine/schema/attribute.rb', line 11

def subAttributes
  @subAttributes
end

#typeObject

Returns the value of attribute type.



11
12
13
# File 'app/models/scim_engine/schema/attribute.rb', line 11

def type
  @type
end

#uniquenessObject

Returns the value of attribute uniqueness.



11
12
13
# File 'app/models/scim_engine/schema/attribute.rb', line 11

def uniqueness
  @uniqueness
end

Instance Method Details

#all_valid?(complex_type, value) ⇒ Boolean

Returns:

  • (Boolean)


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

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



83
84
85
86
87
# File 'app/models/scim_engine/schema/attribute.rb', line 83

def as_json(options = {})
  options[:except] ||= ['complexType']
  options[:except] << 'canonicalValues' if canonicalValues.empty?
  super.except(options)
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.

Returns:

  • (Boolean)

    whether or not the value is valid for this attribute



35
36
37
38
39
40
41
42
43
44
# File 'app/models/scim_engine/schema/attribute.rb', line 35

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)


46
47
48
49
50
# File 'app/models/scim_engine/schema/attribute.rb', line 46

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

#valid_complex_type?(value) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
# File 'app/models/scim_engine/schema/attribute.rb', line 52

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(value.errors.to_hash, prefix: self.name)
  false
end

#valid_date_time?(value) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'app/models/scim_engine/schema/attribute.rb', line 72

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

#valid_simple_type?(value) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
# File 'app/models/scim_engine/schema/attribute.rb', line 63

def valid_simple_type?(value)
  valid = (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))
  errors.add(self.name, "has the wrong type. It has to be a(n) #{self.type}.") unless valid
  valid
end