Module: Scim::Kit::V2::Attributable

Includes:
Enumerable
Included in:
Attribute, Resource
Defined in:
lib/scim/kit/v2/attributable.rb

Overview

Represents a dynamic attribute that corresponds to a SCIM type

Instance Method Summary collapse

Instance Method Details

#assign_attributes(attributes = {}) ⇒ Object

Assigns attribute values via the provided hash.

Parameters:

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

    The name/values to assign.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/scim/kit/v2/attributable.rb', line 25

def assign_attributes(attributes = {})
  attributes.each do |key, value|
    next if key.to_sym == :schemas

    if key.to_s.start_with?(Schemas::EXTENSION)
      assign_attributes(value)
    else
      write_attribute(key, value)
    end
  end
end

#attribute_for(name) ⇒ Scim::Kit::V2::Attribute

Returns the attribute identified by the name.

Parameters:

  • name (String)

    the name of the attribute to return

Returns:



40
41
42
43
44
# File 'lib/scim/kit/v2/attributable.rb', line 40

def attribute_for(name)
  dynamic_attributes[name.to_s.underscore] ||
    dynamic_attributes[name] ||
    UnknownAttribute.new(name)
end

#define_attributes_for(resource, types) ⇒ Object

Defines dynamic attributes on the resource for the types provided

Parameters:



19
20
21
# File 'lib/scim/kit/v2/attributable.rb', line 19

def define_attributes_for(resource, types)
  types.each { |x| attribute(x, resource) }
end

#dynamic_attributesHash

Returns a hash of the generated dynamic attributes

Returns:

  • (Hash)

    the dynamic attributes keys by their name



12
13
14
# File 'lib/scim/kit/v2/attributable.rb', line 12

def dynamic_attributes
  @dynamic_attributes ||= {}.with_indifferent_access
end

#each(&block) ⇒ Object

yields each attribute to the provided block

Parameters:

  • the (Block)

    block to yield each attribute to.



69
70
71
# File 'lib/scim/kit/v2/attributable.rb', line 69

def each(&block)
  dynamic_attributes.each_value(&block)
end

#read_attribute(name) ⇒ Object

Returns the value associated with the attribute name

Parameters:

  • name (String)

    the name of the attribute

Returns:

  • (Object)

    the value assigned to the attribute



49
50
51
52
53
54
# File 'lib/scim/kit/v2/attributable.rb', line 49

def read_attribute(name)
  attribute = attribute_for(name)
  return attribute._value if attribute._type.multi_valued

  attribute._type.complex? ? attribute : attribute._value
end

#write_attribute(name, value) ⇒ Object

Assigns the value to the attribute with the given name

Parameters:

  • name (String)

    the name of the attribute

  • value (Object)

    the value to assign to the attribute



59
60
61
62
63
64
65
# File 'lib/scim/kit/v2/attributable.rb', line 59

def write_attribute(name, value)
  if value.is_a?(Hash)
    attribute_for(name)&.assign_attributes(value)
  else
    attribute_for(name)._value = value
  end
end