Module: Deimos::Utils::SchemaClass

Defined in:
lib/deimos/utils/schema_class.rb

Overview

Class used by SchemaClassGenerator and Consumer/Producer interfaces

Class Method Summary collapse

Class Method Details

.instance(payload, schema, namespace = '') ⇒ Deimos::SchemaClass::Record

Converts a raw payload into an instance of the Schema Class

Parameters:

Returns:



40
41
42
43
44
45
46
47
# File 'lib/deimos/utils/schema_class.rb', line 40

def instance(payload, schema, namespace='')
  return payload if payload.is_a?(Deimos::SchemaClass::Base)

  klass = klass(schema, namespace)
  return payload if klass.nil? || payload.nil?

  klass.new(**payload.symbolize_keys)
end

.klass(schema, namespace) ⇒ Deimos::SchemaClass

Determine and return the SchemaClass with the provided schema and namespace

Parameters:

  • schema (String)
  • namespace (String)

Returns:



53
54
55
56
# File 'lib/deimos/utils/schema_class.rb', line 53

def klass(schema, namespace)
  constants = modules_for(namespace) + [schema.underscore.camelize.singularize]
  constants.join('::').safe_constantize
end

.modules_for(namespace) ⇒ Array<String>

Parameters:

  • namespace (String)

Returns:

  • (Array<String>)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/deimos/utils/schema_class.rb', line 11

def modules_for(namespace)
  modules = ['Schemas']
  namespace_override = nil
  module_namespace = namespace

  if Deimos.config.schema.use_full_namespace
    if Deimos.config.schema.schema_namespace_map.present?
      namespace_keys = Deimos.config.schema.schema_namespace_map.keys.sort_by { |k| -k.length }
      namespace_override = namespace_keys.find { |k| module_namespace.include?(k) }
    end

    if namespace_override.present?
      # override default module
      modules = Array(Deimos.config.schema.schema_namespace_map[namespace_override])
      module_namespace = module_namespace.gsub(/#{namespace_override}\.?/, '')
    end

    namespace_folders = module_namespace.split('.').map { |f| f.underscore.camelize }
    modules.concat(namespace_folders) if namespace_folders.any?
  end

  modules
end

.use?(config) ⇒ Boolean

Parameters:

  • config (Hash)

    Producer or Consumer config

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/deimos/utils/schema_class.rb', line 60

def use?(config)
  use_schema_classes = config[:use_schema_classes]
  use_schema_classes.present? ? use_schema_classes : Deimos.config.schema.use_schema_classes
end