Sha256: 01ec0f9c88453118ca8561b0cc89857b31f79771ea1718ad8876a951abf19cb3

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require_relative 'base'
require 'json'

module Deimos
  module SchemaClass
    # Base Class of Record Classes generated from Avro.
    class Record < Base
      # Element access method as if this Object were a hash
      # @param key[String||Symbol]
      # @return [Object] The value of the attribute if exists, nil otherwise
      def [](key)
        self.try(key.to_sym)
      end

      # :nodoc
      def with_indifferent_access
        self
      end

      # Returns the schema name of the inheriting class.
      # @return [String]
      def schema
        raise NotImplementedError
      end

      # Returns the namespace for the schema of the inheriting class.
      # @return [String]
      def namespace
        raise NotImplementedError
      end

      # Returns the full schema name of the inheriting class.
      # @return [String]
      def full_schema
        "#{namespace}.#{schema}"
      end

      # Returns the schema validator from the schema backend
      # @return [Deimos::SchemaBackends::Base]
      def validator
        Deimos.schema_backend(schema: schema, namespace: namespace)
      end

      # @return [Array<String>] an array of fields names in the schema.
      def schema_fields
        validator.schema_fields.map(&:name)
      end

      # :nodoc:
      def self.initialize_from_value(value)
        return nil if value.nil?

        value.is_a?(self) ? value : self.new(**value.symbolize_keys)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
deimos-ruby-1.12.0 lib/deimos/schema_class/record.rb