Sha256: b4460af12754f651df24bdd98d466eaf750ced831caa08adb67bbb42fc236552

Contents?: true

Size: 1.07 KB

Versions: 6

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module Alba
  # Representing nested attribute
  # @api private
  class NestedAttribute
    # Setter for key_transformation, used when it's changed after class definition
    attr_writer :key_transformation

    # @param key_transformation [Symbol] determines how to transform keys
    # @param block [Proc] class body
    def initialize(key_transformation: :none, &block)
      @key_transformation = key_transformation
      @block = block
    end

    # @param object [Object] the object being serialized
    # @param params [Hash] params Hash inherited from Resource
    # @param within [Object, nil, false, true] determines what associations to be serialized. If not set, it serializes all associations.
    # @return [Hash] hash serialized from running the class body in the object
    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
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
alba-3.5.0 lib/alba/nested_attribute.rb
alba-3.4.0 lib/alba/nested_attribute.rb
alba-3.3.3 lib/alba/nested_attribute.rb
alba-3.3.2 lib/alba/nested_attribute.rb
alba-3.3.1 lib/alba/nested_attribute.rb
alba-3.3.0 lib/alba/nested_attribute.rb