Class: Valise::PathMatcher

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Unpath
Defined in:
lib/valise/path-matcher.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Unpath

#clean_pathname, #containing_workspace, #current_directory, #file_from_backtrace, #from_here, #make_pathname, #starting_directory, #up_to, #up_until

Constructor Details

#initialize(first_pattern = nil, first_value = true) ⇒ PathMatcher

Returns a new instance of PathMatcher.



14
15
16
17
# File 'lib/valise/path-matcher.rb', line 14

def initialize(first_pattern=nil, first_value=true)
  @pattern_pairs = []
  set(first_pattern, first_value) unless first_pattern.nil?
end

Class Method Details

.build(path, value = true) ⇒ Object



8
9
10
11
12
# File 'lib/valise/path-matcher.rb', line 8

def self.build(path, value = true)
  return path if PathMatcher === path
  path = Unpath::make_pathname(path).to_s
  self.new(path, value)
end

Instance Method Details

#===(path) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/valise/path-matcher.rb', line 19

def ===(path)
  path = make_pathname(path)
  fetch(path)
  return true
rescue KeyError
  return false
end

#[](path) ⇒ Object



36
37
38
39
40
# File 'lib/valise/path-matcher.rb', line 36

def [](path)
  fetch(path)
rescue KeyError
  nil
end

#each_pairObject



50
51
52
53
54
# File 'lib/valise/path-matcher.rb', line 50

def each_pair
  @pattern_pairs.each do |pattern, value|
    yield pattern, value
  end
end

#fetch(path) ⇒ Object

Raises:

  • (KeyError)


27
28
29
30
31
32
33
34
# File 'lib/valise/path-matcher.rb', line 27

def fetch(path)
  @pattern_pairs.each do |pattern, value|
    if path.fnmatch?(pattern)
      return value
    end
  end
  raise KeyError, "No pattern matches #{path.to_s}"
end

#merge!(other) ⇒ Object



56
57
58
59
60
# File 'lib/valise/path-matcher.rb', line 56

def merge!(other)
  other.each_pair do |pattern, value|
    set(pattern, value)
  end
end

#set(pattern, value) ⇒ Object Also known as: []=



42
43
44
45
46
47
# File 'lib/valise/path-matcher.rb', line 42

def set(pattern, value)
  @pattern_pairs.delete_if do |old_pattern, _|
    pattern == old_pattern
  end
  @pattern_pairs << [pattern.to_s, value]
end