Sha256: 64848bac16c4c67ac9a6c6dceb1009065dbd751373ed370b02cd89f2e37bfffe

Contents?: true

Size: 1.59 KB

Versions: 20

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

require 'avm/file_formats/utf8_assert'
require 'eac_fs/file_info'
require 'ostruct'

module Avm
  module FileFormats
    class Base
      enable_abstract_methods
      compare_by :class

      def apply(files)
        old_content = Hash[files.map { |f| [f, File.read(f)] }]
        ::Avm::FileFormats::Utf8Assert.assert_files(files) { internal_apply(files) }
        files.map { |f| build_file_result(f, old_content[f]) }
      end

      # @param path [Pathname]
      # @return [Avm::FileFormats::FileWith]
      def file_resource_name(path)
        path.to_pathname.to_path
      end

      def name
        self.class.name.demodulize
      end

      def match?(file)
        match_by_filename?(file) || match_by_type?(file)
      end

      def valid_basenames
        constant_or_array('VALID_BASENAMES')
      end

      def valid_types
        constant_or_array('VALID_TYPES')
      end

      private

      def constant_or_array(name)
        return [] unless self.class.const_defined?(name)

        self.class.const_get(name)
      end

      def build_file_result(file, old_content)
        ::OpenStruct.new(file: file, format: self.class,
                         changed: (old_content != File.read(file)))
      end

      def match_by_filename?(file)
        valid_basenames.any? do |valid_basename|
          file.basename.fnmatch?(valid_basename)
        end
      end

      def match_by_type?(file)
        info = ::EacFs::FileInfo.new(file)
        return unless info.content_type.type == 'text'

        valid_types.include?(info.content_type.subtype)
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
avm-0.65.0 lib/avm/file_formats/base.rb
eac_tools-0.55.7 sub/avm/lib/avm/file_formats/base.rb
avm-0.64.2 lib/avm/file_formats/base.rb
eac_tools-0.55.6 sub/avm/lib/avm/file_formats/base.rb
avm-0.64.1 lib/avm/file_formats/base.rb
eac_tools-0.55.5 sub/avm/lib/avm/file_formats/base.rb
avm-0.64.0 lib/avm/file_formats/base.rb
eac_tools-0.55.4 sub/avm/lib/avm/file_formats/base.rb
avm-0.63.0 lib/avm/file_formats/base.rb
eac_tools-0.55.3 sub/avm/lib/avm/file_formats/base.rb
eac_tools-0.55.2 sub/avm/lib/avm/file_formats/base.rb
eac_tools-0.55.1 sub/avm/lib/avm/file_formats/base.rb
avm-0.62.0 lib/avm/file_formats/base.rb
eac_tools-0.55.0 sub/avm/lib/avm/file_formats/base.rb
avm-0.61.0 lib/avm/file_formats/base.rb
eac_tools-0.54.0 sub/avm/lib/avm/file_formats/base.rb
eac_tools-0.53.0 sub/avm/lib/avm/file_formats/base.rb
eac_tools-0.52.0 sub/avm/lib/avm/file_formats/base.rb
avm-0.60.0 lib/avm/file_formats/base.rb
eac_tools-0.51.0 sub/avm/lib/avm/file_formats/base.rb