Class: Axn::Mountable::Descriptor
- Inherits:
-
Object
- Object
- Axn::Mountable::Descriptor
- Defined in:
- lib/axn/mountable/descriptor.rb
Overview
Descriptor holds the information needed to mount an action
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#existing_axn_klass ⇒ Object
readonly
Returns the value of attribute existing_axn_klass.
-
#kwargs ⇒ Object
readonly
Returns the value of attribute kwargs.
-
#mount_strategy ⇒ Object
readonly
Returns the value of attribute mount_strategy.
-
#mounted_axn ⇒ Object
readonly
Returns the value of attribute mounted_axn.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#raw_kwargs ⇒ Object
readonly
Returns the value of attribute raw_kwargs.
Instance Method Summary collapse
-
#initialize(name:, as:, axn_klass: nil, block: nil, kwargs: {}) ⇒ Descriptor
constructor
A new instance of Descriptor.
- #mount(target:) ⇒ Object
- #mounted? ⇒ Boolean
- #mounted_axn_for(target:) ⇒ Object
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
#block ⇒ Object (readonly)
Returns the value of attribute block.
7 8 9 |
# File 'lib/axn/mountable/descriptor.rb', line 7 def block @block end |
#existing_axn_klass ⇒ Object (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 |
#kwargs ⇒ Object (readonly)
Returns the value of attribute kwargs.
7 8 9 |
# File 'lib/axn/mountable/descriptor.rb', line 7 def kwargs @kwargs end |
#mount_strategy ⇒ Object (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_axn ⇒ Object (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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/axn/mountable/descriptor.rb', line 7 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/axn/mountable/descriptor.rb', line 7 def @options end |
#raw_kwargs ⇒ Object (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
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 |