Sha256: b1fabdadba3bcaa51fbf2abc93cb6e98aa87047b577b67e5711b8fc3568e586f

Contents?: true

Size: 1.3 KB

Versions: 48

Compression:

Stored size: 1.3 KB

Contents

module Avromatic
  module Model

    # This concern adds methods for configuration for a model generated from
    # Avro schema(s).
    module Configurable
      extend ActiveSupport::Concern

      module ClassMethods
        attr_accessor :config
        delegate :avro_schema, :value_avro_schema, :key_avro_schema, to: :config

        def value_avro_field_names
          @value_avro_field_names ||= value_avro_schema.fields.map(&:name).map(&:to_sym).freeze
        end

        def key_avro_field_names
          @key_avro_field_names ||= key_avro_schema.fields.map(&:name).map(&:to_sym).freeze
        end

        def value_avro_fields_by_name
          @value_avro_fields_by_name ||= mapped_by_name(value_avro_schema)
        end

        def key_avro_fields_by_name
          @key_avro_fields_by_name ||= mapped_by_name(key_avro_schema)
        end

        def nested_models
          config.nested_models || Avromatic.nested_models
        end

        private

        def mapped_by_name(schema)
          schema.fields.each_with_object(Hash.new) do |field, result|
            result[field.name.to_sym] = field
          end
        end
      end

      delegate :avro_schema, :value_avro_schema, :key_avro_schema,
               :value_avro_field_names, :key_avro_field_names,
               to: :class
    end
  end
end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
avromatic-0.10.0.rc1 lib/avromatic/model/configurable.rb
avromatic-0.10.0.rc0 lib/avromatic/model/configurable.rb
avromatic-0.9.0 lib/avromatic/model/configurable.rb
avromatic-0.9.0.rc7 lib/avromatic/model/configurable.rb
avromatic-0.9.0.rc6 lib/avromatic/model/configurable.rb
avromatic-0.9.0.rc4 lib/avromatic/model/configurable.rb
avromatic-0.9.0.rc3 lib/avromatic/model/configurable.rb
avromatic-0.9.0.rc2 lib/avromatic/model/configurable.rb