Class: CodeManifest::Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/code_manifest/manifest.rb

Constant Summary collapse

GLOB_OPTIONS =
File::FNM_PATHNAME | File::FNM_DOTMATCH | File::FNM_EXTGLOB

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(patterns) ⇒ Manifest

Returns a new instance of Manifest.



12
13
14
15
16
17
# File 'lib/code_manifest/manifest.rb', line 12

def initialize(patterns)
  @rules ||= Array(patterns).map do |pattern|
    Rule.new(pattern)
  end
  @cache = {}
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



10
11
12
# File 'lib/code_manifest/manifest.rb', line 10

def rules
  @rules
end

Instance Method Details

#digestObject



26
27
28
29
30
31
# File 'lib/code_manifest/manifest.rb', line 26

def digest
  @digest ||= begin
    digests = files.map { |file| Digest::MD5.file(CodeManifest.root.join(file)).hexdigest }
    Digest::MD5.hexdigest(digests.join).freeze
  end
end

#filesObject



19
20
21
22
23
24
# File 'lib/code_manifest/manifest.rb', line 19

def files
  @files ||= begin
    matched_files = matches(Dir.glob(inclusion_rules.map(&:glob), GLOB_OPTIONS, base: CodeManifest.root)).uniq
    files_with_relative_path(matched_files).freeze
  end
end

#matches(paths) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/code_manifest/manifest.rb', line 33

def matches(paths)
  Array(paths).select do |path|
    cached_match =
      if @cache.key?(path)
        @cache.fetch(path)
      else
        @cache[path] = [
          inclusion_rules.any? { |rule| rule.match?(path) },
          exclusion_rules.any? { |rule| rule.match?(path) }
        ]
      end
    cached_match.first && !cached_match.last
  end.sort!
end

#matches_all?(paths) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/code_manifest/manifest.rb', line 48

def matches_all?(paths)
  matches(paths).size == paths.size
end