Class: Alba::Association Private

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

Overview

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

Representing association

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, condition: nil, resource: 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

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

    a proc filtering data

  • 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

  • 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



22
23
24
25
26
27
28
29
30
# File 'lib/alba/association.rb', line 22

def initialize(name:, condition: nil, resource: nil, params: {}, nesting: nil, key_transformation: :none, helper: nil, &block)
  @name = name
  @condition = condition
  @resource = resource
  @params = params
  return if @resource

  assign_resource(nesting, key_transformation, block, helper)
end

Class Attribute Details

.const_cacheObject (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`



8
9
10
# File 'lib/alba/association.rb', line 8

def const_cache
  @const_cache
end

Instance Attribute Details

#nameObject (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.



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

def name
  @name
end

#objectObject (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.



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

def object
  @object
end

Instance Method Details

#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

Returns:

  • (Hash)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/alba/association.rb', line 38

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

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

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