Class: Yoke::Alias

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

Class Method Summary collapse

Class Method Details

.add(path, name) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/yoke/alias.rb', line 17

def add(path, name)
  Yoke::Base.create_alias_file
  if list.has_key?(name)
    false
  else
    content = []
    added = false
    File.readlines(Yoke::Base.alias_file_path).each do |line|
      if line.start_with? "alias"
        alias_extract = line.scan(/^alias\s(\w+)\=\"cd '(.+)'\"$/).last
        if alias_extract[1] == path
          content << alias_string(name, path)
          added = true
        else
          content << line
        end
      else
        content << line
      end
    end
    File.open(Yoke::Base.alias_file_path, "w") do |file|
      content.each { |line| file.puts line }
    end
    unless added
      File.open(Yoke::Base.alias_file_path, 'a') do |file|
        file.puts alias_string(name, path)
      end
    end
    true
  end
end

.alias_string(name, path) ⇒ Object



72
73
74
# File 'lib/yoke/alias.rb', line 72

def alias_string(name, path)
  "alias #{name.split.join('_').downcase}=\"cd '#{path}'\""
end

.listObject



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/yoke/alias.rb', line 4

def list
  return {} unless Yoke::Base.has_alias_file?

  aliases = {}
  File.readlines(Yoke::Base.alias_file_path).each do |line|
    if line.start_with? "alias"
      alias_extract = line.scan(/^alias\s(\w+)\=\"cd '(.+)'\"$/).last
      aliases[alias_extract[0]] = alias_extract[1]
    end
  end
  aliases
end

.remove(path, name) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/yoke/alias.rb', line 49

def remove(path, name)
  return false unless Yoke::Base.has_alias_file?

  if list.has_key?(name)
    content = []
    File.readlines(Yoke::Base.alias_file_path).each do |line|
      if line.start_with? "alias"
        alias_extract = line.scan(/^alias\s(\w+)\=\"cd '(.+)'\"$/).last
        if alias_extract[0] != name
          content << line
        end
      else
        content << line
      end
    end
    File.open(Yoke::Base.alias_file_path, "w") do |file|
      content.each { |line| file.puts line }
    end
  else
    false
  end
end