Sha256: 5c20b0c07e161e445e08e8139b9c1cccc0bf7fa4c3e80c84a0e1cd31345f83d2

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

module Scim
  module Kit
    module V2
      # Represents a dynamic attribute that corresponds to a SCIM type
      module Attributable
        def dynamic_attributes
          @dynamic_attributes ||= {}.with_indifferent_access
        end

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

        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

        private

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

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

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

        def write_attribute(name, value)
          if value.is_a?(Hash)
            attribute_for(name)&.assign_attributes(value)
          else
            attribute = attribute_for(name)
            raise Scim::Kit::UnknownAttributeError, name unless attribute

            attribute._value = value
          end
        end

        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

        def attribute(type, resource)
          dynamic_attributes[type.name] = Attribute.new(
            type: type,
            resource: resource
          )
          extend(create_module_for(type))
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scim-kit-0.2.15 lib/scim/kit/v2/attributable.rb