Class: Alba::NestedAttribute Private

Inherits:
Object
  • Object
show all
Defined in:
lib/alba/nested_attribute.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 nested attribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass:, key_transformation: :none, &block) ⇒ NestedAttribute

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 NestedAttribute.

Parameters:

  • klass (Class<Alba::Resource>)

    the parent for this nested attribute

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

    determines how to transform keys

  • block (Proc)

    class body



13
14
15
16
17
# File 'lib/alba/nested_attribute.rb', line 13

def initialize(klass:, key_transformation: :none, &block)
  @klass = klass
  @key_transformation = key_transformation
  @block = block
end

Instance Attribute Details

#key_transformation=(value) ⇒ Object (writeonly)

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.

Setter for key_transformation, used when it's changed after class definition



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

def key_transformation=(value)
  @key_transformation = value
end

Instance Method Details

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

Returns hash serialized from running the class body in the object.

Parameters:

  • object (Object)

    the object being serialized

  • params (Hash)

    params Hash inherited from Resource

  • within (Object, nil, false, true)

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

Returns:

  • (Hash)

    hash serialized from running the class body in the object



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

def value(object:, params:, within:)
  resource_class = Class.new(@klass)
  resource_class.instance_variable_set(:@_attributes, {}) # reset
  resource_class.transform_keys(@key_transformation)
  resource_class.class_eval(&@block)
  resource_class.new(object, params: params, within: within).serializable_hash
end