Sha256: be66029d07c910cf176c45c1f3d5de428118b1e0bd6b453350ade91dc0534182

Contents?: true

Size: 1.46 KB

Versions: 30

Compression:

Stored size: 1.46 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)

          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

        # @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

30 entries across 30 versions & 1 rubygems

Version Path
deimos-ruby-1.22.5 lib/deimos/utils/schema_class.rb
deimos-ruby-1.22.4 lib/deimos/utils/schema_class.rb
deimos-ruby-1.22.3 lib/deimos/utils/schema_class.rb
deimos-ruby-1.22.2 lib/deimos/utils/schema_class.rb
deimos-ruby-1.22.1 lib/deimos/utils/schema_class.rb
deimos-ruby-1.22 lib/deimos/utils/schema_class.rb
deimos-ruby-1.20.1 lib/deimos/utils/schema_class.rb
deimos-ruby-1.20.0 lib/deimos/utils/schema_class.rb
deimos-ruby-1.19.7 lib/deimos/utils/schema_class.rb
deimos-ruby-1.19.6 lib/deimos/utils/schema_class.rb
deimos-ruby-1.19.5 lib/deimos/utils/schema_class.rb
deimos-ruby-1.19.4 lib/deimos/utils/schema_class.rb
deimos-ruby-1.19.3 lib/deimos/utils/schema_class.rb
deimos-ruby-1.19.2 lib/deimos/utils/schema_class.rb
deimos-ruby-1.19.1 lib/deimos/utils/schema_class.rb
deimos-ruby-1.19.1.pre.beta1 lib/deimos/utils/schema_class.rb
deimos-ruby-1.19.0 lib/deimos/utils/schema_class.rb
deimos-ruby-1.19.beta2 lib/deimos/utils/schema_class.rb
deimos-ruby-1.19.beta1 lib/deimos/utils/schema_class.rb
deimos-ruby-1.18.2 lib/deimos/utils/schema_class.rb