Sha256: 4265d14f09cbb146aac77864c1840748c98b0858fa9f41ab822d8a41cc5d0f53

Contents?: true

Size: 1.5 KB

Versions: 23

Compression:

Stored size: 1.5 KB

Contents

require 'ndr_support/utf8_encoding'

module UTF8Encoding
  # Allows any supported object to have any high-ascii string
  # content to be force-encoded from UTF-8 to BINARY (/ASCII-8BIT).
  # This ensures that any serialisation to YAML, using Psych,
  # can be stored in other encodings. (Psych by default emits
  # UTF-8 YAML, which might not survive being stored in a Windows-1252
  # database, for example.)
  module ForceBinary
    # Recursively ensure the correct encoding is being used:
    def binary_encode_any_high_ascii(object)
      case object
      when String
        binary_encode_if_any_high_ascii(object)
      when Hash
        binary_encode_any_high_ascii_in_hash(object)
      when Array
        binary_encode_any_high_ascii_in_array(object)
      else
        object
      end
    end

    private

    # Returns a BINARY-encoded version of `string`, if is cannot be represented as 7bit ASCII.
    def binary_encode_if_any_high_ascii(string)
      string = ensure_utf8(string)
      string.force_encoding('BINARY') if string.bytes.detect { |byte| byte > 127 }
      string
    end

    # Ensures all values of the given `hash` are BINARY-encoded, if necessary.
    def binary_encode_any_high_ascii_in_hash(hash)
      Hash[hash.map { |key, value| [key, binary_encode_any_high_ascii(value)] }]
    end

    # Ensures all values of the given `array` are BINARY-encoded, if necessary.
    def binary_encode_any_high_ascii_in_array(array)
      array.map { |element| binary_encode_any_high_ascii(element) }
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
ndr_support-3.2.1 lib/ndr_support/utf8_encoding/force_binary.rb
ndr_support-3.2.0 lib/ndr_support/utf8_encoding/force_binary.rb
ndr_support-3.1.1 lib/ndr_support/utf8_encoding/force_binary.rb