Class: Apiwork::Adapter::Standard::Capability::Writing::ContractBuilder

Inherits:
Capability::Contract::Base show all
Defined in:
lib/apiwork/adapter/standard/capability/writing/contract_builder.rb

Instance Attribute Summary

Attributes inherited from Capability::Contract::Base

#options, #scope

Attributes inherited from Builder::Contract::Base

#representation_class

Instance Method Summary collapse

Methods inherited from Capability::Contract::Base

#initialize

Methods inherited from Builder::Contract::Base

#api_class, #contract_for, #enum, #enum?, #import, #initialize, #object, #scoped_enum_name, #scoped_type_name, #type?, #union

Constructor Details

This class inherits a constructor from Apiwork::Adapter::Capability::Contract::Base

Instance Method Details

#buildObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/apiwork/adapter/standard/capability/writing/contract_builder.rb', line 9

def build
  build_enums
  build_nested_payload_union if api_class.representation_registry.nested_writable?(representation_class)

  %i[create update].each do |action_name|
    next unless scope.action?(action_name)

    build_payload_type(action_name)

    payload_type_name = [action_name, 'payload'].join('_').to_sym
    next unless type?(payload_type_name)

    contract_action = action(action_name)
    next if contract_action.resets_request?

    contract_action.request do |request|
      request.body do |body|
        body.reference(representation_class.root_key.singular.to_sym, to: payload_type_name)
      end
    end
  end

  return unless representation_class.subclass?

  build_payload_type(:create)
  build_payload_type(:update)
end