Class: Bugno::Encoding::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/bugno/encoding/encoder.rb

Constant Summary collapse

ALL_ENCODINGS =
[::Encoding::UTF_8, ::Encoding::ISO_8859_1, ::Encoding::ASCII_8BIT, ::Encoding::US_ASCII].freeze
ASCII_ENCODINGS =
[::Encoding::US_ASCII, ::Encoding::ASCII_8BIT, ::Encoding::ISO_8859_1].freeze
ENCODING_OPTIONS =
{ invalid: :replace, undef: :replace, replace: '' }.freeze
UTF8 =
'UTF-8'
BINARY =
'binary'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Encoder

Returns a new instance of Encoder.



14
15
16
# File 'lib/bugno/encoding/encoder.rb', line 14

def initialize(object)
  @object = object
end

Instance Attribute Details

#objectObject

Returns the value of attribute object.



12
13
14
# File 'lib/bugno/encoding/encoder.rb', line 12

def object
  @object
end

Instance Method Details

#encodeObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bugno/encoding/encoder.rb', line 18

def encode
  value = object.to_s
  encoding = value.encoding

  # This will be most of cases so avoid force anything for them
  encoded_value = if encoding == ::Encoding::UTF_8 && value.valid_encoding?
                    value
                  else
                    force_encoding(value).encode(*encoding_args(value))
                  end

  object.is_a?(Symbol) ? encoded_value.to_sym : encoded_value
end