Class: OmniAI::Schema::Scalar

Inherits:
Object
  • 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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Parameters:

  • type (String)

    required - the type 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



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

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

Instance Attribute Details

#descriptionString?

Returns:

  • (String, nil)


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

def description
  @description
end

#enumArray<String>?

Returns:



43
44
45
# File 'lib/omniai/schema/scalar.rb', line 43

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:



70
71
72
73
74
75
76
# File 'lib/omniai/schema/scalar.rb', line 70

def self.deserialize(data)
  type = data["type"] || data[:type] || Type::STRING
  description = data["description"] || data[:description]
  enum = data["enum"] || data[:enum]

  new(type:, description:, enum:)
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)


108
109
110
111
112
113
114
115
# File 'lib/omniai/schema/scalar.rb', line 108

def parse(value)
  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" }

Returns:

  • (Hash)


94
95
96
97
98
99
100
# File 'lib/omniai/schema/scalar.rb', line 94

def serialize
  {
    type: @type,
    description: @description,
    enum: @enum,
  }.compact
end