Sha256: 26a7df9d6dc10b6a613e862f1fb84f141a9525d106730ed1c2be3c2571f15af7

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module Scim
  module Kit
    module V2
      # Represents a dynamic attribute that corresponds to a SCIM type
      module Attributable
        attr_reader :dynamic_attributes

        def define_attributes_for(resource, types)
          @dynamic_attributes ||= {}.with_indifferent_access
          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_for(name)&._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

2 entries across 2 versions & 1 rubygems

Version Path
scim-kit-0.2.14 lib/scim/kit/v2/attributable.rb
scim-kit-0.2.13 lib/scim/kit/v2/attributable.rb