Class: OmniAI::Schema::Scalar

Inherits:
Base
  • Object
show all
Defined in:
lib/omniai/schema/scalar.rb

Overview

Examples:

scalar = OmniAI::Schema::Scalar.deserialize({ type: "string" })
scalar.serialize #=> { type: "string" }
scalar.parse("Hello World") #=> "Hello World"
scalar = OmniAI::Schema::Scalar.deserialize({ type: "integer" })
scalar.serialize #=> { type: "integer" }
scalar.parse("123") #=> 123
scalar = OmniAI::Schema::Scalar.deserialize({ type: "number" })
scalar.serialize #=> { type: "number" }
scalar.parse("123.45") #=> 123.45
scalar = OmniAI::Schema::Scalar.deserialize({ type: "boolean" })
scalar.serialize #=> { type: "boolean" }
scalar.parse(true) #=> true
scalar.parse(false) #=> false

Defined Under Namespace

Modules: Type

Instance Attribute Summary collapse

Attributes inherited from Base

#description, #title

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#nonnullable, #nullable, #nullable?

Constructor Details

#initialize(type:, title: nil, description: nil, enum: nil, nullable: nil) ⇒ OmniAI::Schema::Scalar

Parameters:

  • type (String)

    required - the type of the property

  • title (String) (defaults to: nil)

    optional - a title of the property

  • description (String) (defaults to: nil)

    optional - a description of the property

  • enum (Array) (defaults to: nil)

    optional - the possible values of the property

  • nullable (Boolean) (defaults to: nil)

    optional - if the property may be null



83
84
85
86
87
# File 'lib/omniai/schema/scalar.rb', line 83

def initialize(type:, title: nil, description: nil, enum: nil, nullable: nil)
  super(title:, description:, nullable:)
  @type = type
  @enum = enum
end

Instance Attribute Details

#enumArray<String>?

Returns:



39
40
41
# File 'lib/omniai/schema/scalar.rb', line 39

def enum
  @enum
end

#typeString

Returns:

  • (String)


35
36
37
# File 'lib/omniai/schema/scalar.rb', line 35

def type
  @type
end

Class Method Details

.deserialize(data) ⇒ OmniAI::Schema::Scalar

Examples:

property = OmniAI::Schema::Scalar.deserialize({
  type: "string",
  description: "A predefined color.",
  enum: %w[red organge yellow green blue indigo violet],
})
property = OmniAI::Schema::Scalar.deserialize({
  type: "integer",
})
property = OmniAI::Schema::Scalar.deserialize({
  type: "number",
})
property = OmniAI::Schema::Scalar.deserialize({
  type: "boolean",
})

Parameters:

  • data (Hash)

Returns:



66
67
68
69
70
71
72
73
74
# File 'lib/omniai/schema/scalar.rb', line 66

def self.deserialize(data)
  types = Array(data["type"] || data[:type] || Type::STRING)
  type = types.find { |type| !type.eql?("null") }
  title = data["title"] || data[:title]
  description = data["description"] || data[:description]
  enum = data["enum"] || data[:enum]

  new(type:, title:, description:, enum:, nullable: types.include?("null"))
end

Instance Method Details

#parse(value) ⇒ String, ...

Examples:

property.parse("123") #=> 123

Parameters:

  • value (String, Integer, Float, Boolean, Object)

Returns:

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


111
112
113
114
115
116
117
118
119
120
# File 'lib/omniai/schema/scalar.rb', line 111

def parse(value)
  return if value.nil? && nullable?

  case @type
  when Type::INTEGER then Integer(value)
  when Type::STRING then String(value)
  when Type::NUMBER then Float(value)
  else value
  end
end

#serializeHash

Examples:

property.serialize #=> { type: "string" }
property.serialize #=> { type: ["strig", "null"] }

Returns:

  • (Hash)


96
97
98
99
100
101
102
103
# File 'lib/omniai/schema/scalar.rb', line 96

def serialize(*)
  {
    type: nullify(@type),
    title: @title,
    description: @description,
    enum: @enum,
  }.compact
end