Class: Valise::PathMatcher
- Inherits:
-
Object
- Object
- Valise::PathMatcher
show all
- Includes:
- Enumerable, Unpath
- Defined in:
- lib/valise/path-matcher.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Unpath
#collapse, file_from_backtrace, #file_from_backtrace, #from_here, from_here, repath, string_to_segments, unpath, up_to, #up_to
Constructor Details
#initialize(segment = nil) ⇒ PathMatcher
Returns a new instance of PathMatcher.
21
22
23
24
25
|
# File 'lib/valise/path-matcher.rb', line 21
def initialize(segment = nil)
@children = []
@segment = segment
@value = nil
end
|
Instance Attribute Details
#segment ⇒ Object
Returns the value of attribute segment.
27
28
29
|
# File 'lib/valise/path-matcher.rb', line 27
def segment
@segment
end
|
Class Method Details
.build(path, value = true) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/valise/path-matcher.rb', line 8
def self.build(path, value = true)
case path
when PathMatcher
return path
when String, Array
matcher = PathMatcher.new
matcher[path] = value
return matcher
else
raise ArgumentError, "Path matchers can only be built from arrays or strings"
end
end
|
Instance Method Details
#===(path) ⇒ Object
81
82
83
|
# File 'lib/valise/path-matcher.rb', line 81
def ===(path)
return !!self[path]
end
|
#[](path) ⇒ Object
52
53
54
|
# File 'lib/valise/path-matcher.rb', line 52
def [](path)
retreive(unpath(path))
end
|
#[]=(pattern, result) ⇒ Object
77
78
79
|
# File 'lib/valise/path-matcher.rb', line 77
def []=(pattern, result)
store(unpath(pattern), result)
end
|
#access(segments) ⇒ Object
68
69
70
71
|
# File 'lib/valise/path-matcher.rb', line 68
def access(segments)
return retreive(segments.drop(1)) if match?(segments.first)
return nil
end
|
#each(prefix = []) ⇒ Object
40
41
42
43
44
|
# File 'lib/valise/path-matcher.rb', line 40
def each(prefix = [])
each_pair do |segments, value|
yield(segments) if !!value
end
end
|
#each_pair(prefix = []) {|segments, @value| ... } ⇒ Object
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/valise/path-matcher.rb', line 29
def each_pair(prefix = [])
segments = prefix.dup
segments << @segment if @segment
@children.each do |child|
child.each_pair(segments) do |segments, value|
yield(segments, value)
end
end
yield(segments, @value) if @value
end
|
#match?(segment) ⇒ Boolean
73
74
75
|
# File 'lib/valise/path-matcher.rb', line 73
def match?(segment)
@segment == segment
end
|
#merge!(other) ⇒ Object
46
47
48
49
50
|
# File 'lib/valise/path-matcher.rb', line 46
def merge!(other)
other.each_pair do |path, value|
self[path] = value
end
end
|
#retreive(segments) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/valise/path-matcher.rb', line 56
def retreive(segments)
if segments.empty?
return @value
else
@children.each do |child|
val = child.access(segments)
return val unless val.nil?
end
end
return nil
end
|
#store(segments, result) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/valise/path-matcher.rb', line 85
def store(segments, result)
if segments.empty?
@value = result
else
index = segments.shift
target = @children.find {|child| child.segment == index } ||
case index
when "**"; DirGlob.new.tap{|m| @children << m}
when /.*[*].*/; FileGlob.new(index).tap{|m| @children << m}
else; PathMatcher.new(index).tap{|m| @children.unshift m}
end
target.store(segments, result)
end
end
|