File: plugin.rb

package info (click to toggle)
ruby-ddplugin 1.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 132 kB
  • sloc: ruby: 154; makefile: 3
file content (67 lines) | stat: -rw-r--r-- 1,835 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
59
60
61
62
63
64
65
66
67
# frozen_string_literal: true

module DDPlugin
  # A module that contains class methods for plugins. It provides functions
  # for setting identifiers and finding plugins. Plugin classes should extend
  # this module.
  module Plugin
    # @overload identifiers(*identifiers)
    #
    #   Sets the identifiers for this class.
    #
    #   @param [Array<Symbol, String>] identifiers A list of identifiers to
    #     assign to this class.
    #
    #   @return [void]
    #
    # @overload identifiers
    #
    #   @return [Array<Symbol>] The identifiers for this class
    def identifiers(*identifiers)
      if identifiers.empty?
        DDPlugin::Registry.instance.identifiers_of(root_class, self)
      else
        DDPlugin::Registry.instance.register(root_class, self, *identifiers)
      end
    end

    # @return [Class] The root class for this class
    def root_class
      klass = self
      klass = klass.superclass while klass.superclass.respond_to?(:identifiers)
      klass
    end

    # @overload identifier(identifier)
    #
    #   Sets the identifier for this class.
    #
    #   @param [Symbol, String] identifier The identifier to assign to this
    #     class.
    #
    #   @return [void]
    #
    # @overload identifier
    #
    #   @return [Symbol] The first identifier for this class
    def identifier(identifier = nil)
      if identifier
        identifiers(identifier)
      else
        identifiers.first
      end
    end

    # @return [Enumerable<Class>] All classes of this type
    def all
      DDPlugin::Registry.instance.find_all(self)
    end

    # @param [Symbol, String] identifier The identifier of the class to find
    #
    # @return [Class] The class with the given identifier
    def named(identifier)
      DDPlugin::Registry.instance.find(self, identifier)
    end
  end
end