Class: ScimEngine::Schema::Attribute
- Inherits:
-
Object
- Object
- ScimEngine::Schema::Attribute
- 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.
Instance Attribute Summary collapse
-
#canonicalValues ⇒ Object
Returns the value of attribute canonicalValues.
-
#caseExact ⇒ Object
Returns the value of attribute caseExact.
-
#complexType ⇒ Object
Returns the value of attribute complexType.
-
#multiValued ⇒ Object
Returns the value of attribute multiValued.
-
#mutability ⇒ Object
Returns the value of attribute mutability.
-
#name ⇒ Object
Returns the value of attribute name.
-
#required ⇒ Object
Returns the value of attribute required.
-
#returned ⇒ Object
Returns the value of attribute returned.
-
#subAttributes ⇒ Object
Returns the value of attribute subAttributes.
-
#type ⇒ Object
Returns the value of attribute type.
-
#uniqueness ⇒ Object
Returns the value of attribute uniqueness.
Instance Method Summary collapse
- #all_valid?(complex_type, value) ⇒ Boolean
- #as_json(options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
-
#valid?(value) ⇒ Boolean
Validates a value against this attribute object.
- #valid_blank? ⇒ Boolean
- #valid_complex_type?(value) ⇒ Boolean
- #valid_date_time?(value) ⇒ Boolean
- #valid_simple_type?(value) ⇒ Boolean
Methods included from Errors
Constructor Details
#initialize(options = {}) ⇒ Attribute
Returns a new instance of Attribute.
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( = {}) defaults = { multiValued: false, required: true, caseExact: false, mutability: 'readWrite', uniqueness: 'none', returned: 'default', canonicalValues: [] } if [:complexType] defaults.merge!(type: 'complex', subAttributes: [:complexType].schema.scim_attributes) end super(defaults.merge( || {})) end |
Instance Attribute Details
#canonicalValues ⇒ Object
Returns the value of attribute canonicalValues.
11 12 13 |
# File 'app/models/scim_engine/schema/attribute.rb', line 11 def canonicalValues @canonicalValues end |
#caseExact ⇒ Object
Returns the value of attribute caseExact.
11 12 13 |
# File 'app/models/scim_engine/schema/attribute.rb', line 11 def caseExact @caseExact end |
#complexType ⇒ Object
Returns the value of attribute complexType.
11 12 13 |
# File 'app/models/scim_engine/schema/attribute.rb', line 11 def complexType @complexType end |
#multiValued ⇒ Object
Returns the value of attribute multiValued.
11 12 13 |
# File 'app/models/scim_engine/schema/attribute.rb', line 11 def multiValued @multiValued end |
#mutability ⇒ Object
Returns the value of attribute mutability.
11 12 13 |
# File 'app/models/scim_engine/schema/attribute.rb', line 11 def mutability @mutability end |
#name ⇒ Object
Returns the value of attribute name.
11 12 13 |
# File 'app/models/scim_engine/schema/attribute.rb', line 11 def name @name end |
#required ⇒ Object
Returns the value of attribute required.
11 12 13 |
# File 'app/models/scim_engine/schema/attribute.rb', line 11 def required @required end |
#returned ⇒ Object
Returns the value of attribute returned.
11 12 13 |
# File 'app/models/scim_engine/schema/attribute.rb', line 11 def returned @returned end |
#subAttributes ⇒ Object
Returns the value of attribute subAttributes.
11 12 13 |
# File 'app/models/scim_engine/schema/attribute.rb', line 11 def subAttributes @subAttributes end |
#type ⇒ Object
Returns the value of attribute type.
11 12 13 |
# File 'app/models/scim_engine/schema/attribute.rb', line 11 def type @type end |
#uniqueness ⇒ Object
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
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( = {}) [:except] ||= ['complexType'] [:except] << 'canonicalValues' if canonicalValues.empty? super.except() 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.
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
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
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
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
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 |