Sha256: 0f6cff793d607e8960704d2f93cd5af850bbe3ea2991885e0162cf0b44e5a56a

Contents?: true

Size: 1.79 KB

Versions: 75

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

require 'eac_fs/file_info'

module Avm
  module Files
    class Formatter
      class Utf8Assert
        UTF8_CHARSET = 'utf-8'
        UTF8_CHARSETS = [UTF8_CHARSET, 'us-ascii'].freeze

        class << self
          def assert_files(files)
            asserters = files.map { |file| new(file) }
            begin
              asserters.each(&:assert)
              yield
            ensure
              asserters.each(&:revert)
            end
          end
        end

        enable_simple_cache
        common_constructor :path

        def assert
          return if original_utf8?

          convert_self(original_charset, UTF8_CHARSET)
        end

        def revert
          return if original_utf8?

          convert_self(UTF8_CHARSET, original_charset)
        end

        private

        def original_info_uncached
          ::EacFs::FileInfo.new(path)
        end

        def original_charset_uncached
          return original_info.content_type.charset if original_info.content_type.charset.present?

          raise 'No charset found'
        rescue StandardError => e
          raise "Unable to determine the charset of #{path} (#{e.message})"
        end

        def original_utf8?
          UTF8_CHARSETS.include?(original_charset)
        end

        def convert_file(from_path, from_charset, to_path, to_charset)
          File.open(from_path, "r:#{from_charset}") do |input|
            File.open(to_path, "w:#{to_charset}") do |output|
              output.write(input.read)
            end
          end
        end

        def convert_self(from_charset, to_charset)
          temp = ::Tempfile.new
          temp.close
          convert_file(path, from_charset, temp.path, to_charset)
          ::FileUtils.mv(temp.path, path)
        end
      end
    end
  end
end

Version data entries

75 entries across 75 versions & 4 rubygems

Version Path
avm-files-0.5.0 lib/avm/files/formatter/utf8_assert.rb
eac_tools-0.37.2 sub/avm-files/lib/avm/files/formatter/utf8_assert.rb
eac_tools-0.37.1 sub/avm-files/lib/avm/files/formatter/utf8_assert.rb
eac_tools-0.37.0 sub/avm-files/lib/avm/files/formatter/utf8_assert.rb
eac_tools-0.36.1 sub/avm-files/lib/avm/files/formatter/utf8_assert.rb
eac_tools-0.36.0 sub/avm-files/lib/avm/files/formatter/utf8_assert.rb
eac_tools-0.35.0 sub/avm-files/lib/avm/files/formatter/utf8_assert.rb
eac_tools-0.34.0 sub/avm-files/lib/avm/files/formatter/utf8_assert.rb
eac_tools-0.33.0 sub/avm-files/lib/avm/files/formatter/utf8_assert.rb
eac_tools-0.32.0 sub/avm-files/lib/avm/files/formatter/utf8_assert.rb
eac_tools-0.31.1 sub/avm-files/lib/avm/files/formatter/utf8_assert.rb
avm-files-0.4.1 lib/avm/files/formatter/utf8_assert.rb
eac_tools-0.31.0 sub/avm-files/lib/avm/files/formatter/utf8_assert.rb
eac_tools-0.30.0 sub/avm-files/lib/avm/files/formatter/utf8_assert.rb
avm-tools-0.129.0 sub/avm-files/lib/avm/files/formatter/utf8_assert.rb
eac_tools-0.16.0 sub/avm-tools/sub/avm-files/lib/avm/files/formatter/utf8_assert.rb
avm-tools-0.128.0 sub/avm-files/lib/avm/files/formatter/utf8_assert.rb
eac_tools-0.15.0 sub/avm-tools/sub/avm-files/lib/avm/files/formatter/utf8_assert.rb
eac_tools-0.14.0 sub/avm-tools/sub/avm-files/lib/avm/files/formatter/utf8_assert.rb
eac_tools-0.13.0 sub/avm-tools/sub/avm-files/lib/avm/files/formatter/utf8_assert.rb