Sha256: bc02c0ce290cf42522c26cbe1443327396b490021b906480ab19ad55c4d22136

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

require 'dry/core/class_attributes'
require 'rom/files/constants'
require 'rom/files/attribute'
require 'mime/types/full'

module ROM
  module Files
    class Schema < ROM::Schema
      # @api private
      class AttributesInferrer
        extend Dry::Core::ClassAttributes
        extend Initializer

        KNOWN_COLUMNS = [ID].freeze

        # @!method self.registry
        #   @return [Hash{String => AttributesInferrer}]
        defines :registry, :attr_class
        registry Hash.new(new.freeze)
        attr_class Files::Attribute

        # @param type [String]
        # @param inferrer [AttributesInferrer]
        # @return [AttributesInferrer]
        def self.register(type, inferrer)
          registry[type] = inferrer
        end

        def self.registered?(type)
          registry.key?(type)
        end

        # @param mime_type [String]
        # @return [AttributesInferrer]
        def self.[](mime_type)
          registry[mime_type]
        end

        # @!attribute [r] attr_class
        #   @return [Class(Files::Attribute)]
        option :attr_class, default: -> { self.class.attr_class }

        # @param schema [ROM::Files::Schema]
        # @param gateway [ROM::Files::Gateway]
        #
        # @api private
        def call(schema, gateway)
          inferred = infer_attributes(schema, gateway)

          missing = columns - inferred.map { |attr| attr.meta[:name] }

          [inferred, missing]
        end

        undef :with

        # @return [Files::Attribute]
        def build(type, name, schema)
          attr_class.new(type.meta(name: name, source: schema.name))
        end

        # @return [Array<Symbol>]
        def columns
          KNOWN_COLUMNS
        end

        def with(new_options)
          self.class.new(options.merge(new_options))
        end

        def infer_attributes(schema, _gateway)
          [build(Types::Path, ID, schema)]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-files-0.2.0 lib/rom/files/schema/attributes_inferrer.rb