Module: Axn::Mountable::MountingStrategies::Method

Extended by:
Method
Includes:
Base
Included in:
Method
Defined in:
lib/axn/mountable/mounting_strategies/method.rb

Defined Under Namespace

Modules: DSL

Instance Method Summary collapse

Methods included from Base

#_should_raise_method_collision_error?, #define_namespace_methods, #key, #mount, #mount_method, #mount_to_namespace

Instance Method Details

#default_inherit_modeObject



10
# File 'lib/axn/mountable/mounting_strategies/method.rb', line 10

def default_inherit_mode = :lifecycle

#mount_to_target(descriptor:, target:) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/axn/mountable/mounting_strategies/method.rb', line 57

def mount_to_target(descriptor:, target:)
  name = descriptor.name

  mount_method(target:, method_name: "#{name}!") do |**kwargs|
    # Lazy load the action class for the current target
    axn_klass = descriptor.mounted_axn_for(target: self)

    # Determine expose_return_as by introspecting the axn class
    exposed_fields = axn_klass.external_field_configs.map(&:field)
    expose_return_as = exposed_fields.size == 1 ? exposed_fields.first : nil

    result = axn_klass.call!(**kwargs)
    return result if expose_return_as.nil?

    result.public_send(expose_return_as)
  end
end

#preprocess_kwargs(**kwargs) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/axn/mountable/mounting_strategies/method.rb', line 29

def preprocess_kwargs(**kwargs)
  # Call parent preprocessing first
  processed_kwargs = super

  # Methods require a return value
  processed_kwargs[:expose_return_as] = processed_kwargs[:expose_return_as].presence || :value

  # Methods aren't capable of returning multiple values
  if processed_kwargs[:exposes].present?
    raise MountingError,
          "Methods aren't capable of exposing multiple values (will automatically expose return value instead)"
  end

  # Check for existing axn class with multiple exposed fields
  if processed_kwargs[:axn_klass].present?
    axn_klass = processed_kwargs[:axn_klass]
    exposed_fields = axn_klass.external_field_configs.map(&:field)

    if exposed_fields.size > 1
      raise MountingError,
            "Cannot determine expose_return_as for existing axn class with multiple exposed fields: #{exposed_fields.join(', ')}. " \
            "Use a fresh block with mount_axn_method or ensure the axn class has exactly one exposed field."
    end
  end

  processed_kwargs
end

#strategy_specific_kwargsObject



27
# File 'lib/axn/mountable/mounting_strategies/method.rb', line 27

def strategy_specific_kwargs = super + [:expose_return_as]