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

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

Instance Method Summary collapse

Constructor Details

#initialize(file = "") ⇒ FilePattern

Returns a new instance of FilePattern.



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

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

Instance Method Details

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

Returns:

  • (Boolean)


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

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)


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

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

#pattern(dir = nil) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/eco/data/files/file_pattern.rb', line 22

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 = (filename =~ /\*/)? "" : "*"
  File.join(path, wildcard + filename)
end

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



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

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