Sha256: d0708883d2cf3f20da3417c5d98417636263e80301e716754151b4341bc9d06a

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module SimplySerializable
  module Mixin
    module ClassMethods
      def inherited(subclass)
        subclass.serializes()
        super(subclass)
      end

      def serialize(attributes: [], except: [], only: [])
        serializable_config[:attributes] = serializable_config[:attributes] |= attributes
        serializable_config[:except] = serializable_config[:except] |= except
        serializable_config[:only] = serializable_config[:only] |= only
      end

      def serializable_config
        @serializable_config ||= {
          attributes: [],
          except: [],
          only: []
        }
      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.1.0 lib/simply_serializable/mixin.rb