Class: KFileset::GlobEntry

Inherits:
Object
  • Object
show all
Includes:
GlobProps
Defined in:
lib/k_fileset/glob_entry.rb

Overview

Glob entry can be used to find <PathEntries> that match a Glob with optional exclusion patterns and flags

Instance Attribute Summary

Attributes included from GlobProps

#exclusions, #flags, #glob, #working_directory

Instance Method Summary collapse

Constructor Details

#initialize(working_directory, glob, flags, exclusions) ⇒ GlobEntry

Returns a new instance of GlobEntry.



9
10
11
12
13
14
# File 'lib/k_fileset/glob_entry.rb', line 9

def initialize(working_directory, glob, flags, exclusions)
  @working_directory = working_directory
  @glob = glob
  @flags = flags
  @exclusions = exclusions
end

Instance Method Details

#match?(absolute_file) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/k_fileset/glob_entry.rb', line 27

def match?(absolute_file)
  # match the inclusion Glob first

  # Using absolute files because two sibling Globs may have different exclusion rules
  # and the incoming file needs to be tested against the correct Glob and it's exclusions
  absolute_glob = File.expand_path(glob, path)

  return false unless File.fnmatch?(absolute_glob, absolute_file)

  true
end

#path_entriesObject

Dealing with edge cases: bugs.ruby-lang.org/issues/17280



19
20
21
22
23
24
25
# File 'lib/k_fileset/glob_entry.rb', line 19

def path_entries
  Dir.chdir(working_directory) do
    Dir.glob(glob, flags)
       .reject { |file| exclusions.any? { |pattern| pattern_match?(pattern, file) } }
       .map { |file| PathEntry.new(file) }
  end
end