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:, condition: 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

  • 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

  • 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



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

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

  assign_resource(nesting, key_transformation, block, helper)
end

Class Attribute Details

.const_cacheHash[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[String, Class])


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

def const_cache
  @const_cache
end

Instance Attribute Details

#nameSymbol, String (readonly)

Instance variables and methods

Returns:

  • (Symbol, String)


10
11
12
# File 'sig/alba/association.rbs', line 10

def name
  @name
end

Instance Method Details

#assign_resource(nesting, key_transformation, block, helper) ⇒ 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.

Parameters:

  • nesting (String, nil)
  • key_transformation (Alba::transform_type)
  • block (Proc, nil)
  • helper (Module, nil)


102
103
104
105
106
107
108
109
110
# File 'lib/alba/association.rb', line 102

def assign_resource(nesting, key_transformation, block, helper)
  @resource = if block
                charged_resource_class(helper, key_transformation, block)
              elsif Alba.inflector
                Alba.infer_resource_class(@name, nesting: nesting)
              else
                raise ArgumentError, 'When Alba.inflector is nil, either resource or block is required'
              end
end

#charged_resource_class(helper, key_transformation, block) ⇒ Class

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:

  • helper (Module, nil)
  • key_transformation (Alba::transform_type)
  • block (Proc)

Returns:

  • (Class)


112
113
114
115
116
117
118
# File 'lib/alba/association.rb', line 112

def charged_resource_class(helper, key_transformation, block)
  klass = Alba.resource_class
  klass.helper(helper) if helper
  klass.transform_keys(key_transformation)
  klass.class_eval(&block)
  klass
end

#constantize(resource) ⇒ Class

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:

  • resource (Class, Symbol, String)

Returns:

  • (Class)


89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/alba/association.rb', line 89

def constantize(resource)
  case resource
  when Class
    resource
  when Symbol, String
    self.class.const_cache.fetch(resource) do
      self.class.const_cache[resource] = Object.const_get(resource)
    end
  else
    raise Error, "Unexpected resource type: #{resource.class}"
  end
end

#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



53
54
55
# File 'lib/alba/association.rb', line 53

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

#object_from(target, params) ⇒ 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.

Parameters:

  • target (Object)
  • params (generic_hash)

Returns:

  • (Object)


79
80
81
82
83
84
85
86
87
# File 'lib/alba/association.rb', line 79

def object_from(target, params)
  o = if @source
        target.instance_exec(params, &@source)
      else
        target.is_a?(Hash) ? target.fetch(@name) : target.__send__(@name)
      end
  o = @condition.call(o, params, target) if @condition
  o
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

Returns:

  • (Hash)


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/alba/association.rb', line 63

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

#to_h_with_constantize_resource(object, within, params) ⇒ generic_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.

Parameters:

  • object (Object)
  • within (within)
  • params (generic_hash)

Returns:

  • (generic_hash)


126
127
128
129
# File 'lib/alba/association.rb', line 126

def to_h_with_constantize_resource(object, within, params)
  @resource = constantize(@resource)
  @resource.new(object, params: params, within: within, with_traits: @with_traits).to_h
end

#to_h_with_each_resource(object, within, params) ⇒ Array[generic_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.

Parameters:

  • object (Enumerable[untyped])
  • within (within)
  • params (generic_hash)

Returns:

  • (Array[generic_hash])


120
121
122
123
124
# File 'lib/alba/association.rb', line 120

def to_h_with_each_resource(object, within, params)
  object.map do |item|
    @resource.call(item).new(item, within: within, params: params, with_traits: @with_traits).to_h
  end
end