Module: Alba::Resource

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

Overview

This module represents what should be serialized

Defined Under Namespace

Modules: ClassMethods, InstanceMethods, _ClassContext

Constant Summary collapse

INTERNAL_VARIABLES =

Returns:

  • (Hash[Symbol, untyped])
{_attributes: {}, _key: nil, _key_for_collection: nil, _meta: nil, _transform_type: :none, _transforming_root_key: false, _key_transformation_cascade: true, _on_error: nil, _on_nil: nil, _layout: nil, _collection_key: nil, _helper: nil, _resource_methods: [], _select_arity: nil, _traits: {}}.freeze
WITHIN_DEFAULT =

Returns:

  • (Object)
Object.new.freeze
EMPTY_HASH =

Returns:

  • (generic_hash)
{}.freeze

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.

Returns:

  • (Object)

#paramsgeneric_hash (readonly) Originally defined in module InstanceMethods

Returns the value of attribute params.

Returns:

  • (generic_hash)

Class Method Details

._resource_methodsArray[Symbol] Originally defined in module ClassMethods

Returns:

  • (Array[Symbol])

.alias_methodSymbol Originally defined in module ClassMethods

Parameters:

  • (Symbol)
  • (Symbol)

Returns:

  • (Symbol)

.assign_attributes(attrs, if_value) ⇒ Object Originally defined in module ClassMethods

Parameters:

  • (Array[Symbol | String])
  • (Proc, Symbol, nil)

Returns:

  • (Object)

.assign_attributes_with_types(attrs_with_types, if_value, &block) ⇒ void Originally defined in module ClassMethods

This method returns an undefined value.

Parameters:

  • (Hash[Symbol | String, untyped])
  • (Proc, Symbol, nil)

.association(name, condition = nil, resource: nil, serializer: nil, source: nil, key: nil, with_traits: nil, params: EMPTY_HASH, **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

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

    alias for resource

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

    a Proc to customize the association source

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

    used as key when given

  • with_traits (Symbol, Array<Symbol>, nil) (defaults to: nil)

    specified traits

  • params (Hash) (defaults to: EMPTY_HASH)

    params override for the association

  • options (Hash<Symbol, Proc>)
  • block (Block)
  • (Symbol, String)
  • (Proc, nil)
  • resource: (Object) (defaults to: nil)
  • serializer: (Object) (defaults to: nil)
  • source: (Proc, nil) (defaults to: nil)
  • key: (key_type) (defaults to: nil)
  • with_traits: (Symbol, Array[Symbol], nil) (defaults to: nil)
  • params: (generic_hash) (defaults to: EMPTY_HASH)
  • (Object)

Options Hash (**options):

  • if (Proc, Symbol, nil)

    a condition to decide if this association should be serialized When it's Proc, it's called to check condition When it's Symbol, it's treated as a method name on the Resource and the method is called

See Also:

.attribute(name = nil, if: nil, **name_with_type, &block) ⇒ void Originally defined in module ClassMethods

This method returns an undefined value.

Set an attribute with the given block

Parameters:

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

    key name

  • if (Proc) (defaults to: nil)

    condition to decide if it should serialize these attributes

  • block (Block)

    the block called during serialization

  • (Symbol, String, nil)
  • if: (Proc, Symbol, nil) (defaults to: nil)
  • (Object)

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

  • (Symbol, String)
  • if: (Proc, Symbol, nil) (defaults to: nil)
  • (Object)

.class_evalvoid Originally defined in module ClassMethods

This method returns an undefined value.

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

This method returns an undefined value.

Sets key for collection serialization

Parameters:

  • key (String, Symbol)
  • (Symbol, String)

.dupObject Originally defined in module ClassMethods

Returns:

  • (Object)

.extendObject Originally defined in module ClassMethods

Parameters:

Returns:

  • (Object)

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

This method returns an undefined value.

Define helper methods

Parameters:

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

    a module to extend

  • (Module)

.includeObject Originally defined in module ClassMethods

Parameters:

Returns:

  • (Object)

.included(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

setup method is meta-programmatically defined here for performance.

Parameters:

Returns:

  • (Object)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/alba/resource.rb', line 28

def self.included(base) # rubocop:disable Metrics/MethodLength
  super
  base.class_eval do
    # Initialize
    setup_method_body = +'private def _setup;'
    INTERNAL_VARIABLES.each do |name, initial|
      instance_variable_set(:"@#{name}", initial.dup) unless instance_variable_defined?(:"@#{name}")
      setup_method_body << "@#{name} = self.class.#{name};"
    end
    setup_method_body << 'end'
    class_eval(setup_method_body, __FILE__, __LINE__ + 1)
    define_method(:encode, Alba.encoder)
  end
  base.include InstanceMethods
  base.extend ClassMethods
end

.inherited(subclass) ⇒ Object Originally defined in module ClassMethods

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Returns:

  • (Object)

.instance_methodUnboundMethod Originally defined in module ClassMethods

Parameters:

  • (Symbol)

Returns:

  • (UnboundMethod)

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

This method returns an undefined value.

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

  • file: (String, nil) (defaults to: nil)
  • inline: (Proc, nil) (defaults to: nil)

.meta(key = :meta, &block) ⇒ void Originally defined in module ClassMethods

This method returns an undefined value.

Set metadata

Parameters:

  • (Symbol)

.method_added(method_name) ⇒ Object Originally defined in module ClassMethods

This method_added is used for defining "resource methods"

Parameters:

  • (Symbol)

Returns:

  • (Object)

.module_evalvoid Originally defined in module ClassMethods

This method returns an undefined value.

.nameObject Originally defined in module ClassMethods

Returns:

  • (Object)

.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

  • (Symbol, String)
  • (Object)

Options Hash (**options):

  • if (Proc)

    a condition to decide if this attribute should be serialized

Raises:

  • (ArgumentError)

    if block is absent

.nestingString? Originally defined in module ClassMethods

Returns:

  • (String, nil)

.newObject Originally defined in module ClassMethods

Parameters:

  • (Object)
  • (Object)

Returns:

  • (Object)

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

This method returns an undefined value.

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)
  • (Symbol, nil)

Raises:

  • (ArgumentError)

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

This method returns an undefined value.

Set nil handler

Parameters:

  • block (Block)

.prefer_object_method!void Originally defined in module ClassMethods

This method returns an undefined value.

DSL for alias, purely for readability

.prefer_resource_method!void Originally defined in module ClassMethods

This method returns an undefined value.

DSL for alias, purely for readability

.privateObject Originally defined in module ClassMethods

Parameters:

  • (Symbol)

Returns:

  • (Object)

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

This method returns an undefined value.

Set root key

Parameters:

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

Raises:

  • (NoMethodError)

    when key doesn't respond to to_sym method

.root_key!void Originally defined in module ClassMethods

This method returns an undefined value.

Set root key to true

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

This method returns an undefined value.

Set root key for collection

Parameters:

  • key (String, Symbol)
  • (Symbol, String)

Raises:

  • (NoMethodError)

    when key doesn't respond to to_sym method

.trait(name, &block) ⇒ void Originally defined in module ClassMethods

This method returns an undefined value.

Set a trait

Parameters:

  • name (String, Symbol)

    name of the trait

  • block (Block)

    the "content" of the trait

  • (Symbol, String)

Raises:

  • (ArgumentError)

    if block is absent

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

This method returns an undefined value.

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

  • (Symbol, String)
  • root: (Boolean) (defaults to: true)
  • cascade: (Boolean) (defaults to: true)

Raises:

.transform_keys!(type) ⇒ Object Originally defined in module ClassMethods

Transform keys as specified type AFTER the class is defined Note that this is an experimental API and may be removed/changed

Parameters:

  • (Symbol, String)

Returns:

  • (Object)

See Also:

.validated_error_handler(handler) ⇒ Symbol Originally defined in module ClassMethods

Parameters:

  • (Object)

Returns:

  • (Symbol)

Instance Method Details

#__send__Object Originally defined in module InstanceMethods

Parameters:

  • (Symbol)
  • (Object)

Returns:

  • (Object)

#_fetch_attribute_from_object_first(obj, attribute) ⇒ Object Originally defined in module InstanceMethods

Parameters:

  • (Object)
  • (Symbol)

Returns:

  • (Object)

#_fetch_attribute_from_resource_first(obj, attribute) ⇒ Object Originally defined in module InstanceMethods

Parameters:

  • (Object)
  • (Symbol)

Returns:

  • (Object)

#_keyroot_key_type Originally defined in module InstanceMethods

Returns:

  • (root_key_type)

#_key_for_collectionroot_key_type Originally defined in module InstanceMethods

Returns:

  • (root_key_type)

#_metadata(block, meta) ⇒ generic_hash Originally defined in module InstanceMethods

Parameters:

  • (Object)
  • (generic_hash)

Returns:

  • (generic_hash)

#_setupvoid Originally defined in module InstanceMethods

This method returns an undefined value.

#as_json(_options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) ⇒ Hash Originally defined in module InstanceMethods

Returns a Hash corresponding #serialize

Parameters:

  • _options (Hash) (defaults to: EMPTY_HASH)

    dummy parameter for Rails compatibility

  • root_key (Symbol, nil) (defaults to: nil)
  • meta (Hash) (defaults to: EMPTY_HASH)

    metadata for this serialization

  • (generic_hash)
  • root_key: (root_key_type) (defaults to: nil)
  • meta: (generic_hash) (defaults to: EMPTY_HASH)

Returns:

  • (Hash)

#attributesHash[Symbol | String, attribute] Originally defined in module InstanceMethods

Deprecated.

in favor of select

This is default behavior for getting attributes for serialization Override this method to filter certain attributes

Returns:

#attributes_to_hash(obj, hash) ⇒ generic_hash Originally defined in module InstanceMethods

Parameters:

  • (Object)
  • (generic_hash)

Returns:

  • (generic_hash)

#bindingBinding Originally defined in module InstanceMethods

Returns:

  • (Binding)

#check_within(association_name) ⇒ Object Originally defined in module InstanceMethods

Parameters:

  • (Symbol)

Returns:

  • (Object)

#classClass Originally defined in module InstanceMethods

These methods are available on the eventual resource instance. Declaring them here models the module's inclusion context for Steep.

Returns:

#collection_converterProc Originally defined in module InstanceMethods

Returns:

  • (Proc)

#converterProc Originally defined in module InstanceMethods

Returns:

  • (Proc)

#deprecated_serializable_hashObject Originally defined in module InstanceMethods

Returns:

  • (Object)

#deprecated_serializable_hash_for_collectionObject Originally defined in module InstanceMethods

Returns:

  • (Object)

#do_select(key, value, attribute) ⇒ Boolean Originally defined in module InstanceMethods

Parameters:

  • (Object)
  • (Object)
  • (Object)

Returns:

  • (Boolean)

#encodeString Originally defined in module InstanceMethods

Parameters:

  • (Object)

Returns:

  • (String)

#fetch_attribute(obj, key, attribute) ⇒ Object Originally defined in module InstanceMethods

rubocop:disable Metrics

Parameters:

Returns:

  • (Object)

#fetch_attribute_from_object_and_resource(obj, attribute) ⇒ Object Originally defined in module InstanceMethods

Parameters:

  • (Object)
  • (Symbol)

Returns:

  • (Object)

#fetch_keyString Originally defined in module InstanceMethods

Returns:

  • (String)

#handle_error(error, obj, key, attribute, hash) ⇒ Object Originally defined in module InstanceMethods

Parameters:

  • (StandardError)
  • (Object)
  • (Symbol, String)
  • (attribute)
  • (generic_hash)

Returns:

  • (Object)

#hash_from_traits(obj) ⇒ Object Originally defined in module InstanceMethods

Parameters:

  • (Object)

Returns:

  • (Object)

#hash_with_metadata(hash, meta) ⇒ Object Originally defined in module InstanceMethods

Parameters:

  • (Object)
  • (generic_hash)

Returns:

  • (Object)

#initialize(object, params: EMPTY_HASH, within: WITHIN_DEFAULT, with_traits: nil, select: nil) ⇒ InstanceMethods Originally defined in module InstanceMethods

Returns a new instance of InstanceMethods.

Parameters:

  • object (Object)

    the object to be serialized

  • params (Hash) (defaults to: EMPTY_HASH)

    user-given Hash for arbitrary data

  • within (Alba::WITHIN_DEFAULT, Hash, Array, nil, false, true) (defaults to: WITHIN_DEFAULT)

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

  • with_traits (Symbol, Array<Symbol>, nil) (defaults to: nil)

    specified traits

  • select (Method) (defaults to: nil)

    DEPRECATED noop

  • (Object)
  • params: (generic_hash) (defaults to: EMPTY_HASH)
  • within: (Object) (defaults to: WITHIN_DEFAULT)
  • with_traits: (Symbol, Array[Symbol], nil) (defaults to: nil)
  • select: (Method, nil) (defaults to: nil)

Returns:

#instance_evalvoid Originally defined in module InstanceMethods

This method returns an undefined value.

#instance_execvoid Originally defined in module InstanceMethods

This method returns an undefined value.

#nil_handlerObject Originally defined in module InstanceMethods

Returns:

  • (Object)

#resource_name(pluralized: false) ⇒ String Originally defined in module InstanceMethods

Parameters:

  • pluralized: (Boolean) (defaults to: false)

Returns:

  • (String)

Raises:

#select(_key, _value, _attribute) ⇒ Boolean Originally defined in module InstanceMethods

Default implementation for selecting attributes Override this method to filter attributes based on key and value

Parameters:

  • (Object)
  • (Object)
  • (Object)

Returns:

  • (Boolean)

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

A Hash for serialization

Returns:

  • (Hash)

#serializable_hash_for_collectionObject Originally defined in module InstanceMethods

Returns:

  • (Object)

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

Serialize object into JSON string

Parameters:

  • root_key (Symbol, nil) (defaults to: nil)
  • meta (Hash) (defaults to: EMPTY_HASH)

    metadata for this serialization

  • root_key: (root_key_type) (defaults to: nil)
  • meta: (generic_hash) (defaults to: EMPTY_HASH)

Returns:

  • (String)

    serialized JSON string

#serialize_with(hash) ⇒ String Originally defined in module InstanceMethods

Parameters:

  • (Object)

Returns:

  • (String)

#set_key_and_attribute_body_from(obj, key, attribute, hash) ⇒ Object Originally defined in module InstanceMethods

Parameters:

  • (Object)
  • (Object)
  • (attribute)
  • (generic_hash)

Returns:

  • (Object)

#to_json(options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) ⇒ String Originally defined in module InstanceMethods

For Rails compatibility The first options is a dummy parameter

Parameters:

  • (generic_hash)
  • root_key: (root_key_type) (defaults to: nil)
  • meta: (generic_hash) (defaults to: EMPTY_HASH)

Returns:

  • (String)

See Also:

#transform_key(key) ⇒ Object Originally defined in module InstanceMethods

Parameters:

  • (Object)

Returns:

  • (Object)

#transforming_root_key?Boolean Originally defined in module InstanceMethods

Returns:

  • (Boolean)

#yield_if_within(association_name) {|arg0| ... } ⇒ Object Originally defined in module InstanceMethods

Parameters:

  • (Symbol)

Yields:

Yield Parameters:

  • arg0 (Object)

Yield Returns:

  • (Object)

Returns:

  • (Object)