module Scim::Kit::V2::Attributable

Represents a dynamic attribute that corresponds to a SCIM type

Public Instance Methods

assign_attributes(attributes = {}) click to toggle source

Assigns attribute values via the provided hash. @param attributes [Hash] The name/values to assign.

# 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) click to toggle source

Returns the attribute identified by the name. @param name [String] the name of the attribute to return @return [Scim::Kit::V2::Attribute] the attribute or {Scim::Kit::V2::UnknownAttribute}

# File lib/scim/kit/v2/attributable.rb, line 40
def attribute_for(name)
  dynamic_attributes[name.to_s.underscore] || UnknownAttribute.new(name)
end
define_attributes_for(resource, types) click to toggle source

Defines dynamic attributes on the resource for the types provided @param resource [Scim::Kit::V2::Resource] the resource to attach dynamic attributes to. @param types [Array<Scim::Kit::V2::AttributeType>] the array of types

# File lib/scim/kit/v2/attributable.rb, line 19
def define_attributes_for(resource, types)
  types.each { |x| attribute(x, resource) }
end
dynamic_attributes() click to toggle source

Returns a hash of the generated dynamic attributes @return [Hash] the dynamic attributes keys by their name

# File lib/scim/kit/v2/attributable.rb, line 12
def dynamic_attributes
  @dynamic_attributes ||= {}.with_indifferent_access
end
each() { |attribute| ... } click to toggle source

yields each attribute to the provided block @param [Block] the block to yield each attribute to.

# File lib/scim/kit/v2/attributable.rb, line 67
def each
  dynamic_attributes.each do |_name, attribute|
    yield attribute
  end
end
read_attribute(name) click to toggle source

Returns the value associated with the attribute name @param name [String] the name of the attribute @return [Object] the value assigned to the attribute

# File lib/scim/kit/v2/attributable.rb, line 47
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) click to toggle source

Assigns the value to the attribute with the given name @param name [String] the name of the attribute @param value [Object] the value to assign to the attribute

# File lib/scim/kit/v2/attributable.rb, line 57
def write_attribute(name, value)
  if value.is_a?(Hash)
    attribute_for(name)&.assign_attributes(value)
  else
    attribute_for(name)._value = value
  end
end

Private Instance Methods

attribute(type, resource) click to toggle source
# File lib/scim/kit/v2/attributable.rb, line 88
def attribute(type, resource)
  dynamic_attributes[type.name] = Attribute.new(
    type: type,
    resource: resource
  )
  extend(create_module_for(type))
end
create_module_for(type) click to toggle source
# File lib/scim/kit/v2/attributable.rb, line 75
def create_module_for(type)
  name = type.name.to_sym
  Module.new do
    define_method(name) do |*_args|
      read_attribute(name)
    end

    define_method("#{name}=") do |*args|
      write_attribute(name, args[0])
    end
  end
end