Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/code_samples/default_v2.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'mindee'

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

# Load a file from disk
Expand Down
55 changes: 32 additions & 23 deletions lib/mindee/parsing/v2/field/inference_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module Mindee
module Parsing
module V2
module Field
# Collection of inference fields that extends Hash functionality.
# Represents a hash-like collection of inference fields, providing methods for
# retrieval and string representation.
class InferenceFields < Hash
# @return [Integer] Level of indentation for rst display.
attr_reader :indent_level
Expand All @@ -29,23 +30,37 @@ def get(key)
self[key]
end

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

field
end

# Check if method_missing should handle the method.
# @param method_name [Symbol] The method name.
# @return [Boolean] `true` if the method should be handled.
def respond_to_missing?(method_name, include_private = false)
key?(method_name.to_s) || super
# Get a field by key and ensure it is a ListField.
# @param key [String] Field key to retrieve.
# @return [ListField] The ListField.
# @raise [TypeError] If the field is not a ListField.
def get_list_field(key)
field = self[key]
raise TypeError, "Field #{key} is not a ListField" unless field.is_a?(ListField)

field
end

# Get a field by key and ensure it is an ObjectField.
# @param key [String] Field key to retrieve.
# @return [ObjectField] The ObjectField.
# @raise [TypeError] If the field is not an ObjectField.
def get_object_field(key)
field = self[key]
raise TypeError, "Field #{key} is not a ObjectField" unless field.is_a?(ObjectField)

field
end

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

case (field_value.class.name || '').split('::').last
when 'ListField'
# Check if ListField has items and they're not empty
list_f = field_value # @type var list_f: ListField
if defined?(list_f.items) && list_f.items && !list_f.items.empty?
line += list_f.to_s
end
when 'ObjectField'
when 'ListField', 'ObjectField'
line += field_value.to_s
when 'SimpleField'
# Check if SimpleField has a non-empty value
Expand Down
63 changes: 27 additions & 36 deletions lib/mindee/parsing/v2/field/list_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module V2
module Field
# Represents a field that contains a list of items.
class ListField < BaseField
include Enumerable
# @return [Array<ListField | ObjectField | SimpleField>] Items contained in the list.
attr_reader :items

Expand All @@ -29,10 +28,36 @@ def initialize(server_response, indent_level = 0)
end
end

# Return only simple fields.
# @return [Array<SimpleField>] Simple fields contained in the list.
# @raise [TypeError] If the fields are not SimpleField.
def simple_items
fields = []
@items.each do |item|
raise TypeError, "Invalid field type detected: #{item.class}" unless item.is_a?(SimpleField)

fields << item
end
fields
end

# Return only object fields.
# @return [Array<ObjectField>] Object fields contained in the list.
# @raise [TypeError] If the fields are not ObjectField.
def object_items
fields = []
@items.each do |item|
raise TypeError, "Invalid field type detected: #{item.class}" unless item.is_a?(ObjectField)

fields << item
end
fields
end

# String representation of the list field.
# @return [String] Formatted string with bullet points for each item.
def to_s
return "\n" if @items.empty?
return '' unless @items && !@items.empty?

parts = ['']
@items.each do |item|
Expand All @@ -47,40 +72,6 @@ def to_s

parts.join("\n * ")
end

# Check if the list is empty.
# @return [Boolean] `true` if the list has no items.
def empty?
@items.empty?
end

# Get the number of items in the list.
# @return [Integer] Number of items.
def size
@items.size
end

# Get the number of items in the list (alias for size).
# @return [Integer] Number of items.
def length
@items.length
end

# Get an item by index.
# @param index [Integer] The index of the item to retrieve.
# @return [BaseField, nil] The item at the given index.
def [](index)
@items[index]
end

# Iterator for Enumerator inheritance.
# NOTE: Untyped due to incomplete support in current supported version of RBS.
def each(&block)
return to_enum(:each) unless block_given?

@items.each(&block)
self
end
end
end
end
Expand Down
22 changes: 6 additions & 16 deletions lib/mindee/parsing/v2/field/object_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,12 @@ def multi_str
out_str
end

# Allow dot notation access to nested fields.
# @param method_name [Symbol] The method name (field key).
# @return [ObjectField, nil] The field or nil if not found.
def method_missing(method_name, ...)
if @fields.respond_to?(method_name)
@fields.send(method_name, ...)
else
super
end
end

# Check if method_missing should handle the method.
# @param method_name [Symbol] The method name.
# @return [Boolean] `true` if the method should be handled.
def respond_to_missing?(method_name, include_private = false)
@fields.respond_to?(method_name) || super
# Get a field by key and ensure it is a SimpleField.
# @param key [String] Field key to retrieve.
# @return [SimpleField] The SimpleField.
# @raise [TypeError] If the field is not a SimpleField.
def get_simple_field(key)
@fields.get_simple_field(key)
end
end
end
Expand Down
7 changes: 6 additions & 1 deletion lib/mindee/parsing/v2/field/simple_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ class SimpleField < BaseField
# @param indent_level [Integer] Level of indentation for rst display.
def initialize(server_response, indent_level = 0)
super
@value = server_response.key?('value') ? server_response['value'] : nil
value = server_response['value']
@value = if value.is_a?(Integer)
value.to_f
else
value
end
end

# String representation of the field value.
Expand Down
5 changes: 3 additions & 2 deletions sig/mindee/parsing/v2/field/inference_fields.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ module Mindee
class InferenceFields < Hash[String, ListField | ObjectField | SimpleField?]
attr_reader indent_level: Integer

def get_list_field: (String) -> ListField
def get_simple_field: (String) -> SimpleField
def get_object_field: (String) -> ObjectField
def logger: () -> Logger
def initialize: (Hash[String | Symbol, untyped], ?Integer) -> void
def get: (String) -> (ListField | ObjectField | SimpleField?)
def method_missing: (Symbol, *untyped) -> (ListField | ObjectField | SimpleField?)
def respond_to_missing?: (Symbol, ?bool) -> bool
def to_s: (?Integer) -> String
end
end
Expand Down
4 changes: 3 additions & 1 deletion sig/mindee/parsing/v2/field/list_field.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ module Mindee
class ListField < BaseField
include Enumerable[BaseField]

attr_reader items: Array[BaseField]
attr_reader items: Array[SimpleField | ObjectField | ListField]
def initialize: (Hash[String | Symbol, untyped], ?Integer) -> void
def object_items: -> Array[ObjectField]
def simple_items: -> Array[SimpleField]
def to_s: -> String
def empty?: -> bool
def size: -> Integer
Expand Down
3 changes: 1 addition & 2 deletions sig/mindee/parsing/v2/field/object_field.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ module Mindee
class ObjectField < BaseField
attr_reader fields: InferenceFields
def initialize: (Hash[String | Symbol, untyped], ?Integer) -> void
def get_simple_field: (String) -> SimpleField
def multi_str: -> String
def respond_to_missing?: (Symbol, bool) -> bool
def single_str: -> String
def to_s: -> String
def to_s_from_list: -> String
def method_missing: (Symbol, *untyped, untyped) -> (ObjectField?)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion sig/mindee/parsing/v2/field/simple_field.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Mindee
module V2
module Field
class SimpleField < BaseField
attr_reader value: String | Integer | Float | bool?
attr_reader value: String | Integer | Float | bool | nil

def initialize: (Hash[String | Symbol, untyped], ?Integer) -> void
def to_s: -> String
Expand Down
Loading