Sha256: 39e593d6a507f730f0a5da4daee5fbcc0887afac0416217fc1c4c1503e831bbc

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

require 'rom/files/constants'
require 'rom/files/types'
require 'mime/types/full'

module ROM
  module Files
    module Plugins
      module Schema
        # A plugin for obtaining MIME-type of file using its name
        #
        # @example Generic `mime_type` field
        #   schema do
        #     use :mime_type
        #   end
        #
        # @api public
        module Mime
          PROC = ->(path) { MIME::Types.type_for(path.basename.to_s).first }
          TYPE = Types::MimeType.optional

          # @api private
          def self.apply(schema, name: :mime_type, type: TYPE)
            mime_type = type.meta(
              name: name,
              __proc__: PROC,
              source: schema.name
            )

            schema.attributes.concat(
              schema.class.attributes([mime_type], schema.attr_class)
            )
          end

          # @api private
          module DSL
            # Sets non-default contents attribute
            #
            # @example Set custom attribute name `#type` for MIME-type
            #   schema do
            #     use :mime
            #     mime :type
            #   end
            #
            # @api public
            def mime(name = :mime_type, inline_type = TYPE, type: inline_type)
              options = plugin_options(:mime)
              options[:name] = name
              options[:type] = type

              self
            end
          end
        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/plugins/schema/mime.rb