Class: Apiwork::Adapter::Wrapper::Collection::Base

Inherits:
Base
  • Object
show all
Defined in:
lib/apiwork/adapter/wrapper/collection/base.rb

Overview

Base class for collection response wrappers.

Collection wrappers structure responses for index actions that return multiple records. Extend this class to customize how collections are wrapped in your API responses.

Examples:

Custom collection wrapper

class MyCollectionWrapper < Wrapper::Collection::Base
  shape do
    array(root_key.plural.to_sym) do |array|
      array.reference(data_type)
    end
    object?(:meta)
    .each { |type_name| merge(type_name) }
  end

  def wrap
    { root_key.plural.to_sym => data, meta: meta.presence, ** }.compact
  end
end

Direct Known Subclasses

Default

Instance Attribute Summary collapse

Attributes inherited from Base

#data

Instance Method Summary collapse

Methods inherited from Base

shape, wrap, #wrap

Constructor Details

#initialize(data, metadata, root_key, meta) ⇒ Base

Returns a new instance of Base.



50
51
52
53
54
55
# File 'lib/apiwork/adapter/wrapper/collection/base.rb', line 50

def initialize(data, , root_key, meta)
  super(data)
  @metadata = 
  @root_key = root_key
  @meta = meta
end

Instance Attribute Details

#metaHash (readonly)

The meta for this wrapper.

Returns:

  • (Hash)


46
47
48
# File 'lib/apiwork/adapter/wrapper/collection/base.rb', line 46

def meta
  @meta
end

#metadataHash (readonly)

The metadata for this wrapper.

Returns:

  • (Hash)


46
47
48
# File 'lib/apiwork/adapter/wrapper/collection/base.rb', line 46

attr_reader :meta,
:metadata,
:root_key

#root_keyObject (readonly)



46
47
48
# File 'lib/apiwork/adapter/wrapper/collection/base.rb', line 46

attr_reader :meta,
:metadata,
:root_key