File: template.rb

package info (click to toggle)
ruby-guard-compat 1.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 140 kB
  • sloc: ruby: 350; makefile: 3
file content (68 lines) | stat: -rw-r--r-- 1,780 bytes parent folder | download | duplicates (3)
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
module Guard
  module Compat
    module Test
      class Template
        class Session
          class MultipleGuardNotImplemented < NotImplementedError
            def message
              'multiple guards not supported!'
            end
          end

          class GlobalWatchesNotImplemented < NotImplementedError
            def message
              'global watches not supported!'
            end
          end

          def initialize(path, content)
            @watches = {}
            @current = nil
            instance_eval(content, path, 1)
          end

          def match(file)
            _watches.map do |expr, block|
              next unless (match = file.match(expr))
              block.nil? ? [file] : block.call([file] + match.captures)
            end.flatten.compact.uniq
          end

          def guard(name, _options = {})
            @current = name
            @watches[@current] = []
            yield
            @current = nil
          end

          def watch(expr, &block)
            @watches[@current] << [expr, block]
          end

          private

          def _watches
            keys = @watches.keys
            fail ArgumentError, 'no watches!' if keys.empty?
            fail MultipleGuardNotImplemented if keys.size > 1

            key = keys.first
            fail GlobalWatchesNotImplemented if key.nil?
            @watches[key]
          end
        end

        def initialize(plugin_class)
          name = plugin_class.to_s.sub('Guard::', '').downcase
          path = format('lib/guard/%s/templates/Guardfile', name)
          content = File.read(path)
          @session = Session.new(path, content)
        end

        def changed(file)
          @session.match(file)
        end
      end
    end
  end
end