Moves types map into configuration

This commit is contained in:
Muhammad Nawzad 2023-11-09 15:13:24 +03:00
parent 00c4b7fc7e
commit cab957709d
No known key found for this signature in database
GPG Key ID: B954B6AAE33940B2
3 changed files with 37 additions and 48 deletions

View File

@ -1,4 +1,3 @@
# configuration.rb
module Schemable
class Configuration
attr_accessor(
@ -26,7 +25,43 @@ module Schemable
def type_mapper(type_name)
return @custom_type_mappers[type_name] if @custom_type_mappers.key?(type_name)
TYPES_MAP[type_name.try(:to_sym)]
{
text: { type: :string },
string: { type: :string },
integer: { type: :integer },
boolean: { type: :boolean },
date: { type: :string, format: :date },
time: { type: :string, format: :time },
json: { type: :object, properties: {} },
hash: { type: :object, properties: {} },
jsonb: { type: :object, properties: {} },
object: { type: :object, properties: {} },
binary: { type: :string, format: :binary },
trueclass: { type: :boolean, default: true },
falseclass: { type: :boolean, default: false },
datetime: { type: :string, format: :'date-time' },
float: {
type: (@float_as_string ? :string : :number).to_s.to_sym,
format: :float
},
decimal: {
type: (@decimal_as_string ? :string : :number).to_s.to_sym,
format: :double
},
array: {
type: :array,
items: {
anyOf: [
{ type: :string },
{ type: :integer },
{ type: :boolean },
{ type: :number, format: :float },
{ type: :object, properties: {} },
{ type: :number, format: :double }
]
}
}
}[type_name.try(:to_sym)]
end
def add_custom_type_mapper(type_name, mapping)

View File

@ -1,41 +0,0 @@
module Schemable
module Constants
TYPES_MAP = {
text: { type: :string },
string: { type: :string },
integer: { type: :integer },
boolean: { type: :boolean },
date: { type: :string, format: :date },
time: { type: :string, format: :time },
json: { type: :object, properties: {} },
hash: { type: :object, properties: {} },
jsonb: { type: :object, properties: {} },
object: { type: :object, properties: {} },
binary: { type: :string, format: :binary },
trueclass: { type: :boolean, default: true },
falseclass: { type: :boolean, default: false },
datetime: { type: :string, format: :'date-time' },
float: {
type: (configs[:float_as_string] ? :string : :number).to_s.to_sym,
format: :float
},
decimal: {
type: (configs[:decimal_as_string] ? :string : :number).to_s.to_sym,
format: :double
},
array: {
type: :array,
items: {
anyOf: [
{ type: :string },
{ type: :integer },
{ type: :boolean },
{ type: :number, format: :float },
{ type: :object, properties: {} },
{ type: :number, format: :double }
]
}
}
}.freeze
end
end

View File

@ -1,5 +0,0 @@
module Schemable
module Constants
TYPES_MAP: Hash[Symbol, any]
end
end