Class: RVC::FS
- Inherits:
-
Object
- Object
- RVC::FS
- Defined in:
- lib/rvc/fs.rb
Constant Summary collapse
- MARK_PATTERN =
/^~(?:([\d\w]*|~|@))$/
- REGEX_PATTERN =
/^%/
- GLOB_PATTERN =
/\*/
Instance Attribute Summary collapse
-
#loc ⇒ Object
readonly
Returns the value of attribute loc.
-
#marks ⇒ Object
readonly
Returns the value of attribute marks.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #cd(new_loc) ⇒ Object
- #cur ⇒ Object
- #display_path ⇒ Object
- #glob_to_regex(str) ⇒ Object
-
#initialize(root) ⇒ FS
constructor
A new instance of FS.
- #lookup(path) ⇒ Object
- #lookup_loc(path) ⇒ Object
- #mark(key, loc) ⇒ Object
-
#traverse(base_loc, els) ⇒ Object
Starting from base_loc, traverse each path element in els.
- #traverse_one(loc, el, first) ⇒ Object
Constructor Details
Instance Attribute Details
#loc ⇒ Object (readonly)
Returns the value of attribute loc.
53 54 55 |
# File 'lib/rvc/fs.rb', line 53 def loc @loc end |
#marks ⇒ Object (readonly)
Returns the value of attribute marks.
53 54 55 |
# File 'lib/rvc/fs.rb', line 53 def marks @marks end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
53 54 55 |
# File 'lib/rvc/fs.rb', line 53 def root @root end |
Instance Method Details
#cd(new_loc) ⇒ Object
73 74 75 76 |
# File 'lib/rvc/fs.rb', line 73 def cd new_loc mark '~', @loc @loc = new_loc end |
#cur ⇒ Object
65 66 67 |
# File 'lib/rvc/fs.rb', line 65 def cur @loc.obj end |
#display_path ⇒ Object
69 70 71 |
# File 'lib/rvc/fs.rb', line 69 def display_path @loc.path * '/' end |
#glob_to_regex(str) ⇒ Object
143 144 145 |
# File 'lib/rvc/fs.rb', line 143 def glob_to_regex str Regexp.new "^#{Regexp.escape(str.gsub('*', "\0")).gsub("\0", ".*")}$" end |
#lookup(path) ⇒ Object
78 79 80 |
# File 'lib/rvc/fs.rb', line 78 def lookup path lookup_loc(path).map(&:obj) end |
#lookup_loc(path) ⇒ Object
82 83 84 85 86 |
# File 'lib/rvc/fs.rb', line 82 def lookup_loc path els, absolute, trailing_slash = Path.parse path base_loc = absolute ? Location.new(@root) : @loc traverse(base_loc, els) end |
#mark(key, loc) ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/rvc/fs.rb', line 135 def mark key, loc if loc == nil @marks.delete key else @marks[key] = loc end end |
#traverse(base_loc, els) ⇒ Object
Starting from base_loc, traverse each path element in els. Since the path may contain wildcards, this function returns a list of matches.
126 127 128 129 130 131 132 133 |
# File 'lib/rvc/fs.rb', line 126 def traverse base_loc, els locs = [base_loc.dup] els.each_with_index do |el,i| locs.map! { |loc| traverse_one loc, el, i==0 } locs.flatten! end locs end |
#traverse_one(loc, el, first) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rvc/fs.rb', line 88 def traverse_one loc, el, first case el when '.' [loc] when '..' loc.pop unless loc.obj == @root [loc] when '...' loc.push(el, loc.obj.parent) unless loc.obj == @root [loc] when MARK_PATTERN return unless first loc = @marks[$1] or return [] [loc.dup] when REGEX_PATTERN regex = Regexp.new($') loc.obj.children. select { |k,v| k =~ regex }. map { |k,v| loc.dup.tap { |x| x.push(k, v) } } when GLOB_PATTERN regex = glob_to_regex el loc.obj.children. select { |k,v| k =~ regex }. map { |k,v| loc.dup.tap { |x| x.push(k, v) } } else # XXX check for ambiguous child if first and el =~ /^\d+$/ and @marks.member? el loc = @marks[el].dup else x = loc.obj.traverse_one(el) or return [] loc.push el, x end [loc] end end |