Skip to content

Creating a plugin

Aaron Christiansen edited this page Jul 25, 2019 · 4 revisions

Overview

Plugins are created by subclassing Parlour::Plugin. Your subclass should define:

  • def generate(root), which is executed once by Parlour. root is the root namespace which you should add your RBI objects to. See this page to learn about how to create these RBI objects.

  • def initialize(options), if your plugin accepts configuration. options is a hash with symbol keys.

A plugin's name is its complete module/class path.

Example Plugin

Code

This code defines a plugin called MyGem::Plugin. It is a simple plugin which generates a number of methods with ascending example names, inside a module called Foo. The number of methods generated can be configured with the quantity option, and defaults to 3.

require 'parlour'

module MyGem
  class Plugin < Parlour::Plugin
    def initialize(options)
      @quantity = options[:quantity] || 3
    end

    def generate(root)
      foo = root.create_module('Foo')
      @quantity.times do |i|
        foo.create_method("method#{i + 1}")   
      end
    end
  end
end

Usage

Suppose the code above was saved into a file called plugin.rb. We could configure this plugin in our .parlour file like so:

output_file: out.rbi

relative_requires:
  - plugin.rb

plugins:
  MyGem::Plugin: {} # no configuration

This places the following RBI into out.rbi:

# typed: strong
module Foo
  sig { void }
  def method1; end

  sig { void }
  def method2; end

  sig { void }
  def method3; end
end

Let's see what happens if we adjust our plugins key to add some configuration to the plugin:

plugins:
  MyGem::Plugin:
    quantity: 1

Now only one method is generated in our RBI:

# typed: strong
module Foo
  sig { void }
  def method1; end
end
Clone this wiki locally