Module: Alba::Resource

Extended by:
ClassMethods
Includes:
InstanceMethods
Defined in:
lib/alba/resource.rb

Overview

This module represents what should be serialized

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#objectObject (readonly) Originally defined in module InstanceMethods

Returns the value of attribute object.

#paramsObject (readonly) Originally defined in module InstanceMethods

Returns the value of attribute params.

Class Method Details

.association(name, condition = nil, resource: nil, key: nil, params: {}, **options, &block) ⇒ void Also known as: one, many, has_one, has_many Originally defined in module ClassMethods

This method returns an undefined value.

Set association

Parameters:

  • name (String, Symbol)

    name of the association, used as key when ‘key` param doesn’t exist

  • condition (Proc, nil) (defaults to: nil)

    a Proc to modify the association

  • resource (Class<Alba::Resource>, String, Proc, nil) (defaults to: nil)

    representing resource for this association

  • key (String, Symbol, nil) (defaults to: nil)

    used as key when given

  • params (Hash) (defaults to: {})

    params override for the association

  • options (Hash<Symbol, Proc>)
  • block (Block)

Options Hash (**options):

  • if (Proc)

    a condition to decide if this association should be serialized

See Also:

.attribute(name, **options, &block) ⇒ void Originally defined in module ClassMethods

This method returns an undefined value.

Set an attribute with the given block

Parameters:

  • name (String, Symbol)

    key name

  • options (Hash<Symbol, Proc>)
  • block (Block)

    the block called during serialization

Options Hash (**options):

  • if (Proc)

    a condition to decide if this attribute should be serialized

Raises:

  • (ArgumentError)

    if block is absent

.attributes(*attrs, if: nil, **attrs_with_types) ⇒ void Originally defined in module ClassMethods

This method returns an undefined value.

Set multiple attributes at once

Parameters:

  • attrs (Array<String, Symbol>)
  • if (Proc) (defaults to: nil)

    condition to decide if it should serialize these attributes

  • attrs_with_types (Hash<[Symbol, String], [Array<Symbol, Proc>, Symbol]>)

    attributes with name in its key and type and optional type converter in its value

.collection_key(key) ⇒ Object Originally defined in module ClassMethods

Sets key for collection serialization

Parameters:

  • key (String, Symbol)

.helper(mod = @_helper || Module.new, &block) ⇒ Object Originally defined in module ClassMethods

Define helper methods

Parameters:

  • mod (Module) (defaults to: @_helper || Module.new)

    a module to extend

.layout(file: nil, inline: nil) ⇒ Object Originally defined in module ClassMethods

Set layout

Parameters:

  • file (String) (defaults to: nil)

    name of the layout file

  • inline (Proc) (defaults to: nil)

    a proc returning JSON string or a Hash representing JSON

.meta(&block) ⇒ Object Originally defined in module ClassMethods

Set metadata

.nested_attribute(name, **options, &block) ⇒ void Also known as: nested Originally defined in module ClassMethods

This method returns an undefined value.

Set a nested attribute with the given block

Parameters:

  • name (String, Symbol)

    key name

  • options (Hash<Symbol, Proc>)
  • block (Block)

    the block called during serialization

Options Hash (**options):

  • if (Proc)

    a condition to decide if this attribute should be serialized

Raises:

  • (ArgumentError)

    if block is absent

.on_error(handler = nil, &block) ⇒ Object Originally defined in module ClassMethods

Set error handler If this is set it’s used as a error handler overriding global one

Parameters:

  • handler (Symbol) (defaults to: nil)

    ‘:raise`, `:ignore` or `:nullify`

  • block (Block)

Raises:

  • (ArgumentError)

.on_nil(&block) ⇒ Object Originally defined in module ClassMethods

Set nil handler

Parameters:

  • block (Block)

.prefer_object_method!Object Originally defined in module ClassMethods

DSL for alias, purely for readability

.prefer_resource_method!Object Originally defined in module ClassMethods

DSL for alias, purely for readability

.root_key(key, key_for_collection = nil) ⇒ Object Originally defined in module ClassMethods

Set root key

Parameters:

  • key (String, Symbol)
  • key_for_collection (String, Symbol) (defaults to: nil)

Raises:

  • (NoMethodError)

    when key doesn’t respond to ‘to_sym` method

.root_key!Object Originally defined in module ClassMethods

Set root key to true

.root_key_for_collection(key) ⇒ Object Originally defined in module ClassMethods

Set root key for collection

Parameters:

  • key (String, Symbol)

Raises:

  • (NoMethodError)

    when key doesn’t respond to ‘to_sym` method

.transform_keys(type, root: true, cascade: true) ⇒ Object Originally defined in module ClassMethods

Transform keys as specified type

Parameters:

  • type (String, Symbol)

    one of ‘snake`, `:camel`, `:lower_camel`, `:dash` and `none`

  • root (Boolean) (defaults to: true)

    decides if root key also should be transformed

  • cascade (Boolean) (defaults to: true)

    decides if key transformation cascades into inline association Default is true but can be set false for old (v1) behavior

Raises:

Instance Method Details

#as_json(root_key: nil, meta: {}) ⇒ Hash Originally defined in module InstanceMethods

Returns a Hash correspondng #serialize

Parameters:

  • root_key (Symbol, nil, true) (defaults to: nil)
  • meta (Hash) (defaults to: {})

    metadata for this seialization

Returns:

  • (Hash)

#initialize(object, params: {}, within: WITHIN_DEFAULT) ⇒ Object Originally defined in module InstanceMethods

Parameters:

  • object (Object)

    the object to be serialized

  • params (Hash) (defaults to: {})

    user-given Hash for arbitrary data

  • within (Object, nil, false, true) (defaults to: WITHIN_DEFAULT)

    determines what associations to be serialized. If not set, it serializes all associations.

#serializable_hashHash Also known as: to_h Originally defined in module InstanceMethods

A Hash for serialization

Returns:

  • (Hash)

#serialize(root_key: nil, meta: {}) ⇒ String Originally defined in module InstanceMethods

Serialize object into JSON string

Parameters:

  • root_key (Symbol, nil, true) (defaults to: nil)
  • meta (Hash) (defaults to: {})

    metadata for this seialization

Returns:

  • (String)

    serialized JSON string

#to_json(options = {}, root_key: nil, meta: {}) ⇒ Object Originally defined in module InstanceMethods

For Rails compatibility The first options is a dummy parameter