Skip to content

raindeer-rb/plugs

Repository files navigation

Gem version GitHub repo Codeberg repo

Plugs [UNRELEASED]

Plugs are dependencies that are loosely coupled internally but externally appear as one entity such as a feature, config object or plugin. A plug is reusable, shareable and overridable.

Note

Plugs is for local dependency management. For global dependency management see Providers

Advantages:

  • Keep unit tested classes isolated from their setup
  • Add plugins via a simple :key that do their own setup

Example

Plugs is used by Antlers to configure which elements the Abstract Syntax Tree supports.

require 'plugs'

# Define the dependencies.
class Elements
  include Plugs

  plug(:html) do
    plug(:node) do
      require_relative '../nodes/html_node'
      HTMLNode
    end
  end

  plug(:form) do
    plug(:lexeme) do
      require_relative '../lexemes/form_lexeme'
      FormLexeme
    end

    plug(:node) do
      require_relative '../nodes/form_node'
      FormNode
    end
  end
end

# Require the dependencies:
def new(elements: Elements[:html, :form])
  # => HTMLNode, FormLexeme and FormNode now available.
end

# Return the dependencies:
def new(elements: Elements[:node])
  elements # => [HTMLNode, FormNode]
end

Use Cases

Loosely Coupled Dependencies

Imagine you have two loosely coupled components:

But they are both enabled or disabled at the same time in your application:

Plugs lets you pull them together.

Overriding Dependencies

Say your library uses these dependencies most of the time, but allows other users of the gem to override these dependencies... kinda like as if they were plugins.

Installation

Add gem 'plugs' to your Gemfile then:

bundle install

About

Keep code loosely coupled but keep features tightly coupled

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Packages

 
 
 

Contributors