Sha256: 132839c8a78b2c6101e2d827cc03c51bfe7c128b1eb8d150a916ea1e09820af2

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

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.gsub("-", "")
          lambda { |plugin| plugin.name == shortname }
        when Regexp
          lambda { |plugin| plugin.name =~ filter }
        when Hash
          lambda do |plugin|
            filter.all? do |k, v|
              case k
              when :name
                plugin.name == v.to_s.downcase.gsub("-", "")
              when :group
                plugin.group.name == v.to_sym
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
guard-2.10.2 lib/guard/internals/plugins.rb
guard-2.10.1 lib/guard/internals/plugins.rb
guard-2.10.0 lib/guard/internals/plugins.rb
guard-2.9.2 lib/guard/internals/plugins.rb
guard-2.9.1 lib/guard/internals/plugins.rb