Class: Mindee::Parsing::V2::Field::InferenceFields
- Inherits:
-
Hash
- Object
- Hash
- Mindee::Parsing::V2::Field::InferenceFields
- Defined in:
- lib/mindee/parsing/v2/field/inference_fields.rb
Overview
Represents a hash-like collection of inference fields, providing methods for retrieval and string representation.
Instance Attribute Summary collapse
-
#indent_level ⇒ Integer
readonly
Level of indentation for rst display.
Instance Method Summary collapse
-
#get(key) ⇒ BaseField?
Get a field by key with nil fallback.
-
#get_list_field(key) ⇒ ListField
Get a field by key and ensure it is a ListField.
-
#get_object_field(key) ⇒ ObjectField
Get a field by key and ensure it is an ObjectField.
-
#get_simple_field(key) ⇒ SimpleField
Get a field by key and ensure it is a SimpleField.
-
#initialize(server_response, indent_level = 0) ⇒ InferenceFields
constructor
A new instance of InferenceFields.
-
#to_s(indent = 0) ⇒ String
rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity Convert the fields to a string representation.
Constructor Details
#initialize(server_response, indent_level = 0) ⇒ InferenceFields
Returns a new instance of InferenceFields.
17 18 19 20 21 22 23 24 |
# File 'lib/mindee/parsing/v2/field/inference_fields.rb', line 17 def initialize(server_response, indent_level = 0) super() @indent_level = indent_level server_response.each do |key, value| self[key.to_s] = BaseField.create_field(value, 1) end end |
Instance Attribute Details
#indent_level ⇒ Integer (readonly)
Returns Level of indentation for rst display.
13 14 15 |
# File 'lib/mindee/parsing/v2/field/inference_fields.rb', line 13 def indent_level @indent_level end |
Instance Method Details
#get(key) ⇒ BaseField?
Get a field by key with nil fallback.
29 30 31 |
# File 'lib/mindee/parsing/v2/field/inference_fields.rb', line 29 def get(key) self[key] end |
#get_list_field(key) ⇒ ListField
Get a field by key and ensure it is a ListField.
48 49 50 51 52 53 |
# File 'lib/mindee/parsing/v2/field/inference_fields.rb', line 48 def get_list_field(key) field = self[key] raise TypeError, "Field #{key} is not a ListField" unless field.is_a?(ListField) field end |
#get_object_field(key) ⇒ ObjectField
Get a field by key and ensure it is an ObjectField.
59 60 61 62 63 64 |
# File 'lib/mindee/parsing/v2/field/inference_fields.rb', line 59 def get_object_field(key) field = self[key] raise TypeError, "Field #{key} is not a ObjectField" unless field.is_a?(ObjectField) field end |
#get_simple_field(key) ⇒ SimpleField
Get a field by key and ensure it is a SimpleField.
37 38 39 40 41 42 |
# File 'lib/mindee/parsing/v2/field/inference_fields.rb', line 37 def get_simple_field(key) field = self[key] raise TypeError, "Field #{key} is not a SimpleField" unless field.is_a?(SimpleField) field end |
#to_s(indent = 0) ⇒ String
rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity Convert the fields to a string representation.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/mindee/parsing/v2/field/inference_fields.rb', line 71 def to_s(indent = 0) return '' if empty? indent ||= @indent_level padding = ' ' * indent lines = [] each do |field_key, field_value| line = "#{padding}:#{field_key}:" case (field_value.class.name || '').split('::').last when 'ListField', 'ObjectField' line += field_value.to_s when 'SimpleField' # Check if SimpleField has a non-empty value simple_f = field_value # @type var simple_f: SimpleField if defined?(simple_f.value) && simple_f.value && !simple_f.value.to_s.empty? line += " #{simple_f}" end else logger.debug("Unknown value was passed to the field creator: #{field_key} : #{field_value}") end lines << line end lines.join("\n").rstrip end |