module Alba

Core module

Constants

VERSION

Attributes

_on_error[R]
backend[R]
encoder[R]
inferring[R]
inflector[RW]

Accessor for inflector, a module responsible for incflecting strings

transforming_root_key[R]

Public Class Methods

backend=(backend) click to toggle source

Set the backend, which actually serializes object into JSON

@param backend [#to_sym, nil] the name of the backend

Possible values are `oj`, `active_support`, `default`, `json` and nil

@return [Proc] the proc to encode object into JSON @raise [Alba::UnsupportedBackend] if backend is not supported

# File lib/alba.rb, line 28
def backend=(backend)
  @backend = backend&.to_sym
  set_encoder_from_backend
end
disable_inference!() click to toggle source

Disable inference for key and resource name

# File lib/alba.rb, line 72
def disable_inference!
  @inferring = false
end
disable_root_key_transformation!() click to toggle source

Disable root key transformation

# File lib/alba.rb, line 96
def disable_root_key_transformation!
  @transforming_root_key = false
end
enable_inference!() click to toggle source

Enable inference for key and resource name

# File lib/alba.rb, line 62
def enable_inference!
  begin
    require 'active_support/inflector'
  rescue LoadError
    raise ::Alba::Error, 'To enable inference, please install `ActiveSupport` gem.'
  end
  @inferring = true
end
enable_root_key_transformation!() click to toggle source

Enable root key transformation

# File lib/alba.rb, line 91
def enable_root_key_transformation!
  @transforming_root_key = true
end
encoder=(encoder) click to toggle source

Set encoder, a Proc object that accepts an object and generates JSON from it Set backend as `:custom` which indicates no preset encoder is used

@param encoder [Proc] @raise [ArgumentError] if given encoder is not a Proc or its arity is not one

# File lib/alba.rb, line 38
def encoder=(encoder)
  raise ArgumentError, 'Encoder must be a Proc accepting one argument' unless encoder.is_a?(Proc) && encoder.arity == 1

  @encoder = encoder
  @backend = :custom
end
infer_resource_class(name, nesting: nil) click to toggle source

@param name [String] a String Alba infers resource name with @param nesting [String, nil] namespace Alba tries to find resource class in @return [Class<Alba::Resource>] resource class

# File lib/alba.rb, line 112
def infer_resource_class(name, nesting: nil)
  enable_inference!
  const_parent = nesting.nil? ? Object : Object.const_get(nesting)
  const_parent.const_get("#{ActiveSupport::Inflector.classify(name)}Resource")
end
on_error(handler = nil, &block) click to toggle source

Set error handler

@param [Symbol] handler @param [Block] @raise [ArgumentError] if both handler and block params exist @raise [ArgumentError] if both handler and block params don't exist @return [void]

# File lib/alba.rb, line 83
def on_error(handler = nil, &block)
  raise ArgumentError, 'You cannot specify error handler with both Symbol and block' if handler && block
  raise ArgumentError, 'You must specify error handler with either Symbol or block' unless handler || block

  @_on_error = handler || block
end
resource_class(&block) click to toggle source

@param block [Block] resource body @return [Class<Alba::Resource>] resource class

# File lib/alba.rb, line 102
def resource_class(&block)
  klass = Class.new
  klass.include(Alba::Resource)
  klass.class_eval(&block)
  klass
end
serialize(object, key: nil, root_key: nil, &block) click to toggle source

Serialize the object with inline definitions

@param object [Object] the object to be serialized @param key [Symbol, nil, true] DEPRECATED, use root_key instead @param root_key [Symbol, nil, true] @param block [Block] resource block @return [String] serialized JSON string @raise [ArgumentError] if block is absent or `with` argument's type is wrong

# File lib/alba.rb, line 53
def serialize(object, key: nil, root_key: nil, &block)
  warn '`key` option to `serialize` method is deprecated, use `root_key` instead.' if key
  klass = block ? resource_class(&block) : infer_resource_class(object.class.name)

  resource = klass.new(object)
  resource.serialize(root_key: root_key || key)
end

Private Class Methods

default_encoder() click to toggle source
# File lib/alba.rb, line 147
def default_encoder
  lambda do |hash|
    JSON.dump(hash)
  end
end
set_encoder_from_backend() click to toggle source
# File lib/alba.rb, line 120
def set_encoder_from_backend
  @encoder = case @backend
             when :oj, :oj_strict then try_oj
             when :oj_rails then try_oj(mode: :rails)
             when :active_support then try_active_support
             when nil, :default, :json then default_encoder
             else
               raise Alba::UnsupportedBackend, "Unsupported backend, #{backend}"
             end
end
try_active_support() click to toggle source
# File lib/alba.rb, line 139
def try_active_support
  require 'active_support/json'
  ->(hash) { ActiveSupport::JSON.encode(hash) }
rescue LoadError
  Kernel.warn '`ActiveSupport` is not installed, falling back to default JSON encoder.'
  default_encoder
end
try_oj(mode: :strict) click to toggle source
# File lib/alba.rb, line 131
def try_oj(mode: :strict)
  require 'oj'
  ->(hash) { Oj.dump(hash, mode: mode) }
rescue LoadError
  Kernel.warn '`Oj` is not installed, falling back to default JSON encoder.'
  default_encoder
end