Sha256: 113ba645e40498571339191083736d282da528abd00d8e67f3d2dc3dac614613

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 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, **keywords)
        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

        serializable_config.merge!(keywords)
      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: {}, **options)
      serializer(cache: cache, **options).serialize
    end

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

    def serializable_id
      serializer.id
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simply_serializable-1.4.2 lib/simply_serializable/mixin.rb
simply_serializable-1.4.1 lib/simply_serializable/mixin.rb