Class: Mindee::Parsing::Generated::GeneratedObjectField

Inherits:
Object
  • Object
show all
Includes:
Standard
Defined in:
lib/mindee/parsing/generated/generated_object_field.rb

Overview

A JSON-like object, with miscellaneous values.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_prediction, page_id = nil) ⇒ GeneratedObjectField

Id of the page the object was found on. Confidence with which the value was assessed. Raw unprocessed value, as it was sent by the server.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mindee/parsing/generated/generated_object_field.rb', line 18

def initialize(raw_prediction, page_id = nil)
  @all_values = {}
  item_page_id = nil
  raw_prediction.each do |name, value|
    case name
    when 'page_id'
      item_page_id = value
    when 'polygon', 'rectangle', 'quadrangle', 'bounding_box'
      handle_position_field(name, value, item_page_id)
    when 'confidence'
      @confidence = value
    when 'raw_value'
      @raw_value = value
    else
      handle_default_field(name, value)
    end
    @page_id = page_id || item_page_id
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_args) ⇒ Object

Necessary overload of the method_missing method to allow for direct access to dynamic attributes without complicating usage too much Returns the corresponding attribute when asked.

Otherwise, raises a NoMethodError.

Parameters:

  • method_name (Symbol)

    The name of the method being called.

  • _args (Array)

    Arguments passed to the method.

Returns:

  • (Object)

    The value associated with the method name in @all_values.



58
59
60
61
# File 'lib/mindee/parsing/generated/generated_object_field.rb', line 58

def method_missing(method_name, *_args)
  super unless @all_values.key?(method_name.to_s)
  @all_values[method_name.to_s]
end

Instance Attribute Details

#confidenceObject

Returns the value of attribute confidence.



12
13
14
# File 'lib/mindee/parsing/generated/generated_object_field.rb', line 12

def confidence
  @confidence
end

#page_idObject

Returns the value of attribute page_id.



12
13
14
# File 'lib/mindee/parsing/generated/generated_object_field.rb', line 12

def page_id
  @page_id
end

#raw_valueObject

Returns the value of attribute raw_value.



12
13
14
# File 'lib/mindee/parsing/generated/generated_object_field.rb', line 12

def raw_value
  @raw_value
end

Instance Method Details

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Necessary overload of the respond_to_missing? method to allow for direct access to dynamic attributes without complicating usage too much Returns true if the method name exists as a key in @all_values, indicating that the object can respond to the method. Otherwise, calls super to fallback to the default behavior.

Parameters:

  • method_name (Symbol)

    The name of the method being checked.

  • include_private (Boolean) (defaults to: false)

    Whether to include private methods in the check.

Returns:

  • (Boolean)

    True if the method can be responded to, false otherwise.



72
73
74
# File 'lib/mindee/parsing/generated/generated_object_field.rb', line 72

def respond_to_missing?(method_name, include_private = false)
  @all_values.key?(method_name.to_s) || super
end

#str_level(level = 0) ⇒ Object

String representation that takes into account the level of indentation.



39
40
41
42
43
44
45
46
47
# File 'lib/mindee/parsing/generated/generated_object_field.rb', line 39

def str_level(level = 0)
  indent = "  #{'  ' * level}"
  out_str = ''
  @all_values.each do |attr, value|
    str_value = value.nil? ? '' : value.to_s
    out_str += "\n#{indent}:#{attr}: #{str_value}".rstrip
  end
  "\n#{indent}#{out_str.strip}"
end

#to_sObject

String representation



77
78
79
# File 'lib/mindee/parsing/generated/generated_object_field.rb', line 77

def to_s
  str_level
end