Class: Philiprehberger::EncodingKit::DetectionResult

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(encoding, confidence) ⇒ DetectionResult

Returns a new instance of DetectionResult.

Parameters:

  • encoding (Encoding)

    the detected encoding

  • confidence (Float)

    confidence score between 0.0 and 1.0



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

#confidenceObject (readonly)

Returns the value of attribute confidence.



9
10
11
# File 'lib/philiprehberger/encoding_kit/detection_result.rb', line 9

def confidence
  @confidence
end

#encodingObject (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.

Parameters:

  • other (Object)

    the object to compare

Returns:

  • (Boolean)


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.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


38
39
40
# File 'lib/philiprehberger/encoding_kit/detection_result.rb', line 38

def eql?(other)
  self == other
end

#hashInteger

Delegate hash to encoding for hash key consistency.

Returns:

  • (Integer)


45
46
47
# File 'lib/philiprehberger/encoding_kit/detection_result.rb', line 45

def hash
  @encoding.hash
end

#inspectString

Inspect shows both encoding and confidence.

Returns:

  • (String)


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.

Returns:

  • (Boolean)


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_hHash

Convert to a plain hash representation.

Returns:

  • (Hash)


66
67
68
# File 'lib/philiprehberger/encoding_kit/detection_result.rb', line 66

def to_h
  { encoding: @encoding, confidence: @confidence }
end

#to_sString

String representation shows the encoding name.

Returns:

  • (String)


52
53
54
# File 'lib/philiprehberger/encoding_kit/detection_result.rb', line 52

def to_s
  @encoding.to_s
end