Class: Axn::Mountable::Descriptor

Inherits:
Object
  • Object
show all
Defined in:
lib/axn/mountable/descriptor.rb

Overview

Descriptor holds the information needed to mount an action

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, as:, axn_klass: nil, block: nil, kwargs: {}) ⇒ Descriptor

Returns a new instance of Descriptor.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/axn/mountable/descriptor.rb', line 9

def initialize(name:, as:, axn_klass: nil, block: nil, kwargs: {})
  @mount_strategy = MountingStrategies.find(as)
  @existing_axn_klass = axn_klass

  @name = name
  @block = block
  @raw_kwargs = kwargs

  @kwargs = mount_strategy.preprocess_kwargs(**kwargs.except(*mount_strategy.strategy_specific_kwargs), axn_klass:)
  @options = kwargs.slice(*mount_strategy.strategy_specific_kwargs)

  @validator = Helpers::Validator.new(self)

  @validator.validate!
  freeze
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



7
8
9
# File 'lib/axn/mountable/descriptor.rb', line 7

def block
  @block
end

#existing_axn_klassObject (readonly)

Returns the value of attribute existing_axn_klass.



7
8
9
# File 'lib/axn/mountable/descriptor.rb', line 7

def existing_axn_klass
  @existing_axn_klass
end

#kwargsObject (readonly)

Returns the value of attribute kwargs.



7
8
9
# File 'lib/axn/mountable/descriptor.rb', line 7

def kwargs
  @kwargs
end

#mount_strategyObject (readonly)

Returns the value of attribute mount_strategy.



7
8
9
# File 'lib/axn/mountable/descriptor.rb', line 7

def mount_strategy
  @mount_strategy
end

#mounted_axnObject (readonly)

Returns the value of attribute mounted_axn.



7
8
9
# File 'lib/axn/mountable/descriptor.rb', line 7

def mounted_axn
  @mounted_axn
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/axn/mountable/descriptor.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/axn/mountable/descriptor.rb', line 7

def options
  @options
end

#raw_kwargsObject (readonly)

Returns the value of attribute raw_kwargs.



7
8
9
# File 'lib/axn/mountable/descriptor.rb', line 7

def raw_kwargs
  @raw_kwargs
end

Instance Method Details

#mount(target:) ⇒ Object



26
27
28
29
# File 'lib/axn/mountable/descriptor.rb', line 26

def mount(target:)
  validate_before_mount!(target:)
  mount_strategy.mount(descriptor: self, target:)
end

#mounted?Boolean

Returns:

  • (Boolean)


60
# File 'lib/axn/mountable/descriptor.rb', line 60

def mounted? = @mounted_axn.present?

#mounted_axn_for(target:) ⇒ Object



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
56
57
58
# File 'lib/axn/mountable/descriptor.rb', line 31

def mounted_axn_for(target:)
  # Check if the target already has this action class cached
  cache_key = "#{@name}_#{object_id}_#{target.object_id}"

  # Use a class variable to store the cache on the target
  cache_var = :@_axn_cache
  target.instance_variable_set(cache_var, {}) unless target.instance_variable_defined?(cache_var)
  cache = target.instance_variable_get(cache_var)

  return cache[cache_key] if cache.key?(cache_key)

  # Check if constant is already registered
  action_class_builder = Helpers::ClassBuilder.new(self)
  namespace = Helpers::NamespaceManager.get_or_create_namespace(target)
  constant_name = action_class_builder.generate_constant_name(@name.to_s)
  if namespace.const_defined?(constant_name, false)
    mounted_axn = namespace.const_get(constant_name)
    cache[cache_key] = mounted_axn
    return mounted_axn
  end

  # Build and configure action class
  mounted_axn = action_class_builder.build_and_configure_action_class(target, @name.to_s, namespace)

  # Cache on the target
  cache[cache_key] = mounted_axn
  mounted_axn
end