Sha256: 09d94bbc502ecb3ec6245aa06310c548171c398716f4c7b86b1048d7fa99df36

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module SimplySerializable
  module Mixin
    module ClassMethods
      def inherited(subclass)
        subclass.serialize(**serializable_config)
        super(subclass)
      end

      def serialize(attributes: [], except: nil, only: nil)
        serializable_config[:attributes] = serializable_config[:attributes] |= attributes
        unless except.nil?
          serializable_config[:except] ||= []
          serializable_config[:except] = serializable_config[:except] |= except
        end

        unless only.nil?
          serializable_config[:only] ||= []
          serializable_config[:only] = serializable_config[:only] |= only
        end
      end

      def serializable_config
        @serializable_config ||= {
          attributes: [],
          except: nil,
          only: nil
        }
      end
    end

    def self.included(base)
      base.include(Fingerprintable::Mixin)
      base.extend(ClassMethods)
    end

    def serialize(cache: {})
      serializer(cache: cache).serialize
    end

    def serializer(cache: {})
      Serializer.new(
        **self.class.serializable_config.merge(
          cache: cache,
          object: self
        )
      )
    end

    def serializable_id
      "#{serializable_type}/#{fingerprint}"
    end

    def serializable_type
      self.class.name
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simply_serializable-1.2.2 lib/simply_serializable/mixin.rb