Class: Valise::PathMatcher
- Inherits:
-
Object
- Object
- Valise::PathMatcher
- Includes:
- Unpath
- Defined in:
- lib/valise/path-matcher.rb
Instance Attribute Summary collapse
-
#segment ⇒ Object
readonly
Returns the value of attribute segment.
Instance Method Summary collapse
- #[](path) ⇒ Object
- #[]=(pattern, result) ⇒ Object
- #access(segments) ⇒ Object
- #each_pair(prefix = []) {|segments, @value| ... } ⇒ Object
-
#initialize(segment = nil) ⇒ PathMatcher
constructor
A new instance of PathMatcher.
- #match?(segment) ⇒ Boolean
- #merge!(other) ⇒ Object
- #retreive(segments) ⇒ Object
- #store(segments, result) ⇒ Object
Methods included from Unpath
Constructor Details
#initialize(segment = nil) ⇒ PathMatcher
Returns a new instance of PathMatcher.
7 8 9 10 11 |
# File 'lib/valise/path-matcher.rb', line 7 def initialize(segment = nil) @children = [] @segment = segment @value = nil end |
Instance Attribute Details
#segment ⇒ Object (readonly)
Returns the value of attribute segment.
13 14 15 |
# File 'lib/valise/path-matcher.rb', line 13 def segment @segment end |
Instance Method Details
#[](path) ⇒ Object
32 33 34 |
# File 'lib/valise/path-matcher.rb', line 32 def [](path) retreive(unpath(path)) end |
#[]=(pattern, result) ⇒ Object
57 58 59 |
# File 'lib/valise/path-matcher.rb', line 57 def []=(pattern, result) store(unpath(pattern), result) end |
#access(segments) ⇒ Object
48 49 50 51 |
# File 'lib/valise/path-matcher.rb', line 48 def access(segments) return retreive(segments.drop(1)) if match?(segments.first) return nil end |
#each_pair(prefix = []) {|segments, @value| ... } ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/valise/path-matcher.rb', line 15 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
53 54 55 |
# File 'lib/valise/path-matcher.rb', line 53 def match?(segment) @segment == segment end |
#merge!(other) ⇒ Object
26 27 28 29 30 |
# File 'lib/valise/path-matcher.rb', line 26 def merge!(other) other.each_pair do |path, value| self[path] = value end end |
#retreive(segments) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/valise/path-matcher.rb', line 36 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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/valise/path-matcher.rb', line 61 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 |