Skip to content

Commit 54fc9d7

Browse files
committed
♻️ rework field accessors
1 parent a91f86a commit 54fc9d7

File tree

10 files changed

+178
-127
lines changed

10 files changed

+178
-127
lines changed

docs/code_samples/default_v2.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'mindee'
24

35
input_path = '/path/to/the/file.ext'
@@ -22,7 +24,7 @@ inference_params = Mindee::Input::InferenceParameters.new(
2224
polygon: nil,
2325
# Boost the precision and accuracy of all extractions.
2426
# Calculate confidence scores for all fields.
25-
confidence: nil,
27+
confidence: nil
2628
)
2729

2830
# Load a file from disk

lib/mindee/parsing/v2/field/inference_fields.rb

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ module Mindee
66
module Parsing
77
module V2
88
module Field
9-
# Collection of inference fields that extends Hash functionality.
9+
# Represents a hash-like collection of inference fields, providing methods for
10+
# retrieval and string representation.
1011
class InferenceFields < Hash
1112
# @return [Integer] Level of indentation for rst display.
1213
attr_reader :indent_level
@@ -29,23 +30,37 @@ def get(key)
2930
self[key]
3031
end
3132

32-
# Allow dot notation access to fields.
33-
# @param method_name [Symbol] The method name (field key).
34-
# @return [BaseField, nil] The field or nil if not found.
35-
def method_missing(method_name, *args, &block)
36-
key = method_name.to_s
37-
if key?(key)
38-
self[key]
39-
else
40-
super
41-
end
33+
# Get a field by key and ensure it is a SimpleField.
34+
# @param key [String] Field key to retrieve.
35+
# @return [SimpleField] The SimpleField.
36+
# @raise [TypeError] If the field is not a SimpleField.
37+
def get_simple_field(key)
38+
field = self[key]
39+
raise TypeError, "Field #{key} is not a SimpleField" unless field.is_a?(SimpleField)
40+
41+
field
4242
end
4343

44-
# Check if method_missing should handle the method.
45-
# @param method_name [Symbol] The method name.
46-
# @return [Boolean] `true` if the method should be handled.
47-
def respond_to_missing?(method_name, include_private = false)
48-
key?(method_name.to_s) || super
44+
# Get a field by key and ensure it is a ListField.
45+
# @param key [String] Field key to retrieve.
46+
# @return [ListField] The ListField.
47+
# @raise [TypeError] If the field is not a ListField.
48+
def get_list_field(key)
49+
field = self[key]
50+
raise TypeError, "Field #{key} is not a ListField" unless field.is_a?(ListField)
51+
52+
field
53+
end
54+
55+
# Get a field by key and ensure it is an ObjectField.
56+
# @param key [String] Field key to retrieve.
57+
# @return [ObjectField] The ObjectField.
58+
# @raise [TypeError] If the field is not an ObjectField.
59+
def get_object_field(key)
60+
field = self[key]
61+
raise TypeError, "Field #{key} is not a ObjectField" unless field.is_a?(ObjectField)
62+
63+
field
4964
end
5065

5166
# rubocop:disable Metrics/CyclomaticComplexity
@@ -64,13 +79,7 @@ def to_s(indent = 0)
6479
line = "#{padding}:#{field_key}:"
6580

6681
case (field_value.class.name || '').split('::').last
67-
when 'ListField'
68-
# Check if ListField has items and they're not empty
69-
list_f = field_value # @type var list_f: ListField
70-
if defined?(list_f.items) && list_f.items && !list_f.items.empty?
71-
line += list_f.to_s
72-
end
73-
when 'ObjectField'
82+
when 'ListField', 'ObjectField'
7483
line += field_value.to_s
7584
when 'SimpleField'
7685
# Check if SimpleField has a non-empty value

lib/mindee/parsing/v2/field/list_field.rb

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ module V2
88
module Field
99
# Represents a field that contains a list of items.
1010
class ListField < BaseField
11-
include Enumerable
1211
# @return [Array<ListField | ObjectField | SimpleField>] Items contained in the list.
1312
attr_reader :items
1413

@@ -29,10 +28,30 @@ def initialize(server_response, indent_level = 0)
2928
end
3029
end
3130

31+
def simple_items
32+
fields = []
33+
@items.each do |item|
34+
raise TypeError, "Invalid field type detected: #{item.class}" unless item.is_a?(SimpleField)
35+
36+
fields << item
37+
end
38+
fields
39+
end
40+
41+
def object_items
42+
fields = []
43+
@items.each do |item|
44+
raise TypeError, "Invalid field type detected: #{item.class}" unless item.is_a?(ObjectField)
45+
46+
fields << item
47+
end
48+
fields
49+
end
50+
3251
# String representation of the list field.
3352
# @return [String] Formatted string with bullet points for each item.
3453
def to_s
35-
return "\n" if @items.empty?
54+
return '' unless @items && !@items.empty?
3655

3756
parts = ['']
3857
@items.each do |item|
@@ -47,40 +66,6 @@ def to_s
4766

4867
parts.join("\n * ")
4968
end
50-
51-
# Check if the list is empty.
52-
# @return [Boolean] `true` if the list has no items.
53-
def empty?
54-
@items.empty?
55-
end
56-
57-
# Get the number of items in the list.
58-
# @return [Integer] Number of items.
59-
def size
60-
@items.size
61-
end
62-
63-
# Get the number of items in the list (alias for size).
64-
# @return [Integer] Number of items.
65-
def length
66-
@items.length
67-
end
68-
69-
# Get an item by index.
70-
# @param index [Integer] The index of the item to retrieve.
71-
# @return [BaseField, nil] The item at the given index.
72-
def [](index)
73-
@items[index]
74-
end
75-
76-
# Iterator for Enumerator inheritance.
77-
# NOTE: Untyped due to incomplete support in current supported version of RBS.
78-
def each(&block)
79-
return to_enum(:each) unless block_given?
80-
81-
@items.each(&block)
82-
self
83-
end
8469
end
8570
end
8671
end

lib/mindee/parsing/v2/field/object_field.rb

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,12 @@ def multi_str
7878
out_str
7979
end
8080

81-
# Allow dot notation access to nested fields.
82-
# @param method_name [Symbol] The method name (field key).
83-
# @return [ObjectField, nil] The field or nil if not found.
84-
def method_missing(method_name, ...)
85-
if @fields.respond_to?(method_name)
86-
@fields.send(method_name, ...)
87-
else
88-
super
89-
end
90-
end
91-
92-
# Check if method_missing should handle the method.
93-
# @param method_name [Symbol] The method name.
94-
# @return [Boolean] `true` if the method should be handled.
95-
def respond_to_missing?(method_name, include_private = false)
96-
@fields.respond_to?(method_name) || super
81+
# Get a field by key and ensure it is a SimpleField.
82+
# @param key [String] Field key to retrieve.
83+
# @return [SimpleField] The SimpleField.
84+
# @raise [TypeError] If the field is not a SimpleField.
85+
def get_simple_field(key)
86+
@fields.get_simple_field(key)
9787
end
9888
end
9989
end

lib/mindee/parsing/v2/field/simple_field.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ class SimpleField < BaseField
1515
# @param indent_level [Integer] Level of indentation for rst display.
1616
def initialize(server_response, indent_level = 0)
1717
super
18-
@value = server_response.key?('value') ? server_response['value'] : nil
18+
value = server_response['value']
19+
@value = if value.is_a?(Integer)
20+
value.to_f
21+
else
22+
value
23+
end
1924
end
2025

2126
# String representation of the field value.

sig/mindee/parsing/v2/field/inference_fields.rbs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ module Mindee
66
class InferenceFields < Hash[String, ListField | ObjectField | SimpleField?]
77
attr_reader indent_level: Integer
88

9+
def get_list_field: (String) -> ListField
10+
def get_simple_field: (String) -> SimpleField
11+
def get_object_field: (String) -> ObjectField
912
def logger: () -> Logger
1013
def initialize: (Hash[String | Symbol, untyped], ?Integer) -> void
1114
def get: (String) -> (ListField | ObjectField | SimpleField?)
12-
def method_missing: (Symbol, *untyped) -> (ListField | ObjectField | SimpleField?)
13-
def respond_to_missing?: (Symbol, ?bool) -> bool
1415
def to_s: (?Integer) -> String
1516
end
1617
end

sig/mindee/parsing/v2/field/list_field.rbs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ module Mindee
66
class ListField < BaseField
77
include Enumerable[BaseField]
88

9-
attr_reader items: Array[BaseField]
9+
attr_reader items: Array[SimpleField | ObjectField | ListField]
1010
def initialize: (Hash[String | Symbol, untyped], ?Integer) -> void
11+
def object_items: -> Array[ObjectField]
12+
def simple_items: -> Array[SimpleField]
1113
def to_s: -> String
1214
def empty?: -> bool
1315
def size: -> Integer

sig/mindee/parsing/v2/field/object_field.rbs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ module Mindee
88
class ObjectField < BaseField
99
attr_reader fields: InferenceFields
1010
def initialize: (Hash[String | Symbol, untyped], ?Integer) -> void
11+
def get_simple_field: (String) -> SimpleField
1112
def multi_str: -> String
12-
def respond_to_missing?: (Symbol, bool) -> bool
1313
def single_str: -> String
1414
def to_s: -> String
1515
def to_s_from_list: -> String
16-
def method_missing: (Symbol, *untyped, untyped) -> (ObjectField?)
1716
end
1817
end
1918
end

sig/mindee/parsing/v2/field/simple_field.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Mindee
44
module V2
55
module Field
66
class SimpleField < BaseField
7-
attr_reader value: String | Integer | Float | bool?
7+
attr_reader value: String | Integer | Float | bool | nil
88

99
def initialize: (Hash[String | Symbol, untyped], ?Integer) -> void
1010
def to_s: -> String

0 commit comments

Comments
 (0)