lib/rulebow/watchlist.rb
# Given a pattern and a file, does the file match the
# pattern? This code is based on the rules used by
# git's .gitignore file.
#
# TODO: The code is probably not quite right.
#
# TODO: Handle regular expressions.
#
# Returns [Boolean]
Given a pattern and a file, does the file match the
pattern. This code is based on the rules used by
git's .gitignore file.

The code is probably not quite right.

Handle regular expressions.

Returns [Boolean]
Given a pattern and a file does the file match the pattern . This code is based on the rules used by git ' s . gitignore file . The code is probably not quite right . Handle regular expressions . Returns [ Boolean ]
def match?(pattern, file)
      if Regexp === pattern
        return pattern.match(file) ? true : false
      end
      if pattern.start_with?('!')
        return !match?(pattern.sub('!','').strip)
      end
      dir = pattern.end_with?('/')
      pattern = pattern.chomp('/') if dir
      if pattern.start_with?('/')
        fnmatch?(pattern.sub('/',''), file)
      else
        if dir
          fnmatch?(File.join(pattern, '**', '*'), file) ||
          fnmatch?(pattern, file) && File.directory?(file)
        elsif pattern.include?('/')
          fnmatch?(pattern, file)
        else
          fnmatch?(File.join('**',pattern), file)
        end
      end
    end
def match? ( pattern , file ) if Regexp === pattern return pattern . match ( file ) ? true : false end if pattern . start_with? ( '!' ) return ! match? ( pattern . sub ( '!' , '' ) . strip ) end dir = pattern . end_with? ( '/' ) pattern = pattern . chomp ( '/' ) if dir if pattern . start_with? ( '/' ) fnmatch? ( pattern . sub ( '/' , '' ) , file ) else if dir fnmatch? ( File . join ( pattern , '**' , '*' ) , file ) || fnmatch? ( pattern , file ) && File . directory? ( file ) elsif pattern . include? ( '/' ) fnmatch? ( pattern , file ) else fnmatch? ( File . join ( '**' , pattern ) , file ) end end end
Given a pattern and a file, does the file match the
pattern?
Given a pattern and a file does the file match the pattern?
pattern
<not_specific>
file
<not_specific>