Class: OmniAI::Schema::Scalar
- Inherits:
-
Base
- Object
- Base
- OmniAI::Schema::Scalar
show all
- Defined in:
- lib/omniai/schema/scalar.rb
Overview
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
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
#enum ⇒ Array<String>?
39
40
41
|
# File 'lib/omniai/schema/scalar.rb', line 39
def enum
@enum
end
|
#type ⇒ String
35
36
37
|
# File 'lib/omniai/schema/scalar.rb', line 35
def type
@type
end
|
Class Method Details
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, ...
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
|
#serialize ⇒ 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
|