Sha256: 1fa5c929103f81bd8e2bccb19863d99ed99ec07e191fe43155772ad95b83017f

Contents?: true

Size: 1.95 KB

Versions: 22

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

require 'active_support/concern'
require 'active_model'
require 'avromatic/model/configuration'
require 'avromatic/model/value_object'
require 'avromatic/model/configurable'
require 'avromatic/model/field_helper'
require 'avromatic/model/nested_models'
require 'avromatic/model/validation'
require 'avromatic/model/types/type_factory'
require 'avromatic/model/attributes'
require 'avromatic/model/raw_serialization'
require 'avromatic/model/messaging_serialization'

module Avromatic
  module Model

    # This class implements generating models from Avro schemas.
    class Builder

      attr_reader :mod, :config

      # For options see Avromatic::Model.build
      def self.model(**options, &block)
        Class.new do
          include Avromatic::Model::Builder.new(**options).mod

          # Name is required for attribute validations on an anonymous class.
          def self.name
            super || (@name ||= config.avro_schema.name.classify)
          end

          class_eval(&block) if block
        end
      end

      # For options see Avromatic::Model.build
      def initialize(**options)
        @mod = Module.new
        @config = Avromatic::Model::Configuration.new(**options)
        define_included_method
      end

      def inclusions
        [
          Avromatic::Model::Configurable,
          Avromatic::Model::NestedModels,
          Avromatic::Model::Validation,
          Avromatic::Model::Attributes,
          Avromatic::Model::ValueObject,
          Avromatic::Model::RawSerialization,
          Avromatic::Model::MessagingSerialization
        ]
      end

      private

      def define_included_method
        local_mod = mod
        local_builder = self
        mod.define_singleton_method(:included) do |model_class|
          model_class.include(*local_builder.inclusions)
          model_class.config = local_builder.config
          model_class.add_avro_fields(local_mod)
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
avromatic-5.1.1 lib/avromatic/model/builder.rb
avromatic-5.1.0 lib/avromatic/model/builder.rb
avromatic-5.0.0 lib/avromatic/model/builder.rb
avromatic-4.3.0 lib/avromatic/model/builder.rb
avromatic-4.2.0 lib/avromatic/model/builder.rb
avromatic-4.1.1 lib/avromatic/model/builder.rb
avromatic-4.1.0 lib/avromatic/model/builder.rb
avromatic-4.0.0 lib/avromatic/model/builder.rb
avromatic-3.0.2 lib/avromatic/model/builder.rb
avromatic-3.0.1 lib/avromatic/model/builder.rb
avromatic-3.0.0 lib/avromatic/model/builder.rb
avromatic-2.4.0 lib/avromatic/model/builder.rb
avromatic-2.3.0 lib/avromatic/model/builder.rb
avromatic-2.2.6 lib/avromatic/model/builder.rb
avromatic-2.2.5 lib/avromatic/model/builder.rb
avromatic-2.2.4 lib/avromatic/model/builder.rb
avromatic-2.2.3 lib/avromatic/model/builder.rb
avromatic-2.2.2 lib/avromatic/model/builder.rb
avromatic-2.2.1 lib/avromatic/model/builder.rb
avromatic-2.2.0 lib/avromatic/model/builder.rb