Module: AEMEncodingSupport::EnableStringEncodings

Defined in:
lib/_aem/encodingsupport.rb

Class Method Summary collapse

Class Method Details

.pack_string(s, as_type) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/_aem/encodingsupport.rb', line 21

def EnableStringEncodings.pack_string(s, as_type)
  begin
    return AE::AEDesc.new(KAE::TypeUTF8Text, EnableStringEncodings.to_utf8_string(s)).coerce(as_type)
  rescue AE::MacOSError => e
    if e.to_i == -1700 # couldn't coerce to TypeUnicodeText
      raise TypeError, "Not valid UTF8 data or couldn't coerce to type %{as_type}: #{s.inspect}"
    else
      raise
    end
  end
end

.to_utf8_string(s) ⇒ Object

AE extension methods consume and return Strings containing UTF-8 encoded data, ignoring any attached encoding information. This module provides wrappers for AE interactions that take care of any encoding issues in Ruby 1.9+.



15
16
17
18
19
# File 'lib/_aem/encodingsupport.rb', line 15

def EnableStringEncodings.to_utf8_string(s)
  # Call before passing a string to an AE method that expects it to contain UTF-8 encoded data.
  return s if [Encoding::ASCII_8BIT, Encoding::UTF_8].include?(s.encoding)
  return s.encode('UTF-8')
end

.unpack_string(desc) ⇒ Object



33
34
35
36
37
# File 'lib/_aem/encodingsupport.rb', line 33

def EnableStringEncodings.unpack_string(desc)
  # String instances returned by AE methods contain UTF-8 data and ASCII-8BIT encoding,
  # so change the encoding to match the data
  return desc.coerce(KAE::TypeUTF8Text).data.force_encoding('UTF-8')
end