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

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

  constants = modules_for(namespace) + [schema.underscore.camelize.singularize]
  klass = constants.join('::').safe_constantize
  return payload if klass.nil? || payload.nil?

  klass.new(**payload.symbolize_keys)
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)


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

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