@@ -19,70 +19,106 @@ 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+ # String representation of the confidence level.
41+ # @return [String] The confidence level value.
42+ def to_s
43+ @value
4044 end
4145
42- # Check if this is a certain confidence level.
43- # @return [Boolean] `true` if confidence is certain .
44- def certain?
45- @value == CERTAIN
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 )
4650 end
4751
48- # Check if this is a high confidence level .
49- # @return [Boolean] `true` if confidence is high .
50- def high?
51- @value == HIGH
52+ # Inspect method for debugging .
53+ # @return [String] Debug representation .
54+ def inspect
55+ "#< #{ self . class . name } : #{ @value } >"
5256 end
5357
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
58+ # rubocop:disable Style/CaseLikeIf
5959
60- # Check if this is a low confidence level.
61- # @return [Boolean] `true` if confidence is low.
62- def low?
63- @value == LOW
60+ # Equality of two FieldConfidence instances.
61+ # @param other [String, Integer, FieldConfidence] The other confidence to compare.
62+ # @return [Boolean] `true` if they have the same value.
63+ def ==( other )
64+ if other . is_a? ( FieldConfidence )
65+ @value == other . value
66+ elsif other . is_a? ( String )
67+ @value == other
68+ elsif other . is_a? ( Integer )
69+ to_i == other
70+ else
71+ raise ArgumentError , "Invalid type: #{ other . class } "
72+ end
6473 end
6574
66- # String representation of the confidence level.
67- # @return [String] The confidence level value.
68- def to_s
69- @value
75+ # Greater than or equality of two FieldConfidence instances.
76+ # @param other [String, Integer, FieldConfidence] The other confidence to compare.
77+ def >=( other )
78+ if other . is_a? ( FieldConfidence )
79+ to_i >= val_to_i ( other . value )
80+ elsif other . is_a? ( String )
81+ to_i >= val_to_i ( other )
82+ elsif other . is_a? ( Integer )
83+ to_i >= other
84+ else
85+ raise ArgumentError , "Invalid type: #{ other . class } "
86+ end
7087 end
7188
72- # Compare two FieldConfidence instances.
73- # @param other [FieldConfidence] The other confidence to compare.
74- # @return [Boolean] `true` if they have the same value.
75- def ==( other )
76- other . is_a? ( FieldConfidence ) && @value == other . value
89+ # less than or equality of two FieldConfidence instances.
90+ # # @param other [String, Integer, FieldConfidence] The other confidence to compare.
91+ def <=( other )
92+ if other . is_a? ( FieldConfidence )
93+ to_i <= val_to_i ( other . value )
94+ elsif other . is_a? ( String )
95+ to_i <= val_to_i ( other )
96+ elsif other . is_a? ( Integer )
97+ to_i <= other
98+ else
99+ raise ArgumentError , "Invalid type: #{ other . class } "
100+ end
77101 end
78102
79- # Make instances comparable and hashable
103+ # rubocop:enable Style/CaseLikeIf
104+
105+ # Aliases for the comparison operators.
80106 alias eql? ==
107+ alias gteql? >=
108+ alias lteql? <=
81109
82- # Inspect method for debugging.
83- # @return [String] Debug representation.
84- def inspect
85- "#<#{ self . class . name } :#{ @value } >"
110+ protected
111+
112+ def val_to_i ( value )
113+ case value
114+ when CERTAIN then 4
115+ when HIGH then 3
116+ when MEDIUM then 2
117+ when LOW then 1
118+ else
119+ raise ArgumentError ,
120+ "Invalid confidence level: '#{ value } '. Must be one of: #{ VALID_VALUES . join ( ', ' ) } "
121+ end
86122 end
87123 end
88124 end
0 commit comments