Sha256: 4be829aeb57bb6d3685cfee4936b94cc2bf76ada9c21e5a9b415dc795fca26d7

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

module Alba
  # This module represents how a resource should be serialized.
  module Serializer
    def self.included(base)
      base.include InstanceMethods
      base.extend ClassMethods
    end

    # Instance methods
    module InstanceMethods
      def initialize(resource)
        @_opts = self.class._opts || {}
        key = case @_opts[:key]
              when true
                resource.key
              else
                @_opts[:key]
              end
        @hash = resource.serializable_hash(with_key: false)
        @hash = {key.to_sym => @hash} if key
      end

      def serialize
        fallback = lambda do
          require 'json'
          JSON.dump(@hash)
        end
        case Alba.backend
        when :oj
          begin
            require 'oj'
            -> { Oj.dump(@hash, mode: :strict) }
          rescue LoadError
            fallback
          end
        else
          fallback
        end.call
      end
    end

    # Class methods
    module ClassMethods
      attr_reader :_opts

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alba-0.7.0 lib/alba/serializer.rb
alba-0.6.0 lib/alba/serializer.rb