Class: Philiprehberger::EncodingKit::DetectionResult
- Inherits:
-
Object
- Object
- Philiprehberger::EncodingKit::DetectionResult
- Defined in:
- lib/philiprehberger/encoding_kit/detection_result.rb
Overview
A detection result that wraps an Encoding with a confidence score. Delegates to the underlying Encoding so it can be used transparently wherever an Encoding object is expected (e.g., == Encoding::UTF_8).
Instance Attribute Summary collapse
-
#confidence ⇒ Object
readonly
Returns the value of attribute confidence.
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Equality check delegates to the underlying encoding so that ‘result == Encoding::UTF_8` works as expected.
-
#eql?(other) ⇒ Boolean
Support ‘eql?` for hash key usage.
-
#hash ⇒ Integer
Delegate hash to encoding for hash key consistency.
-
#initialize(encoding, confidence) ⇒ DetectionResult
constructor
A new instance of DetectionResult.
-
#inspect ⇒ String
Inspect shows both encoding and confidence.
-
#method_missing(method) ⇒ Object
Delegate unknown methods to the underlying Encoding object.
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Support respond_to? for delegated methods.
-
#to_h ⇒ Hash
Convert to a plain hash representation.
-
#to_s ⇒ String
String representation shows the encoding name.
Constructor Details
#initialize(encoding, confidence) ⇒ DetectionResult
Returns a new instance of DetectionResult.
13 14 15 16 |
# File 'lib/philiprehberger/encoding_kit/detection_result.rb', line 13 def initialize(encoding, confidence) @encoding = encoding @confidence = confidence.to_f end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method) ⇒ Object
Delegate unknown methods to the underlying Encoding object.
71 72 73 74 75 76 77 |
# File 'lib/philiprehberger/encoding_kit/detection_result.rb', line 71 def method_missing(method, ...) if @encoding.respond_to?(method) @encoding.send(method, ...) else super end end |
Instance Attribute Details
#confidence ⇒ Object (readonly)
Returns the value of attribute confidence.
9 10 11 |
# File 'lib/philiprehberger/encoding_kit/detection_result.rb', line 9 def confidence @confidence end |
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
9 10 11 |
# File 'lib/philiprehberger/encoding_kit/detection_result.rb', line 9 def encoding @encoding end |
Instance Method Details
#==(other) ⇒ Boolean
Equality check delegates to the underlying encoding so that ‘result == Encoding::UTF_8` works as expected.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/philiprehberger/encoding_kit/detection_result.rb', line 23 def ==(other) case other when Encoding @encoding == other when DetectionResult @encoding == other.encoding else super end end |
#eql?(other) ⇒ Boolean
Support ‘eql?` for hash key usage.
38 39 40 |
# File 'lib/philiprehberger/encoding_kit/detection_result.rb', line 38 def eql?(other) self == other end |
#hash ⇒ Integer
Delegate hash to encoding for hash key consistency.
45 46 47 |
# File 'lib/philiprehberger/encoding_kit/detection_result.rb', line 45 def hash @encoding.hash end |
#inspect ⇒ String
Inspect shows both encoding and confidence.
59 60 61 |
# File 'lib/philiprehberger/encoding_kit/detection_result.rb', line 59 def inspect "#<#{self.class} encoding=#{@encoding} confidence=#{@confidence}>" end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Support respond_to? for delegated methods.
80 81 82 |
# File 'lib/philiprehberger/encoding_kit/detection_result.rb', line 80 def respond_to_missing?(method, include_private = false) @encoding.respond_to?(method, include_private) || super end |
#to_h ⇒ Hash
Convert to a plain hash representation.
66 67 68 |
# File 'lib/philiprehberger/encoding_kit/detection_result.rb', line 66 def to_h { encoding: @encoding, confidence: @confidence } end |
#to_s ⇒ String
String representation shows the encoding name.
52 53 54 |
# File 'lib/philiprehberger/encoding_kit/detection_result.rb', line 52 def to_s @encoding.to_s end |