Class: RVC::FS

Inherits:
Object
  • Object
show all
Defined in:
lib/rvc/fs.rb

Constant Summary collapse

MARK_PATTERN =
/^~(?:([\d\w]*|~|@))$/
REGEX_PATTERN =
/^%/
GLOB_PATTERN =
/\*/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ FS

Returns a new instance of FS.



59
60
61
62
63
# File 'lib/rvc/fs.rb', line 59

def initialize root
  @root = root
  @loc = Location.new root
  @marks = {}
end

Instance Attribute Details

#locObject (readonly)

Returns the value of attribute loc.



53
54
55
# File 'lib/rvc/fs.rb', line 53

def loc
  @loc
end

#marksObject (readonly)

Returns the value of attribute marks.



53
54
55
# File 'lib/rvc/fs.rb', line 53

def marks
  @marks
end

#rootObject (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

#curObject



65
66
67
# File 'lib/rvc/fs.rb', line 65

def cur
  @loc.obj
end

#display_pathObject



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