Class: EncodingEstimator::Conversion
- Inherits:
-
Object
- Object
- EncodingEstimator::Conversion
- 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
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
-
#operation ⇒ Object
readonly
Returns the value of attribute operation.
Class Method Summary collapse
-
.default ⇒ EncodingEstimator::Conversion
Get the default (utf-8) encoding conversion: does nothing when performing the conversion.
-
.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.
Instance Method Summary collapse
-
#equals?(other) ⇒ Boolean
Check if two conversions are representing the same operation.
-
#initialize(encoding = DEFAULT_TARGET_ENCODING, operation = Operation::KEEP) ⇒ Conversion
constructor
Initialize a new conversion object from an encoding and tell it whether to convert from it or to it.
-
#key ⇒ String
Get the internal name (unique key) for this conversion.
-
#perform(data) ⇒ String
Perform the conversion with the current settings on a given string.
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
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
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
15 16 17 |
# File 'lib/encoding_estimator/conversion.rb', line 15 def encoding @encoding end |
#operation ⇒ Object (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
.default ⇒ EncodingEstimator::Conversion
Get the default (utf-8) encoding conversion: does nothing when performing the conversion.
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.
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.
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 |
#key ⇒ String
Get the internal name (unique key) for this conversion. Useful when storing/referencing conversions in hashes.
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
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 |