Class: Valise::PathMatcher

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

Defined Under Namespace

Classes: Pattern

Constant Summary collapse

DEFAULT_FLAGS =
File::FNM_EXTGLOB

Instance Attribute Summary collapse

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, first_flags = nil) ⇒ PathMatcher

Returns a new instance of PathMatcher.



53
54
55
56
57
# File 'lib/valise/path-matcher.rb', line 53

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

Instance Attribute Details

#default_flagsObject

Returns the value of attribute default_flags.



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

def default_flags
  @default_flags
end

Class Method Details

.build(path, value = true, flags = nil) ⇒ Object



46
47
48
49
50
# File 'lib/valise/path-matcher.rb', line 46

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

Instance Method Details

#===(path) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/valise/path-matcher.rb', line 60

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

#[](path) ⇒ Object



77
78
79
80
81
# File 'lib/valise/path-matcher.rb', line 77

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

#add_pattern(pattern) ⇒ Object



88
89
90
91
92
93
# File 'lib/valise/path-matcher.rb', line 88

def add_pattern(pattern)
  @pattern_pairs.delete_if do |old_pattern|
    pattern.match == old_pattern.match
  end
  @pattern_pairs << pattern
end

#each(&block) ⇒ Object



95
96
97
# File 'lib/valise/path-matcher.rb', line 95

def each(&block)
  @pattern_pairs.each(&block)
end

#fetch(path) ⇒ Object

Raises:

  • (KeyError)


68
69
70
71
72
73
74
75
# File 'lib/valise/path-matcher.rb', line 68

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

#merge!(other) ⇒ Object



99
100
101
102
103
# File 'lib/valise/path-matcher.rb', line 99

def merge!(other)
  other.each do |pattern|
    add_pattern(pattern)
  end
end

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



83
84
85
# File 'lib/valise/path-matcher.rb', line 83

def set(pattern, value, flags = nil)
  add_pattern(Pattern.new(pattern.to_s, value, flags))
end