class Nandi::FileMatcher

Constants

TIMESTAMP_REGEX

Attributes

files[R]
spec[R]

Public Class Methods

call(*args, **kwargs) click to toggle source
# File lib/nandi/file_matcher.rb, line 7
def self.call(*args, **kwargs)
  new(*args, **kwargs).call
end
new(files:, spec:) click to toggle source
# File lib/nandi/file_matcher.rb, line 11
def initialize(files:, spec:)
  @files = Set.new(files)
  @spec = spec
end

Public Instance Methods

call() click to toggle source
# File lib/nandi/file_matcher.rb, line 16
def call
  case spec
  when "all"
    Set.new(
      files.reject { |f| ignored_filenames.include?(File.basename(f)) },
    )
  when "git-diff"
    files.intersection(files_from_git_status)
  when TIMESTAMP_REGEX
    match_timestamp
  end
end

Private Instance Methods

files_from_git_status() click to toggle source
# File lib/nandi/file_matcher.rb, line 62
def files_from_git_status
  `
    git status --porcelain --short --untracked-files=all |
    cut -c4- |
    xargs -n1 basename
  `.lines.map(&:strip)
end
ignored_filenames() click to toggle source
# File lib/nandi/file_matcher.rb, line 39
def ignored_filenames
  ignored_files.map(&File.method(:basename))
end
ignored_files() click to toggle source
# File lib/nandi/file_matcher.rb, line 31
def ignored_files
  @ignored_files ||= if File.exist?(".nandiignore")
                       File.read(".nandiignore").lines.map(&:strip)
                     else
                       []
                     end
end
match_timestamp() click to toggle source
# File lib/nandi/file_matcher.rb, line 43
def match_timestamp
  match = TIMESTAMP_REGEX.match(spec)

  case match[:operator]
  when nil
    files.select do |file|
      file.start_with?(match[:timestamp])
    end
  when ">"
    migrations_after((Integer(match[:timestamp]) + 1).to_s)
  when ">="
    migrations_after(match[:timestamp])
  end.to_set
end
migrations_after(minimum) click to toggle source
# File lib/nandi/file_matcher.rb, line 58
def migrations_after(minimum)
  files.select { |file| file >= minimum }
end