Class: Valise::PathMatcher
- Inherits:
-
Object
- Object
- Valise::PathMatcher
show all
- Includes:
- 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, #repath, #string_to_segments, #unpath
Constructor Details
#initialize(segment = nil) ⇒ PathMatcher
Returns a new instance of PathMatcher.
20
21
22
23
24
|
# File 'lib/valise/path-matcher.rb', line 20
def initialize(segment = nil)
@children = []
@segment = segment
@value = nil
end
|
Instance Attribute Details
#segment ⇒ Object
Returns the value of attribute segment.
26
27
28
|
# File 'lib/valise/path-matcher.rb', line 26
def segment
@segment
end
|
Class Method Details
.build(path, value = true) ⇒ Object
7
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/valise/path-matcher.rb', line 7
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
74
75
76
|
# File 'lib/valise/path-matcher.rb', line 74
def ===(path)
return !!self[path]
end
|
#[](path) ⇒ Object
45
46
47
|
# File 'lib/valise/path-matcher.rb', line 45
def [](path)
retreive(unpath(path))
end
|
#[]=(pattern, result) ⇒ Object
70
71
72
|
# File 'lib/valise/path-matcher.rb', line 70
def []=(pattern, result)
store(unpath(pattern), result)
end
|
#access(segments) ⇒ Object
61
62
63
64
|
# File 'lib/valise/path-matcher.rb', line 61
def access(segments)
return retreive(segments.drop(1)) if match?(segments.first)
return nil
end
|
#each_pair(prefix = []) {|segments, @value| ... } ⇒ Object
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/valise/path-matcher.rb', line 28
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
66
67
68
|
# File 'lib/valise/path-matcher.rb', line 66
def match?(segment)
@segment == segment
end
|
#merge!(other) ⇒ Object
39
40
41
42
43
|
# File 'lib/valise/path-matcher.rb', line 39
def merge!(other)
other.each_pair do |path, value|
self[path] = value
end
end
|
#retreive(segments) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/valise/path-matcher.rb', line 49
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/valise/path-matcher.rb', line 78
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 << m}
end
target.store(segments, result)
end
end
|