Module: Dry::Plugins::Host::DSL

Defined in:
lib/dry/plugins/host/dsl.rb

Overview

Mixin used as the DSL of the host class or module

Instance Method Summary collapse

Instance Method Details

#inherited(child) ⇒ Object



81
82
83
84
# File 'lib/dry/plugins/host/dsl.rb', line 81

def inherited(child)
  super(child)
  child.instance_variable_set :@used_plugins, used_plugins.dup
end

#plugins(&block) ⇒ Module, DSL

Returns:

  • (Module, DSL)


75
76
77
78
79
# File 'lib/dry/plugins/host/dsl.rb', line 75

def plugins(&block)
  plugins = const_get(Plugins.config.plugins_module_name)
  return plugins.module_eval(&block) if block_given?
  plugins
end

#plugins_registryRegistry

Returns:

See Also:



# File 'lib/dry/plugins/host/dsl.rb', line 86

#use(*names, &configuration) ⇒ <Symbol>

(Auto)load the plugin called name and apply it to host

In case plugin name is not registered yet, registry will try to resolve that plugin by name

Examples:

class Resource
  extend Dry::Plugins

  module Plugins
    module Persistence
      def persist(something)
        STDOUT.puts "#{something} persisted!"
      end
    end

    register :persistence, Persistence
  end
end

class ArticleResource < Resource
  use :persistence #=> %i[persistence]
end

article = ArticleResource.new
article.persist(:something) # prints `something persisted!`

Parameters:

  • name (Symbol)
  • configuration (Proc)

    optional configuration block

Returns:

  • (<Symbol>)

    names of the currently used plugins



42
43
44
45
46
47
# File 'lib/dry/plugins/host/dsl.rb', line 42

def use(*names, &configuration)
  names.map do |name|
    plugin = plugins.plugins_registry.resolve(name)
    plugin.call(self, &configuration)
  end
end

#used_plugins<Symbol>

Examples:

class Resource
  extend Dry::Plugins

  module Plugins
    module Persistence
      def persist(something)
        STDOUT.puts "#{something} persisted!"
      end
    end

    register :persistence, Persistence
  end
end

class ArticleResource < Resource
  use :persistence #=> %i[persistence]
  used_plugins #=> %i[persistence]
end

Returns:

  • (<Symbol>)


70
71
72
# File 'lib/dry/plugins/host/dsl.rb', line 70

def used_plugins
  @used_plugins ||= Set.new
end