Class: Mindee::Parsing::V2::Field::ObjectField

Inherits:
BaseField
  • Object
show all
Defined in:
lib/mindee/parsing/v2/field/object_field.rb

Overview

A field containing a nested set of inference fields.

Instance Attribute Summary collapse

Attributes inherited from BaseField

#confidence, #indent_level, #locations

Instance Method Summary collapse

Methods inherited from BaseField

create_field

Constructor Details

#initialize(raw_prediction, indent_level = 0) ⇒ ObjectField

Returns a new instance of ObjectField.

Parameters:

  • raw_prediction (Hash)

    Raw server response hash.

  • indent_level (Integer) (defaults to: 0)

    Level of indentation for rst display.



17
18
19
20
21
22
# File 'lib/mindee/parsing/v2/field/object_field.rb', line 17

def initialize(raw_prediction, indent_level = 0)
  super

  inner_fields = raw_prediction.fetch('fields', raw_prediction)
  @fields = InferenceFields.new(inner_fields, @indent_level + 1)
end

Instance Attribute Details

#fieldsInferenceFields (readonly)

Returns Fields contained in the object.

Returns:



13
14
15
# File 'lib/mindee/parsing/v2/field/object_field.rb', line 13

def fields
  @fields
end

Instance Method Details

#get_simple_field(key) ⇒ SimpleField

Get a field by key and ensure it is a SimpleField.

Parameters:

  • key (String)

    Field key to retrieve.

Returns:

Raises:

  • (TypeError)

    If the field is not a SimpleField.



85
86
87
# File 'lib/mindee/parsing/v2/field/object_field.rb', line 85

def get_simple_field(key)
  @fields.get_simple_field(key)
end

#multi_strString

String representation for multi-object display

Returns:

  • (String)

    Formatted string for list context.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mindee/parsing/v2/field/object_field.rb', line 60

def multi_str
  return '' unless @fields && !@fields.empty?

  out_str = ''
  indent = ' ' * @indent_level
  first = true

  @fields.each do |field_key, field_value|
    value_str = field_value ? field_value.to_s : ''

    if first
      out_str += "#{indent}:#{field_key}: #{value_str}"
      first = false
    else
      out_str += "\n#{indent}    :#{field_key}: #{value_str}"
    end
  end

  out_str
end

#single_strString

String representation of a single object field

Returns:

  • (String)

    Multi-line string with proper indentation.



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mindee/parsing/v2/field/object_field.rb', line 44

def single_str
  return '' unless @fields && !@fields.empty?

  out_str = ''
  indent = ' ' * @indent_level

  @fields.each do |field_key, field_value|
    value_str = field_value && !field_value.to_s.empty? ? field_value.to_s : ''
    out_str += "\n#{indent}  :#{field_key}: #{value_str}"
  end

  out_str
end

#to_sString

String representation of the object field.

Returns:

  • (String)

    String representation with newline prefix.



26
27
28
29
30
# File 'lib/mindee/parsing/v2/field/object_field.rb', line 26

def to_s
  return "\n" unless @fields && !@fields.empty?

  "\n#{@fields.to_s(1)}"
end

#to_s_from_listString

String representation suitable for list display.

Returns:

  • (String)

    String representation without leading spaces.



34
35
36
37
38
39
40
# File 'lib/mindee/parsing/v2/field/object_field.rb', line 34

def to_s_from_list
  return '' if @fields.nil?
  return '' unless @fields && !@fields.empty?

  field_str = @fields.to_s(2)
  field_str.length > 4 ? (field_str[4..] || '') : ''
end