Sha256: 6600fc619ba0ac88940903d59798f660ce54323aa311b504272f8338b4c6194e

Contents?: true

Size: 1.99 KB

Versions: 30

Compression:

Stored size: 1.99 KB

Contents

require 'virtus'
require 'active_support/concern'
require 'active_model'
require 'avromatic/model/configuration'
require 'avromatic/model/value_object'
require 'avromatic/model/configurable'
require 'avromatic/model/nested_models'
require 'avromatic/model/validation'
require 'avromatic/model/attribute/union'
require 'avromatic/model/attributes'
require 'avromatic/model/attribute/record'
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)
        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
        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
        [
          ActiveModel::Validations,
          Virtus.value_object,
          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
        with_builder do |builder|
          mod.define_singleton_method(:included) do |model_class|
            model_class.include(*builder.inclusions)
            model_class.config = builder.config
            model_class.add_avro_fields
          end
        end
      end

      def with_builder
        yield(self)
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
avromatic-0.22.0 lib/avromatic/model/builder.rb
avromatic-0.21.1 lib/avromatic/model/builder.rb
avromatic-0.21.0 lib/avromatic/model/builder.rb
avromatic-0.21.0.rc1 lib/avromatic/model/builder.rb
avromatic-0.21.0.rc0 lib/avromatic/model/builder.rb
avromatic-0.20.0 lib/avromatic/model/builder.rb
avromatic-0.19.0 lib/avromatic/model/builder.rb
avromatic-0.18.1 lib/avromatic/model/builder.rb
avromatic-0.18.0 lib/avromatic/model/builder.rb
avromatic-0.18.0.rc0 lib/avromatic/model/builder.rb
avromatic-0.17.1 lib/avromatic/model/builder.rb
avromatic-0.17.0 lib/avromatic/model/builder.rb
avromatic-0.15.1 lib/avromatic/model/builder.rb
avromatic-0.15.0 lib/avromatic/model/builder.rb
avromatic-0.15.0.rc0 lib/avromatic/model/builder.rb
avromatic-0.14.0 lib/avromatic/model/builder.rb
avromatic-0.14.0.rc1 lib/avromatic/model/builder.rb
avromatic-0.14.0.rc0 lib/avromatic/model/builder.rb
avromatic-0.13.0 lib/avromatic/model/builder.rb
avromatic-0.13.0.rc1 lib/avromatic/model/builder.rb