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(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:

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

    determines how to transform keys

  • block (Proc)

    class body



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

def initialize(key_transformation: :none, &block)
  @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



6
7
8
# File 'lib/alba/nested_attribute.rb', line 6

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



19
20
21
22
23
24
# File 'lib/alba/nested_attribute.rb', line 19

def value(object:, params:, within:)
  resource_class = Alba.resource_class
  resource_class.transform_keys(@key_transformation)
  resource_class.class_eval(&@block)
  resource_class.new(object, params: params, within: within).serializable_hash
end