Class: Mindee::V2::Parsing::Field::SimpleField

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

Overview

A simple field with a scalar value.

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(server_response, indent_level = 0) ⇒ SimpleField

Returns a new instance of SimpleField.

Parameters:

  • server_response (Hash)

    Raw server response hash.

  • indent_level (Integer) (defaults to: 0)

    Level of indentation for rst display.



16
17
18
19
20
21
22
23
24
# File 'lib/mindee/v2/parsing/field/simple_field.rb', line 16

def initialize(server_response, indent_level = 0)
  super
  value = server_response['value']
  @value = if value.is_a?(Integer)
             value.to_f
           else
             value
           end
end

Instance Attribute Details

#valueString, ... (readonly)

Returns Value contained in the field.

Returns:

  • (String, Integer, Float, Boolean, nil)

    Value contained in the field.



12
13
14
# File 'lib/mindee/v2/parsing/field/simple_field.rb', line 12

def value
  @value
end

Instance Method Details

#boolean_valueBoolean?

Retrieves the field value as a Boolean.

Returns:

  • (Boolean, nil)

Raises:

  • (RuntimeError)

    If the value is not a Boolean.



64
65
66
67
68
69
70
71
# File 'lib/mindee/v2/parsing/field/simple_field.rb', line 64

def boolean_value
  unless @value.nil? || @value.is_a?(TrueClass) || @value.is_a?(FalseClass)
    raise "Value is not a boolean: #{@value.class}"
  end

  val = @value
  @value.is_a?(TrueClass) || @value.is_a?(FalseClass) ? val : nil # @type var val: bool | nil
end

#float_valueFloat?

Retrieves the field value as a Float.

Returns:

  • (Float, nil)

Raises:

  • (RuntimeError)

    If the value is not a Float.



44
45
46
47
48
49
# File 'lib/mindee/v2/parsing/field/simple_field.rb', line 44

def float_value
  raise "Value is not a float: #{@value.class}" unless @value.nil? || @value.is_a?(Float)

  val = @value
  val.is_a?(Float) ? val : nil # @type var val: Float | nil
end

#string_valueString?

Retrieves the field value as a String.

Returns:

  • (String, nil)

Raises:

  • (RuntimeError)

    If the value is not a String.



54
55
56
57
58
59
# File 'lib/mindee/v2/parsing/field/simple_field.rb', line 54

def string_value
  raise "Value is not a string: #{@value.class}" unless @value.nil? || @value.is_a?(String)

  val = @value
  val.is_a?(String) ? val : nil # @type var val: String | nil
end

#to_sString

String representation of the field value.

Returns:

  • (String)

    String representation of the value.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mindee/v2/parsing/field/simple_field.rb', line 28

def to_s
  return '' if @value.nil?

  if @value.is_a?(TrueClass) || @value.is_a?(FalseClass)
    @value ? 'True' : 'False'
  elsif @value.is_a?(Integer) || @value.is_a?(Float)
    num = @value # @type var num: Integer | Float
    format_numeric_value(num)
  else
    @value.to_s
  end
end