File: config.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 (70 lines) | stat: -rw-r--r-- 1,654 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require "guard/options"
require "guard/ui/logger"

module Guard
  module UI
    class Config < Guard::Options
      DEFAULTS = {
        only: nil,
        except: nil,

        # nil (will be whatever $stderr is later) or LumberJack device, e.g.
        # $stderr or 'foo.log'
        device: nil,
      }.freeze

      DEPRECATED_OPTS = %w(template time_format level progname).freeze

      attr_reader :logger_config

      def initialize(options = {})
        opts = Guard::Options.new(options, DEFAULTS)

        # migrate old options stored in UI config directly
        deprecated_logger_opts = {}
        DEPRECATED_OPTS.each do |option|
          if opts.key?(option)
            deprecated_logger_opts[option.to_sym] = opts.delete(option)
          end
        end

        @logger_config = Logger::Config.new(deprecated_logger_opts)
        super(opts.to_hash)
      end

      def device
        # Use strings to work around Thor's indifferent Hash's bug
        fetch("device") || $stderr
      end

      def only
        fetch("only")
      end

      def except
        fetch("except")
      end

      def [](name)
        name = name.to_s

        # TODO: remove in Guard 3.x
        return logger_config[name] if DEPRECATED_OPTS.include?(name)
        return device if name == "device"

        # let Thor's Hash handle anything else
        super(name.to_s)
      end

      def with_progname(name)
        if Guard::UI.logger.respond_to?(:set_progname)
          Guard::UI.logger.set_progname(name) do
            yield if block_given?
          end
        elsif block_given?
          yield
        end
      end
    end
  end
end