Class: Alba::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/alba/association.rb,
sig/alba/association.rbs

Overview

Representing association

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, modifier: nil, resource: nil, source: nil, with_traits: nil, params: {}, nesting: nil, key_transformation: :none, helper: nil, &block) { ... } ⇒ Association

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.

Returns a new instance of Association.

Parameters:

  • name (Symbol, String) —

    name of the method to fetch association

  • modifier (Proc, nil) (defaults to: nil) —

    a Proc to modify the association

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

    a resource class for the association, a proc returning a resource class or a name of the resource

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

    a proc to specify the source of the association

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

    specified traits

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

    params override for the association

  • nesting (String) (defaults to: nil) —

    a namespace where source class is inferred with

  • key_transformation (Symbol) (defaults to: :none) —

    key transformation type

  • helper (Module) (defaults to: nil) —

    helper module to include

  • block (Block) —

    used to define resource when resource arg is absent

  • name: (Symbol, String)
  • modifier: (Proc, nil) (defaults to: nil)
  • resource: (Class, Proc, String, Symbol, nil) (defaults to: nil)
  • source: (Proc, nil) (defaults to: nil)
  • with_traits: (Symbol, Array[Symbol], nil) (defaults to: nil)
  • params: (generic_hash) (defaults to: {})
  • nesting: (String, nil) (defaults to: nil)
  • key_transformation: (Alba::transform_type) (defaults to: :none)
  • helper: (Module, nil) (defaults to: nil)

Yields:

Yield Returns:

  • (void)


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

def initialize(
  name:,
  modifier: nil,
  resource: nil,
  source: nil,
  with_traits: nil,
  params: {},
  nesting: nil,
  key_transformation: :none,
  helper: nil,
  &block
)
  @name = name
  @modifier = modifier
  @resource = resource || resource_from(nesting, key_transformation, block, helper)
  @source = source
  @with_traits = with_traits
  @params = params
end

Class Attribute Details

.const_cache ⇒ Hash[Symbol | String, Class] (readonly)

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.

cache for const_get

Returns:

  • (Hash[Symbol | String, Class])


10
11
12
# File 'lib/alba/association.rb', line 10

def const_cache
  @const_cache
end

Instance Attribute Details

#name ⇒ Symbol, String (readonly)

Instance variables and methods

Returns:

  • (Symbol, String)


16
17
18
# File 'sig/alba/association.rbs', line 16

def name
  @name
end

Instance Method Details

#key_transformation=(type) ⇒ void

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.

This method returns an undefined value.

This is the same API in NestedAttribute

Parameters:

  • type (String, Symbol) —

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



50
51
52
# File 'lib/alba/association.rb', line 50

def key_transformation=(type)
  @resource.transform_keys(type) unless @resource.is_a?(Proc)
end

#to_h(target, within: nil, params: {}) ⇒ Hash

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.

Recursively converts an object into a Hash

Parameters:

  • target (Object) —

    the object having an association method

  • within (Hash) (defaults to: nil) —

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

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

    user-given Hash for arbitrary data

  • within: (within) (defaults to: nil)
  • params: (generic_hash) (defaults to: {})

Returns:

  • (Hash)


60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/alba/association.rb', line 60

def to_h(target, within: nil, params: {})
  params = params.merge(@params)
  object = object_from(target, params)
  return if object.nil?

  if @resource.is_a?(Proc)
    return to_h_with_each_resource(object, within, params) if object.is_a?(Enumerable)

    @resource.call(object).new(object, within: within, params: params, with_traits: @with_traits).to_h
  else
    to_h_with_constantize_resource(object, within, params)
  end
end