Sha256: 181e28e836a3b159522ac1f0c68d06126c9559871cefdf283add989e3de68d8d

Contents?: true

Size: 1.64 KB

Versions: 50

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

require 'avm/file_formats/base'
require 'eac_fs/file_info'
require 'eac_ruby_utils/core_ext'

module Avm
  module EacGenericBase0
    class FileFormats
      class Base < ::Avm::FileFormats::Base
        class Match
          DEFAULT_TYPE = 'text'

          common_constructor :file_format, :file

          # @return [Boolean]
          def result
            result_by_filename? || result_by_type?
          end

          # @return [Array<String>]
          def valid_basenames
            constant_or_array('VALID_BASENAMES')
          end

          # @return [Array<String>]
          def valid_types
            constant_or_array('VALID_TYPES').map do |mime_type|
              mime_type_sanitize(mime_type)
            end
          end

          protected

          # @return [String]
          def mime_type_sanitize(mime_type)
            if mime_type.split('/').count > 1
              mime_type
            else
              "#{DEFAULT_TYPE}/#{mime_type}"
            end
          end

          private

          # @return [Array<String>]
          def constant_or_array(name)
            return [] unless file_format.class.const_defined?(name)

            file_format.class.const_get(name)
          end

          # @return [Boolean]
          def result_by_filename?
            valid_basenames.any? do |valid_basename|
              file.basename.fnmatch?(valid_basename)
            end
          end

          # @return [Boolean]
          def result_by_type?
            info = ::EacFs::FileInfo.new(file)
            valid_types.include?(info.content_type.mime_type)
          end
        end
      end
    end
  end
end

Version data entries

50 entries across 50 versions & 2 rubygems

Version Path
avm-eac_generic_base0-0.13.0 lib/avm/eac_generic_base0/file_formats/base/match.rb
eac_tools-0.87.0 sub/avm-eac_generic_base0/lib/avm/eac_generic_base0/file_formats/base/match.rb
eac_tools-0.84.2 sub/avm-eac_generic_base0/lib/avm/eac_generic_base0/file_formats/base/match.rb
avm-eac_generic_base0-0.12.2 lib/avm/eac_generic_base0/file_formats/base/match.rb
eac_tools-0.84.1 sub/avm-eac_generic_base0/lib/avm/eac_generic_base0/file_formats/base/match.rb
eac_tools-0.84.0 sub/avm-eac_generic_base0/lib/avm/eac_generic_base0/file_formats/base/match.rb
eac_tools-0.83.0 sub/avm-eac_generic_base0/lib/avm/eac_generic_base0/file_formats/base/match.rb
eac_tools-0.82.0 sub/avm-eac_generic_base0/lib/avm/eac_generic_base0/file_formats/base/match.rb
eac_tools-0.81.0 sub/avm-eac_generic_base0/lib/avm/eac_generic_base0/file_formats/base/match.rb
eac_tools-0.80.0 sub/avm-eac_generic_base0/lib/avm/eac_generic_base0/file_formats/base/match.rb
eac_tools-0.79.0 sub/avm-eac_generic_base0/lib/avm/eac_generic_base0/file_formats/base/match.rb
eac_tools-0.78.0 sub/avm-eac_generic_base0/lib/avm/eac_generic_base0/file_formats/base/match.rb
eac_tools-0.77.1 sub/avm-eac_generic_base0/lib/avm/eac_generic_base0/file_formats/base/match.rb
avm-eac_generic_base0-0.12.1 lib/avm/eac_generic_base0/file_formats/base/match.rb
eac_tools-0.77.0 sub/avm-eac_generic_base0/lib/avm/eac_generic_base0/file_formats/base/match.rb
eac_tools-0.76.1 sub/avm-eac_generic_base0/lib/avm/eac_generic_base0/file_formats/base/match.rb
eac_tools-0.76.0 sub/avm-eac_generic_base0/lib/avm/eac_generic_base0/file_formats/base/match.rb
eac_tools-0.75.2 sub/avm-eac_generic_base0/lib/avm/eac_generic_base0/file_formats/base/match.rb
eac_tools-0.75.1 sub/avm-eac_generic_base0/lib/avm/eac_generic_base0/file_formats/base/match.rb
eac_tools-0.75.0 sub/avm-eac_generic_base0/lib/avm/eac_generic_base0/file_formats/base/match.rb