Sha256: b52ba8263a2a25a4bdd2001eb15b457bdbf9f47cd6b0be98014b7fd403eedb65

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module AdequateSerialization
  class Serializer
    class ClassNotFoundError < Error
      def initialize(serializer, serializes)
        super(<<~MSG)
          AdequateSerialization was unable to find the associated class to
          serialize for #{serializer}. It expected to find a class named
          #{serializes}. This could mean that it was incorrectly named, or that
          you have yet to create the class that it will serialize.
        MSG
      end
    end

    class << self
      def attributes
        @attributes ||= []
      end

      def attribute(*names, &block)
        options =
          if names.last.is_a?(Hash)
            names.pop
          else
            {}
          end

        additions =
          names.map! { |name| Attribute.from(name, options.dup, &block) }

        @attributes = attributes + additions
      end

      def serializes
        return @serializes if defined?(@serializes)

        class_name = name.gsub(/Serializer\z/, '')

        begin
          @serializes = const_get(class_name)
        rescue NameError
          raise ClassNotFoundError.new(name, class_name)
        end
      end
    end

    def serialize(model, opts = Options.null)
      self.class.attributes.each_with_object({}) do |attribute, response|
        attribute.serialize_to(self, response, model, opts.includes)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
adequate_serialization-2.0.1 lib/adequate_serialization/serializer.rb
adequate_serialization-2.0.0 lib/adequate_serialization/serializer.rb
adequate_serialization-1.0.1 lib/adequate_serialization/serializer.rb