Class: Compony::ManageIntentsDsl

Inherits:
Dslblend::Base
  • Object
show all
Defined in:
lib/compony/manage_intents_dsl.rb

Instance Method Summary collapse

Constructor Details

#initialize(previously_exposed_intents, **intent_opts) ⇒ ManageIntentsDsl

Returns a new instance of ManageIntentsDsl.



3
4
5
6
7
# File 'lib/compony/manage_intents_dsl.rb', line 3

def initialize(previously_exposed_intents, **intent_opts)
  super()
  @exposed_intents = previously_exposed_intents
  @intent_opts = intent_opts
end

Instance Method Details

#add(*args, before: nil, **kwargs) ⇒ Object (protected)

DSL method Adds or replaces an intent to those exposed by the component based on the intent name (override the name if you need to avoid a naming collision). Intents specified this way can be retrieved and rendered by the parent component or by calling root_intents in case of standalone access.

Parameters:

  • before (Symbol) (defaults to: nil)

    If specified, will insert the intent before the other. When replacing, an element keeps its position unless before:` is passed.



15
16
17
18
19
20
21
22
# File 'lib/compony/manage_intents_dsl.rb', line 15

def add(*args, before: nil, **kwargs)
  intent = Compony.intent(*args, **@intent_opts, **kwargs)
  @exposed_intents.natural_push(intent.name, intent, before:)
rescue NameError => e # Ignore if the component is not actually defined
  Rails.logger.debug do
    "Skipping intent for arguments #{args.inspect}, #{kwargs.inspect}: #{e.inspect}."
  end
end

#remove(intent_name) ⇒ Object (protected)

DSL method Removes an exposed intent previously added to this component

Parameters:

  • intent_name (Symbol)

    The name of the intent to remove



27
28
29
30
31
32
# File 'lib/compony/manage_intents_dsl.rb', line 27

def remove(intent_name)
  existing_index = @exposed_intents.find_index { |el| el.name == intent_name.to_sym }
  if existing_index
    @exposed_intents.delete_at(existing_index)
  end
end