class DTK::DSL::FileType::Match

Public Class Methods

matches?(file_type, file_path, opts = {}) click to toggle source

opts can have keys:

:exact (Booelan) - meaning regexp completely matches file_path; otherwise it means jsut match the end
# File lib/dsl/file_type/match.rb, line 29
def self.matches?(file_type, file_path, opts = {})
  new(file_type).matches?(file_path, opts = {})
end
new(file_type) click to toggle source
# File lib/dsl/file_type/match.rb, line 22
def initialize(file_type)
  @file_type = file_type
end

Public Instance Methods

matches?(file_path, opts = {}) click to toggle source
# File lib/dsl/file_type/match.rb, line 33
def matches?(file_path, opts = {})
  file_path = remove_multiple_slashes(file_path)
  if opts[:exact]
    file_path =~ regexp_exact
  else
    # extra check to see if regexp is just for file part or has '/' seperators
    # if just for file then we can have more restrictive match
    if regexp.source =~ Regexp.new('/')
      file_path =~ regexp_match_end
    else
      file_path.split('/').last =~ regexp_exact
    end
  end
end

Private Instance Methods

regexp() click to toggle source
# File lib/dsl/file_type/match.rb, line 54
def regexp
  @regexp ||=  @file_type.regexp
end
regexp_exact() click to toggle source
# File lib/dsl/file_type/match.rb, line 58
def regexp_exact
  @regexp_exact ||= Regexp.new("^#{regexp.source}$")
end
regexp_match_end() click to toggle source
# File lib/dsl/file_type/match.rb, line 62
def regexp_match_end
  @regexp_match_end ||= Regexp.new("#{regexp.source}$")
end
remove_multiple_slashes(string) click to toggle source
# File lib/dsl/file_type/match.rb, line 50
def remove_multiple_slashes(string)
  string.split('/').reject { |el| el.empty?}.join('/')
end