Sha256: 3ae6a0db64c27e62826603074074bf532800efd5da063caec13994b2a9a515c1

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

require 'alba/version'
require 'alba/serializer'
require 'alba/resource'

# Core module
module Alba
  class Error < StandardError; end
  class UnsupportedBackend < Error; end

  class << self
    attr_reader :backend, :encoder
    attr_accessor :default_serializer

    def backend=(backend)
      @backend = backend&.to_sym
      set_encoder
    end

    def serialize(object, with: nil, &block)
      raise ArgumentError, 'Block required' unless block

      resource_class.class_eval(&block)
      resource = resource_class.new(object)
      with ||= @default_serializer
      resource.serialize(with: with)
    end

    private

    def set_encoder
      @encoder = case @backend
                 when :oj
                   try_oj
                 when :active_support
                   try_active_support
                 when nil, :default, :json
                   default_encoder
                 else
                   raise Alba::UnsupportedBackend, "Unsupported backend, #{backend}"
                 end
    end

    def try_oj
      require 'oj'
      ->(hash) { Oj.dump(hash, mode: :strict) }
    rescue LoadError
      default_encoder
    end

    def try_active_support
      require 'active_support/json'
      ->(hash) { ActiveSupport::JSON.encode(hash) }
    rescue LoadError
      default_encoder
    end

    def default_encoder
      lambda do |hash|
        require 'json'
        JSON.dump(hash)
      end
    end

    def resource_class
      @resource_class ||= begin
                             klass = Class.new
                             klass.include(Alba::Resource)
                           end
    end
  end

  @encoder = default_encoder
end

Version data entries

2 entries across 2 versions & 1 rubygems

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