Sha256: 32a9636d6c70b850311c1f1135dbba45b7114ae2ea9ba44a4f66cb306eb5d2ab

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

require 'json'

module Deimos
  module SchemaClass
    # Base Class for Schema Classes generated from Avro.
    class Base

      # :nodoc:
      def initialize(*_args)
      end

      # Converts the object to a string that represents a JSON object
      # @return [String] a JSON string
      def to_json(*_args)
        to_h.to_json
      end

      # Converts the object to a hash which can be used for debugging.
      # @return [Hash] a hash representation of the payload
      def as_json(_opts={})
        JSON.parse(to_json)
      end

      # Converts the object attributes to a hash which can be used for Kafka
      # @return [Hash] the payload as a hash.
      def to_h
        raise NotImplementedError
      end

      # :nodoc:
      def ==(other)
        comparison = other
        if other.class == self.class
          comparison = other.state
        end

        comparison == self.state
      end

      # :nodoc:
      def to_s
        klass = self.class
        "#{klass}(#{self.as_json.symbolize_keys.to_s[1..-2]})"
      end

      # Initializes this class from a given value
      # @param value [Object]
      def self.initialize_from_value(value)
        raise NotImplementedError
      end

    protected

      # :nodoc:
      def state
        as_json
      end

      # :nodoc:
      def hash
        state.hash
      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/base.rb