Class: Eco::Data::Files::FilePattern

Inherits:
Object
  • Object
show all
Defined in:
lib/eco/data/files/file_pattern.rb

Overview

Helper class to build regex patterns relative to a source folder.

Instance Method Summary collapse

Constructor Details

#initialize(file = '') ⇒ FilePattern

Returns a new instance of FilePattern.



6
7
8
# File 'lib/eco/data/files/file_pattern.rb', line 6

def initialize(file = '')
  @source_file = file
end

Instance Method Details

#any?(files, dir: nil) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/eco/data/files/file_pattern.rb', line 18

def any?(files, dir: nil)
  return false unless files.is_a?(Array)

  files.any? { |file| match?(file, dir: dir) }
end

#match?(file, dir: nil) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/eco/data/files/file_pattern.rb', line 14

def match?(file, dir: nil)
  /#{Regexp.escape(pattern(dir))}/i.match?(file)
end

#pattern(dir = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/eco/data/files/file_pattern.rb', line 24

def pattern(dir = nil)
  return File.join(dir, '*') if @source_file.to_s.empty?

  filename = File.basename(@source_file)
  path     = File.dirname(@source_file)

  path     = File.join(dir, path) if dir

  wildcard = '*'
  wildcard = '' if filename =~ /\*/

  File.join(path, wildcard + filename)
end

#resolve(dir: nil, start: '') ⇒ Object



10
11
12
# File 'lib/eco/data/files/file_pattern.rb', line 10

def resolve(dir: nil, start: '')
  pattern(dir).gsub('*', start)
end