@@ -19,71 +19,97 @@ class FieldConfidence
1919 LOW = 'Low'
2020
2121 # List of valid values, as frozen strings.
22- VALID_VALUES = [ CERTAIN , HIGH , MEDIUM , LOW ] . freeze
22+ VALID_VALUES = [ 'Certain' , 'High' , 'Medium' , 'Low' ] . freeze
2323
2424 # @param value [String] The confidence level value.
2525 # @raise [ArgumentError] If the value is not a valid confidence level.
2626 def initialize ( value )
27- unless VALID_VALUES . include? ( value )
27+ case value
28+ when 'Certain' then @value = CERTAIN
29+ when 'High' then @value = HIGH
30+ when 'Medium' then @value = MEDIUM
31+ when 'Low' then @value = LOW
32+ else
2833 raise ArgumentError ,
29- "Invalid confidence level: #{ value } . Must be one of: #{ VALID_VALUES . join ( ', ' ) } "
34+ "Invalid confidence level: ' #{ value } ' . Must be one of: #{ VALID_VALUES . join ( ', ' ) } "
3035 end
3136
3237 @value = value
3338 end
3439
35- # Create a FieldConfidence from a string value.
36- # @param value [String] The confidence level string.
37- # @return [FieldConfidence] The confidence instance.
38- def self . from_string ( value )
39- new ( value )
40- end
41-
42- # Check if this is a certain confidence level.
43- # @return [Boolean] `true` if confidence is certain.
44- def certain?
45- @value == CERTAIN
46- end
47-
48- # Check if this is a high confidence level.
49- # @return [Boolean] `true` if confidence is high.
50- def high?
51- @value == HIGH
52- end
53-
54- # Check if this is a medium confidence level.
55- # @return [Boolean] `true` if confidence is medium.
56- def medium?
57- @value == MEDIUM
58- end
59-
60- # Check if this is a low confidence level.
61- # @return [Boolean] `true` if confidence is low.
62- def low?
63- @value == LOW
64- end
65-
6640 # String representation of the confidence level.
6741 # @return [String] The confidence level value.
6842 def to_s
6943 @value
7044 end
7145
72- # Compare two FieldConfidence instances.
73- # @param other [FieldConfidence] The other confidence to compare.
46+ # String representation of the confidence level.
47+ # @return [Integer] The confidence level value as an integer: 1 is LOW, 4 is HIGH.
48+ def to_i
49+ val_to_i ( @value )
50+ end
51+
52+ # Equality of two FieldConfidence instances.
53+ # @param other [String, Integer, FieldConfidence] The other confidence to compare.
7454 # @return [Boolean] `true` if they have the same value.
7555 def ==( other )
76- other . is_a? ( FieldConfidence ) && @value == other . value
56+ case other . class . name
57+ when 'String' then @value == other
58+ when 'Integer' then to_i == other
59+ when 'FieldConfidence' then @value == other . value
60+ else
61+ raise ArgumentError , "Invalid type: #{ other . class } "
62+ end
63+ end
64+
65+ # Greater than or equality of two FieldConfidence instances.
66+ # @param other [String, Integer, FieldConfidence] The other confidence to compare.
67+ def >=( other )
68+ case other . class . name
69+ when 'String' then val_to_i ( @value ) >= val_to_i ( other )
70+ when 'Integer' then to_i >= other
71+ when 'FieldConfidence' then val_to_i ( @value ) >= val_to_i ( other . value )
72+ else
73+ raise ArgumentError , "Invalid type: #{ other . class } "
74+ end
75+ end
76+
77+ # less than or equality of two FieldConfidence instances.
78+ # # @param other [String, Integer, FieldConfidence] The other confidence to compare.
79+ def <=( other )
80+ case other . class . name
81+ when 'String' then val_to_i ( @value ) <= val_to_i ( other )
82+ when 'Integer' then to_i <= other
83+ when 'FieldConfidence' then val_to_i ( @value ) <= val_to_i ( other . value )
84+ else
85+ raise ArgumentError , "Invalid type: #{ other . class } "
86+ end
7787 end
7888
79- # Make instances comparable and hashable
89+ # Aliases for the comparison operators.
8090 alias eql? ==
91+ alias gteql? >=
92+ alias lteql? <=
8193
8294 # Inspect method for debugging.
8395 # @return [String] Debug representation.
8496 def inspect
8597 "#<#{ self . class . name } :#{ @value } >"
8698 end
99+
100+ protected
101+
102+ def val_to_i ( value )
103+ case value
104+ when CERTAIN then 4
105+ when HIGH then 3
106+ when MEDIUM then 2
107+ when LOW then 1
108+ else
109+ raise ArgumentError ,
110+ "Invalid confidence level: '#{ value } '. Must be one of: #{ VALID_VALUES . join ( ', ' ) } "
111+ end
112+ end
87113 end
88114 end
89115 end
0 commit comments