File: plugins.rb

package info (click to toggle)
ruby-guard 2.18.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,344 kB
  • sloc: ruby: 9,256; makefile: 6
file content (53 lines) | stat: -rw-r--r-- 1,260 bytes parent folder | download | duplicates (4)
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
require "guard/plugin_util"
require "guard/group"
require "guard/plugin"

module Guard
  # @private api
  module Internals
    class Plugins
      def initialize
        @plugins = []
      end

      def all(filter = nil)
        return @plugins if filter.nil?
        matcher = matcher_for(filter)
        @plugins.select { |plugin| matcher.call(plugin) }
      end

      def remove(plugin)
        @plugins.delete(plugin)
      end

      # TODO: should it allow duplicates? (probably yes because of different
      # configs or groups)
      def add(name, options)
        @plugins << PluginUtil.new(name).initialize_plugin(options)
      end

      private

      def matcher_for(filter)
        case filter
        when String, Symbol
          shortname = filter.to_s.downcase.delete("-")
          ->(plugin) { plugin.name == shortname }
        when Regexp
          ->(plugin) { plugin.name =~ filter }
        when Hash
          lambda do |plugin|
            filter.all? do |k, v|
              case k
              when :name
                plugin.name == v.to_s.downcase.delete("-")
              when :group
                plugin.group.name == v.to_sym
              end
            end
          end
        end
      end
    end
  end
end