File: tracker.rb

package info (click to toggle)
ruby-rabl 0.16.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,276 kB
  • sloc: ruby: 6,732; javascript: 102; makefile: 6
file content (53 lines) | stat: -rw-r--r-- 1,325 bytes parent folder | download | duplicates (5)
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
module Rabl
  # DependencyTracker for ActionView to support cache digest
  class Tracker
    # Matches:
    #   extends "categories/show"
    EXTENDS_DEPENDENCY = /
      extends\s*              # extends, followed by optional whitespace
      \(?                     # start an optional parenthesis for the extends call
      \s*["']([0-9a-z_\/\.]+) # the template name itself
    /x

    # Matches:
    #   partial "categories/show"
    PARTIAL_DEPENDENCY = /
      partial\s*              # partial, followed by optional whitespace
      \(?                     # start an optional parenthesis for the partial call
      \s*["']([0-9a-z_\/\.]+) # the template name itself
    /x

    def self.call(name, template)
      new(name, template).dependencies
    end

    def initialize(name, template)
      @name, @template = name, template
    end

    def dependencies
      (extends_dependencies + partial_dependencies).uniq
    end

    attr_reader :name, :template
    private :name, :template

    private

      def source
        template.source
      end

      def directory
        name.split("/")[0..-2].join("/")
      end

      def extends_dependencies
        source.scan(EXTENDS_DEPENDENCY).flatten
      end

      def partial_dependencies
        source.scan(PARTIAL_DEPENDENCY).flatten
      end
  end
end