Sha256: f248b5fd1f25ed1d3206b583dd0650e1098680ffe14b15b02863153308e62771

Contents?: true

Size: 1.74 KB

Versions: 9

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module Deimos
  module Utils
    # Class used by SchemaClassGenerator and Consumer/Producer interfaces
    module SchemaClass
      class << self

        # @param namespace [String]
        # @return [Array<String>]
        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

        # Converts a raw payload into an instance of the Schema Class
        # @param payload [Hash, Deimos::SchemaClass::Base]
        # @param schema [String]
        # @param namespace [String]
        # @return [Deimos::SchemaClass::Record]
        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

        # Determine and return the SchemaClass with the provided schema and namespace
        # @param schema [String]
        # @param namespace [String]
        # @return [Deimos::SchemaClass]
        def klass(schema, namespace)
          constants = modules_for(namespace) + [schema.underscore.camelize.singularize]
          constants.join('::').safe_constantize
        end

        # @param config [Hash] Producer or Consumer config
        # @return [Boolean]
        def use?(config)
          use_schema_classes = config[:use_schema_classes]
          use_schema_classes.present? ? use_schema_classes : Deimos.config.schema.use_schema_classes
        end

      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
deimos-ruby-1.23.3 lib/deimos/utils/schema_class.rb
deimos-ruby-1.23.2 lib/deimos/utils/schema_class.rb
deimos-ruby-1.23.1.pre.beta6 lib/deimos/utils/schema_class.rb
deimos-ruby-1.23.1.pre.beta5 lib/deimos/utils/schema_class.rb
deimos-ruby-1.23.1.pre.beta4 lib/deimos/utils/schema_class.rb
deimos-ruby-1.23.1.pre.beta3 lib/deimos/utils/schema_class.rb
deimos-ruby-1.23.1.pre.beta2 lib/deimos/utils/schema_class.rb
deimos-ruby-1.23.1.pre.beta1 lib/deimos/utils/schema_class.rb
deimos-ruby-1.23.0 lib/deimos/utils/schema_class.rb