Sha256: 28e7a112d11beb94a18295c04c7f1cab9e0ff9e1cd2a8602186abeeac9ce2892

Contents?: true

Size: 1.21 KB

Versions: 16

Compression:

Stored size: 1.21 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

        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

16 entries across 16 versions & 1 rubygems

Version Path
avromatic-0.9.0.rc1 lib/avromatic/model/configurable.rb
avromatic-0.9.0.rc0 lib/avromatic/model/configurable.rb
avromatic-0.8.0 lib/avromatic/model/configurable.rb
avromatic-0.7.1 lib/avromatic/model/configurable.rb
avromatic-0.7.0 lib/avromatic/model/configurable.rb
avromatic-0.6.2 lib/avromatic/model/configurable.rb
avromatic-0.6.1 lib/avromatic/model/configurable.rb
avromatic-0.6.0 lib/avromatic/model/configurable.rb
avromatic-0.5.0 lib/avromatic/model/configurable.rb
avromatic-0.4.0 lib/avromatic/model/configurable.rb
avromatic-0.3.0 lib/avromatic/model/configurable.rb
avromatic-0.2.0 lib/avromatic/model/configurable.rb
avromatic-0.1.2 lib/avromatic/model/configurable.rb
avromatic-0.1.2.pre.rc0 lib/avromatic/model/configurable.rb
avromatic-0.1.1 lib/avromatic/model/configurable.rb
avromatic-0.1.0 lib/avromatic/model/configurable.rb