Class: Ace::Core::Molecules::FrontmatterFreePolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/core/molecules/frontmatter_free_policy.rb

Overview

Shared frontmatter-free policy for path matching and config defaults.

Constant Summary collapse

MATCH_FLAGS =
File::FNM_PATHNAME | File::FNM_EXTGLOB | File::FNM_DOTMATCH
DEFAULT_PATTERNS =
["README.md", "*/README.md"].freeze

Class Method Summary collapse

Class Method Details

.canonical_missing_path(path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ace/core/molecules/frontmatter_free_policy.rb', line 41

def self.canonical_missing_path(path)
  parent = path
  suffix = []
  until File.exist?(parent) || parent == File.dirname(parent)
    suffix.unshift(File.basename(parent))
    parent = File.dirname(parent)
  end
  return path unless File.exist?(parent)

  File.join(File.realpath(parent), *suffix)
end

.canonical_path(path) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/ace/core/molecules/frontmatter_free_policy.rb', line 32

def self.canonical_path(path)
  expanded = File.expand_path(path)
  return File.realpath(expanded) if File.exist?(expanded)

  canonical_missing_path(expanded)
rescue
  File.expand_path(path)
end

.match?(path, patterns:, project_root: Dir.pwd) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ace/core/molecules/frontmatter_free_policy.rb', line 18

def self.match?(path, patterns:, project_root: Dir.pwd)
  return false if path.nil? || path.to_s.empty?
  return false if patterns.nil? || patterns.empty?

  absolute_path = canonical_path(path)
  root = canonical_path(project_root || Dir.pwd)
  relative_path = relative_path_under(absolute_path, root) || absolute_path

  patterns.any? do |pattern|
    File.fnmatch?(pattern, relative_path, MATCH_FLAGS) ||
      File.fnmatch?(pattern, absolute_path, MATCH_FLAGS)
  end
end

.patterns(config:, key: "frontmatter_free", default_patterns: DEFAULT_PATTERNS) ⇒ Object



11
12
13
14
15
16
# File 'lib/ace/core/molecules/frontmatter_free_policy.rb', line 11

def self.patterns(config:, key: "frontmatter_free", default_patterns: DEFAULT_PATTERNS)
  patterns = config[key]
  return patterns if patterns.is_a?(Array) && !patterns.empty?

  default_patterns
end

.relative_path_under(path, root) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/ace/core/molecules/frontmatter_free_policy.rb', line 53

def self.relative_path_under(path, root)
  return "." if path == root

  prefix = "#{root}#{File::SEPARATOR}"
  return path.delete_prefix(prefix) if path.start_with?(prefix)

  nil
end