Sha256: 402cd4c4acabebf3c9abdd83500e985e2ea50870f44e84676fb6a7e44ac35403

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

module Alba
  # This module represents how a resource should be serialized.
  module Serializer
    def self.included(base)
      super
      base.class_eval do
        @_opts = {} unless instance_variable_defined?('@_opts')
        @_metadata = {} unless instance_variable_defined?('@_metadata')
      end
      base.include InstanceMethods
      base.extend ClassMethods
    end

    # Instance methods
    module InstanceMethods
      def initialize(resource)
        @resource = resource
        @hash = resource.serializable_hash
        @hash = {key.to_sym => @hash} if key
        # @hash is either Hash or Array
        @hash.is_a?(Hash) ? @hash.merge!(metadata.to_h) : @hash << metadata
      end

      def serialize
        Alba.encoder.call(@hash)
      end

      private

      def key
        opts = self.class._opts
        opts[:key] == true ? @resource.key : opts[:key]
      end

      def metadata
        metadata = self.class._metadata
        metadata.transform_values { |block| block.call(@resource.object) }
      end
    end

    # Class methods
    module ClassMethods
      attr_reader :_opts, :_metadata

      def inherited(subclass)
        super
        %w[_opts _metadata].each { |name| subclass.instance_variable_set("@#{name}", public_send(name).clone) }
      end

      def set(key: false)
        @_opts[:key] = key
      end

      def metadata(name, &block)
        @_metadata[name] = block
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alba-0.11.0 lib/alba/serializer.rb
alba-0.10.2 lib/alba/serializer.rb