class Rulebow::FileFact

This subclass of Fact is specialized for file change conditions.

Attributes

coerce[R]
pattern[R]

File glob or regular expression.

Public Class Methods

new(pattern, &coerce) click to toggle source

Initialize new instance of FileFact.

pattern - File glob or regular expression. [String,Regexp] digest - The system digest. [Digest]

# File lib/rulebow/fact.rb, line 56
def initialize(pattern, &coerce)
  @pattern = pattern
  @coerce  = coerce
end

Public Instance Methods

call(digest) click to toggle source

Process logic.

# File lib/rulebow/fact.rb, line 68
def call(digest)
  result = []

  case pattern
  when Regexp
    list = Dir.glob('**/*', File::FNM_PATHNAME)
    list = digest.filter(list)  # excludes ignored files (odd way to do it but if should work)
    list.each do |fname|
      if md = pattern.match(fname)
        if digest.current[fname] != digest.saved[fname]
          result << Match.new(fname, md)
        end
      end
    end
    # NOTE: The problem with using the digest list, is that if a rule
    #       adds a new file to the project, then a subsequent rule needs
    #       to be able to see it.
    #@digest.current.keys.each do |fname|
    #  if md = pattern.match(fname)
    #    if @digest.current[fname] != @digest.saved[fname]
    #      result << Match.new(fname, md)
    #    end
    #  end
    #end
  else
    list = Dir.glob(pattern, File::FNM_PATHNAME)
    list = digest.filter(list)   # excludes ignored files (odd way to do it but if should work)
    list.each do |fname|
      if digest.current[fname] != digest.saved[fname]
        result << fname
      end
    end
    #@digest.current.keys.each do |fname|
    #  if md = File.fnmatch?(pattern, fname, File::FNM_PATHNAME | File::FNM_EXTGLOB)
    #    if @digest.current[fname] != @digest.saved[fname]
    #      result << Match.new(fname, md)
    #    end
    #  end
    #end
  end

  if coerce
    return [coerce.call(result)].flatten.compact
  else
    return result
  end
end