Class: SkipDir

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

Instance Method Summary collapse

Constructor Details

#initialize(location) ⇒ SkipDir

Returns a new instance of SkipDir.



5
6
7
8
9
10
11
12
# File 'lib/skipdir.rb', line 5

def initialize(location)
  @location = location
  unless File.exist? @location
    File.open(@location, 'w') do end
  end

  @entries = read_map
end

Instance Method Details

#add(name, dir) ⇒ Object

Adds a mapping from the given name to the given directory

Returns:

  • a list of all current mappings as an array



16
17
18
19
20
# File 'lib/skipdir.rb', line 16

def add(name, dir)
  @entries[name.to_s] = dir.to_s
  save_map
  return @entries
end

#allObject

A list of all current mappings



36
37
38
# File 'lib/skipdir.rb', line 36

def all
  @entries
end

#get(name) ⇒ Object

The current mapping for the given alias or nil if there is no mapping



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/skipdir.rb', line 23

def get(name)
  entry = @entries[name]
  if entry
    return entry
  else
    @entries.each do |name_alias, dir|
      return dir if name_alias.start_with? name
    end
  end
  return nil
end

#remove(name) ⇒ Object

removes the entry with the given name.

Returns:

  • the removed dir or nil if no entry was found with that name



43
44
45
46
47
48
49
50
51
52
# File 'lib/skipdir.rb', line 43

def remove(name)
  if @entries.include? name
    dir = @entries[name]
    @entries.delete name
    save_map
    return dir
  else
    return nil
  end
end