Class: Mindee::V1::Parsing::Universal::UniversalObjectField

Inherits:
Object
  • Object
show all
Defined in:
lib/mindee/v1/parsing/universal/universal_object_field.rb,
sig/mindee/v1/parsing/universal/universal_object_field.rbs

Overview

A JSON-like object, with miscellaneous values.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_prediction, page_id = nil) ⇒ UniversalObjectField

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.

Parameters:

  • (Hash[String | Symbol, untyped])
  • (Integer, nil)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mindee/v1/parsing/universal/universal_object_field.rb', line 29

def initialize(raw_prediction, page_id = nil)
  @all_values = {} # : Hash[String | Symbol, untyped]
  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 changing the user 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.

  • (Symbol)
  • (Object)
  • (Object)

Returns:

  • (Object)

    The value associated with the method name in @all_values.



69
70
71
72
# File 'lib/mindee/v1/parsing/universal/universal_object_field.rb', line 69

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

#all_valuesHash (readonly)

All values

Returns:

  • (Hash)


23
24
25
# File 'lib/mindee/v1/parsing/universal/universal_object_field.rb', line 23

def all_values
  @all_values
end

#confidenceFloat (readonly)

The confidence score, value will be between 0.0 and 1.0

Returns:

  • (Float)


17
18
19
# File 'lib/mindee/v1/parsing/universal/universal_object_field.rb', line 17

def confidence
  @confidence
end

#page_idInteger (readonly)

ID of the page (as given by the API).

Returns:

  • (Integer)


14
15
16
# File 'lib/mindee/v1/parsing/universal/universal_object_field.rb', line 14

def page_id
  @page_id
end

#raw_valueString (readonly)

Value as String

Returns:

  • (String)


20
21
22
# File 'lib/mindee/v1/parsing/universal/universal_object_field.rb', line 20

def raw_value
  @raw_value
end

Instance Method Details

#handle_default_field(name, value) ⇒ void

This method returns an undefined value.

Parameters:

  • (String, Symbol)
  • (Hash[String | Symbol, untyped], Integer, String, Float, bool, nil)


101
102
103
# File 'lib/mindee/v1/parsing/universal/universal_object_field.rb', line 101

def handle_default_field(name, value)
  @all_values[name] = value&.to_s
end

#handle_position_field(name, value, item_page_id) ⇒ void

This method returns an undefined value.

Parameters:

  • (String, Symbol)
  • (Hash[String | Symbol, untyped], Integer, String, Float, bool, nil)
  • (Integer, nil)


94
95
96
97
98
99
# File 'lib/mindee/v1/parsing/universal/universal_object_field.rb', line 94

def handle_position_field(name, value, item_page_id)
  @all_values[name.to_s] =
    Mindee::V1::Parsing::Standard::PositionField.new(
      { name.to_s => value }, item_page_id
    )
end

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

Necessary overload of the respond_to_missing? method to allow for direct access to dynamic attributes without changing DX 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 (bool) (defaults to: false)

    Whether to include private methods in the check.

  • (Symbol)
  • (Boolean)

Returns:

  • (bool)

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



83
84
85
# File 'lib/mindee/v1/parsing/universal/universal_object_field.rb', line 83

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

#str_level(level = 0) ⇒ String

String representation that takes into account the level of indentation.

Parameters:

  • (Integer)

Returns:

  • (String)


50
51
52
53
54
55
56
57
58
# File 'lib/mindee/v1/parsing/universal/universal_object_field.rb', line 50

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_sString

String representation

Returns:

  • (String)


88
89
90
# File 'lib/mindee/v1/parsing/universal/universal_object_field.rb', line 88

def to_s
  str_level
end