Class: Schemable::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/schemable/configuration.rb

Overview

The Configuration class provides a set of configuration options for the Schemable module. It includes options for setting the ORM, handling enums, custom type mappers, and more. It is worth noting that the configuration options are global, and will affect all Definitions.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Initializes a new Configuration instance with default values.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/schemable/configuration.rb', line 25

def initialize
  @orm = :active_record # orm options are :active_record, :mongoid
  @float_as_string = false
  @custom_type_mappers = {}
  @pagination_enabled = true
  @decimal_as_string = false
  @use_serialized_instance = false
  @custom_defined_enum_method = nil
  @custom_meta_response_schema = nil
  @enum_prefix_for_simple_enum = nil
  @enum_suffix_for_simple_enum = nil
  @infer_expand_nested_from_expand = false
  @infer_attributes_from_custom_method = nil
  @infer_attributes_from_jsonapi_serializable = false
end

Instance Attribute Details

#custom_defined_enum_methodObject

Returns the value of attribute custom_defined_enum_method.



8
9
10
# File 'lib/schemable/configuration.rb', line 8

def custom_defined_enum_method
  @custom_defined_enum_method
end

#custom_meta_response_schemaObject

Returns the value of attribute custom_meta_response_schema.



8
9
10
# File 'lib/schemable/configuration.rb', line 8

def custom_meta_response_schema
  @custom_meta_response_schema
end

#custom_type_mappersObject

Returns the value of attribute custom_type_mappers.



8
9
10
# File 'lib/schemable/configuration.rb', line 8

def custom_type_mappers
  @custom_type_mappers
end

#decimal_as_stringObject

Returns the value of attribute decimal_as_string.



8
9
10
# File 'lib/schemable/configuration.rb', line 8

def decimal_as_string
  @decimal_as_string
end

#enum_prefix_for_simple_enumObject

Returns the value of attribute enum_prefix_for_simple_enum.



8
9
10
# File 'lib/schemable/configuration.rb', line 8

def enum_prefix_for_simple_enum
  @enum_prefix_for_simple_enum
end

#enum_suffix_for_simple_enumObject

Returns the value of attribute enum_suffix_for_simple_enum.



8
9
10
# File 'lib/schemable/configuration.rb', line 8

def enum_suffix_for_simple_enum
  @enum_suffix_for_simple_enum
end

#float_as_stringObject

Returns the value of attribute float_as_string.



8
9
10
# File 'lib/schemable/configuration.rb', line 8

def float_as_string
  @float_as_string
end

#infer_attributes_from_custom_methodObject

Returns the value of attribute infer_attributes_from_custom_method.



8
9
10
# File 'lib/schemable/configuration.rb', line 8

def infer_attributes_from_custom_method
  @infer_attributes_from_custom_method
end

#infer_attributes_from_jsonapi_serializableObject

Returns the value of attribute infer_attributes_from_jsonapi_serializable.



8
9
10
# File 'lib/schemable/configuration.rb', line 8

def infer_attributes_from_jsonapi_serializable
  @infer_attributes_from_jsonapi_serializable
end

#infer_expand_nested_from_expandObject

Returns the value of attribute infer_expand_nested_from_expand.



8
9
10
# File 'lib/schemable/configuration.rb', line 8

def infer_expand_nested_from_expand
  @infer_expand_nested_from_expand
end

#ormObject

Returns the value of attribute orm.



8
9
10
# File 'lib/schemable/configuration.rb', line 8

def orm
  @orm
end

#pagination_enabledObject

Returns the value of attribute pagination_enabled.



8
9
10
# File 'lib/schemable/configuration.rb', line 8

def pagination_enabled
  @pagination_enabled
end

#use_serialized_instanceObject

Returns the value of attribute use_serialized_instance.



8
9
10
# File 'lib/schemable/configuration.rb', line 8

def use_serialized_instance
  @use_serialized_instance
end

Instance Method Details

#add_custom_type_mapper(type_name, mapping) ⇒ Object

Adds a custom type mapper for a given type name.

Examples:

add_custom_type_mapper(:custom_type, { type: :custom })
type_mapper(:custom_type) #=> { type: :custom }

# It preferable to invoke this method in the config/initializers/schemable.rb file.
# This way, the custom type mapper will be available for all Definitions.
Schemable.configure do |config|
 config.add_custom_type_mapper(:custom_type, { type: :custom })
end

Parameters:

  • type_name (Symbol, String)

    The name of the type.

  • mapping (Hash)

    The mapping to add.



112
113
114
# File 'lib/schemable/configuration.rb', line 112

def add_custom_type_mapper(type_name, mapping)
  custom_type_mappers[type_name.to_sym] = mapping
end

#type_mapper(type_name) ⇒ Hash

Note:

If a custom type mapper is defined for the given type name, it will be returned.

Returns a type mapper for a given type name.

Examples:

type_mapper(:string) #=> { type: :string }

Parameters:

  • type_name (Symbol, String)

    The name of the type.

Returns:

  • (Hash)

    The type mapper for the given type name.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/schemable/configuration.rb', line 50

def type_mapper(type_name)
  return @custom_type_mappers[type_name] if @custom_type_mappers.key?(type_name.to_sym)

  {
    text: { type: :string },
    string: { type: :string },
    symbol: { 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' },
    big_decimal: { type: (@decimal_as_string ? :string : :number).to_s.to_sym, format: :double },
    'bson/objectid': { type: :string, format: :object_id },
    'mongoid/boolean': { type: :boolean },
    'mongoid/stringified_symbol': { type: :string },
    'active_support/time_with_zone': { 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.to_s.underscore.try(:to_sym)]
end