Class: Scimitar::Schema::Attribute
- Inherits:
-
Object
- Object
- Scimitar::Schema::Attribute
- 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
-
#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
options
-
Hash of values to be used for instantiating the attribute object.
- #simple_type?(value) ⇒ Boolean
-
#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
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( = {}) defaults = { multiValued: false, required: false, 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.
20 21 22 |
# File 'app/models/scimitar/schema/attribute.rb', line 20 def canonicalValues @canonicalValues end |
#caseExact ⇒ Object
Returns the value of attribute caseExact.
20 21 22 |
# File 'app/models/scimitar/schema/attribute.rb', line 20 def caseExact @caseExact end |
#complexType ⇒ Object
Returns the value of attribute complexType.
20 21 22 |
# File 'app/models/scimitar/schema/attribute.rb', line 20 def complexType @complexType end |
#multiValued ⇒ Object
Returns the value of attribute multiValued.
20 21 22 |
# File 'app/models/scimitar/schema/attribute.rb', line 20 def multiValued @multiValued end |
#mutability ⇒ Object
Returns the value of attribute mutability.
20 21 22 |
# File 'app/models/scimitar/schema/attribute.rb', line 20 def mutability @mutability end |
#name ⇒ Object
Returns the value of attribute name.
20 21 22 |
# File 'app/models/scimitar/schema/attribute.rb', line 20 def name @name end |
#required ⇒ Object
Returns the value of attribute required.
20 21 22 |
# File 'app/models/scimitar/schema/attribute.rb', line 20 def required @required end |
#returned ⇒ Object
Returns the value of attribute returned.
20 21 22 |
# File 'app/models/scimitar/schema/attribute.rb', line 20 def returned @returned end |
#subAttributes ⇒ Object
Returns the value of attribute subAttributes.
20 21 22 |
# File 'app/models/scimitar/schema/attribute.rb', line 20 def subAttributes @subAttributes end |
#type ⇒ Object
Returns the value of attribute type.
20 21 22 |
# File 'app/models/scimitar/schema/attribute.rb', line 20 def type @type end |
#uniqueness ⇒ Object
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
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( = {}) [:except] ||= ['complexType'] [:except] << 'canonicalValues' if canonicalValues.empty? super.except() end |
#simple_type?(value) ⇒ 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
.
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
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
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
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
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 |