Class: Scimitar::Schema::Base

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

Overview

The base class that each schema class must inherit from. These classes represent the schema of a SCIM resource or a complex type that could be used in a resource.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



9
10
11
12
# File 'app/models/scimitar/schema/base.rb', line 9

def initialize(options = {})
  super
  @meta = Meta.new(resourceType: 'Schema')
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



7
8
9
# File 'app/models/scimitar/schema/base.rb', line 7

def description
  @description
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'app/models/scimitar/schema/base.rb', line 7

def id
  @id
end

#metaObject

Returns the value of attribute meta.



7
8
9
# File 'app/models/scimitar/schema/base.rb', line 7

def meta
  @meta
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'app/models/scimitar/schema/base.rb', line 7

def name
  @name
end

#scim_attributesObject

Returns the value of attribute scim_attributes.



7
8
9
# File 'app/models/scimitar/schema/base.rb', line 7

def scim_attributes
  @scim_attributes
end

Class Method Details

.cloned_scim_attributesObject



35
36
37
# File 'app/models/scimitar/schema/base.rb', line 35

def self.cloned_scim_attributes
  scim_attributes.map { |scim_attribute| scim_attribute.clone }
end

.find_attribute(*path) ⇒ Object

Find a given attribute this schema, travelling down a path to any sub-attributes within. Given that callers might be dealing with paths into actual SCIM data, array indices for multi-value attributes are allowed (as integers) and simply skipped - only the names are of interest.

This is typically used to access attribute properties such as intended mutability (‘readOnly’, ‘readWrite’, ‘immutable’, ‘writeOnly’).

Returns the found Scimitar::Schema::Attribute or “nil”.

*path

One or more attribute names as Strings, or Integer indices.

For example, in a User schema, passing “name”, “givenName” would find the “givenName” attribute. Passing “emails”, 0, “value” would find the schema attribute for “value” under “emails”, ignoring the array index (since the schema is identical for each item in an array of values).

See also Scimitar::Resources::Base::find_attribute



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/scimitar/schema/base.rb', line 59

def self.find_attribute(*path)
  found_attribute    = nil
  current_attributes = self.scim_attributes()

  until path.empty? do
    current_path_entry = path.shift()
    next if current_path_entry.is_a?(Integer) # Skip array indicies arising from multi-value attributes

    current_path_entry = current_path_entry.to_s.downcase

    found_attribute = current_attributes.find do | attribute_to_check |
      attribute_to_check.name.to_s.downcase == current_path_entry
    end

    if found_attribute && path.present? # Any sub-attributes to check?...
      if found_attribute.subAttributes.present? # ...and are any defined?
        current_attributes = found_attribute.subAttributes
      else
        found_attribute = nil
        break # NOTE EARLY EXIT - tried to find a sub-attribute but there are none
      end
    else
      break # NOTE EARLY EXIT - no found attribute, or found target item at end of path
    end
  end # "until path.empty() do"

  return found_attribute
end

.valid?(resource) ⇒ Boolean

Validates the resource against specific validations of each attribute, for example if the type of the attribute matches the one defined in the schema.

resource

A resource object that uses this schema.

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'app/models/scimitar/schema/base.rb', line 27

def self.valid?(resource)
  cloned_scim_attributes.each do |scim_attribute|
    unless scim_attribute.valid?(resource.send(scim_attribute.name))
      resource.add_errors_from_hash(errors_hash: scim_attribute.errors.to_hash)
    end
  end
end

Instance Method Details

#as_json(options = {}) ⇒ Object

Converts the schema to its json representation that will be returned by /SCHEMAS end-point of a SCIM service provider.



15
16
17
18
19
# File 'app/models/scimitar/schema/base.rb', line 15

def as_json(options = {})
  @meta.location ||= Scimitar::Engine.routes.url_helpers.scim_schemas_path(name: id)
  original = super
  original.merge('attributes' => original.delete('scim_attributes'))
end