Module: Axn::Core::DefaultCall

Defined in:
lib/axn/core/default_call.rb

Overview

Default implementation of the call method that automatically exposes all declared exposures by calling methods with matching names.

Instance Method Summary collapse

Instance Method Details

#callObject

User-defined action logic - override this method in your action classes Default implementation automatically exposes all declared exposures by calling methods with matching names. Raises if a method is missing and no default is provided.



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
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/axn/core/default_call.rb', line 11

def call
  return if self.class.external_field_configs.empty?

  exposures = {}

  self.class.external_field_configs.each do |config|
    field = config.field
    # Check if field is optional (allow_blank or no presence validation)
    is_optional = Axn::Internal::FieldConfig.optional?(config)

    # If method exists, call it (user-defined methods override auto-generated ones)
    # The auto-generated method for exposed-only fields returns nil (field not in provided_data)
    next unless respond_to?(field, true)

    value = send(field)
    # If it returns nil and it's an exposed-only field with no default,
    # it's likely the auto-generated method (user methods can also return nil, but
    # we'll assume it's auto-generated in this case)
    is_exposed_only = !self.class.internal_field_configs.map(&:field).include?(field)
    is_not_in_provided = !@__context.provided_data.key?(field)

    # Only expose if we have a value, or if it's nil but there's a default
    # If it's nil and optional, don't expose - let validation handle it
    if value.nil? && is_exposed_only && is_not_in_provided && config.default.nil? && !is_optional
      # This is the auto-generated method returning nil for a required field
      # Don't expose it - let outbound validation catch the missing exposure
    else
      exposures[field] = value unless value.nil? && config.default.nil?
    end
    # If method doesn't exist:
    # - If optional, skip it - validation will handle it
    # - If not optional and no default, skip it - let outbound validation catch it
    # - If there's a default, skip it - the default will be applied later
  end

  expose(**exposures) if exposures.any?
end