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:



25
26
27
28
29
30
31
32
# File 'lib/deimos/utils/schema_class.rb', line 25

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:



38
39
40
41
# File 'lib/deimos/utils/schema_class.rb', line 38

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
# File 'lib/deimos/utils/schema_class.rb', line 11

def modules_for(namespace)
  modules = ['Schemas']
  namespace_folder = namespace.split('.').last
  if Deimos.config.schema.generate_namespace_folders && namespace_folder
    modules.push(namespace_folder.underscore.classify)
  end
  modules
end

.use?(config) ⇒ Boolean

Parameters:

  • config (Hash)

    Producer or Consumer config

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/deimos/utils/schema_class.rb', line 45

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