Class: EncodingEstimator::Conversion

Inherits:
Object
  • Object
show all
Defined in:
lib/encoding_estimator/conversion.rb

Overview

Class that represents the conversion of a string to or from an other encoding

Defined Under Namespace

Modules: Operation

Constant Summary collapse

DEFAULT_TARGET_ENCODING =
:'utf-8'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encoding = DEFAULT_TARGET_ENCODING, operation = Operation::KEEP) ⇒ Conversion

Initialize a new conversion object from an encoding and tell it whether to convert from it or to it

Parameters:

  • encoding (String) (defaults to: DEFAULT_TARGET_ENCODING)

    Encoding to convert to/from

  • operation (Symbol) (defaults to: Operation::KEEP)

    Whether to convert from that encoding or to it



21
22
23
24
# File 'lib/encoding_estimator/conversion.rb', line 21

def initialize( encoding = DEFAULT_TARGET_ENCODING, operation = Operation::KEEP )
  @encoding  = encoding
  @operation = operation
end

Instance Attribute Details

#encodingObject (readonly)

Returns the value of attribute encoding.



15
16
17
# File 'lib/encoding_estimator/conversion.rb', line 15

def encoding
  @encoding
end

#operationObject (readonly)

Returns the value of attribute operation.



14
15
16
# File 'lib/encoding_estimator/conversion.rb', line 14

def operation
  @operation
end

Class Method Details

.defaultEncodingEstimator::Conversion

Get the default (utf-8) encoding conversion: does nothing when performing the conversion.

Returns:



64
65
66
# File 'lib/encoding_estimator/conversion.rb', line 64

def self.default
  EncodingEstimator::Conversion.new
end

.generate(encodings = %w(utf-8 iso-8859-1 Windows-1251),, operations = [Operation::ENCODE, Operation::DECODE ], include_no_change = true) ⇒ Array<Conversion>

Generate all conversions of for given encodings and operations. Note: this will produce #encodings * #operations conversions if default is not included and #encoding * #operations + 1 if the default is included.

Parameters:

  • encodings (Array<String>) (defaults to: %w(utf-8 iso-8859-1 Windows-1251),)

    Names of the encodings to generate conversions for

  • operations (Array<Symbol>) (defaults to: [Operation::ENCODE, Operation::DECODE ])

    Operations describing which conversions (encode/decode/keep) to include

  • include_no_change (Boolean) (defaults to: true)

    Include the default conversion (keep UTF-8) in the list

Returns:

  • (Array<Conversion>)

    List of conversions generated from the encodings and operations



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/encoding_estimator/conversion.rb', line 76

def self.generate(
    encodings  = %w(utf-8 iso-8859-1 Windows-1251),
    operations = [Operation::ENCODE, Operation::DECODE ],
    include_no_change = true
)

  conversions = include_no_change ? [ Conversion.new ] : []

  encodings.each do |encoding|
    conversions = conversions + operations.map { |operation| Conversion.new( encoding, operation )  }
  end

  conversions
end

Instance Method Details

#equals?(other) ⇒ Boolean

Check if two conversions are representing the same operation.

Parameters:

Returns:

  • (Boolean)

    True if equal, false if not



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/encoding_estimator/conversion.rb', line 30

def equals?( other )

  # Not the same encoding? Cannot be equal
  return false if other.encoding.to_s != self.encoding.to_s

  # If the default and the target encoding is the same, the operation doesn't matter
  # as the conversion does nothing at all
  return true if self.encoding.to_s == DEFAULT_TARGET_ENCODING.to_s

  # Not the default encoding, so check if the operation is the same
  self.operation == other.operation
end

#keyString

Get the internal name (unique key) for this conversion. Useful when storing/referencing conversions in hashes.

Returns:

  • (String)

    Unique key of this conversion



57
58
59
# File 'lib/encoding_estimator/conversion.rb', line 57

def key
  @key ||= "#{operation}_#{encoding}"
end

#perform(data) ⇒ String

Perform the conversion with the current settings on a given string

Parameters:

  • data (String)

    String to encode/decode

Returns:

  • (String)

    The encoded/decoded string



47
48
49
50
51
# File 'lib/encoding_estimator/conversion.rb', line 47

def perform( data )
  return encode( data, encoding ) if operation == Operation::ENCODE
  return decode( data, encoding ) if operation == Operation::DECODE
  data
end