Class: AtticCleanup::Path::Custom

Inherits:
Object
  • Object
show all
Defined in:
lib/attic-cleanup/path/custom.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fileObject

Returns the value of attribute file.



4
5
6
# File 'lib/attic-cleanup/path/custom.rb', line 4

def file
  @file
end

#line_nrObject

Returns the value of attribute line_nr.



4
5
6
# File 'lib/attic-cleanup/path/custom.rb', line 4

def line_nr
  @line_nr
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/attic-cleanup/path/custom.rb', line 4

def name
  @name
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/attic-cleanup/path/custom.rb', line 4

def path
  @path
end

Class Method Details

.all(file) ⇒ Object

Selects every line from the selected file but ignores lines where the first character is ‘#’



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/attic-cleanup/path/custom.rb', line 20

def self.all(file)
  File.open(file, 'r') do |r|
    line_array = []
    while line = r.gets do
      if line[0..0] == "#"
      else
        line_array << line
      end
    end
    line_array
  end
end

.check_ignore(file) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/attic-cleanup/path/custom.rb', line 79

def self.check_ignore(file)
  is_file = false
  lines = []
  File.open(MyAttic::IGNORE, 'r') do |r|
    while line = r.gets do
      if line == file || line == file+"\n"
        is_file = true
      end
    end
  end
  is_file
end

.defaultObject

Gets the last line in the default_path.txt file, which is the path to the default route



8
9
10
11
12
13
14
15
16
# File 'lib/attic-cleanup/path/custom.rb', line 8

def self.default
  File.open(MyAttic::DEFAULT, 'r') do |r|
    line_array = []
    while line = r.gets do
      line_array << line
    end
    line_array.last
  end
end

.set_default(value) ⇒ Object



105
106
107
108
109
# File 'lib/attic-cleanup/path/custom.rb', line 105

def self.set_default(value)
  File.open(MyAttic::DEFAULT, 'w') do |w| 
    w.write("#Write your default location here.\n#{value}")
  end
end

.set_ignore(value) ⇒ Object



99
100
101
102
103
# File 'lib/attic-cleanup/path/custom.rb', line 99

def self.set_ignore(value)
  File.open(MyAttic::IGNORE, 'a') do |w|
    w.write("\n"+value)
  end
end

Instance Method Details

#find_customObject

Gets the custom shortcuts and looks in the custom_paths.txt file if there is a match. If there is it will select that line and extract the shortcut, so only the path remains



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/attic-cleanup/path/custom.rb', line 54

def find_custom
  File.open(@file, 'r') do |r|
    k = []
    k[0] = nil
    while line = r.gets do
      name_count = @name.count("A-z, \s")
      k << line[0..name_count]
      this_name = k.last
      
      if this_name == @name+" "
        selected_line = line
        the_name = this_name
      end
    end
    if selected_line == nil
      puts "Shortcut not found"
      exit 1
    else
      the_name_count = the_name.count("A-z, \s")
      line_count = selected_line.count("A-z, \s, '/'")
      selected_line[the_name_count..line_count-1]
    end
  end
end

#find_lineObject

Gets a specific line from a file



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/attic-cleanup/path/custom.rb', line 34

def find_line
  File.open(@file, 'r') do |r|
    k = []
    k[0] = nil
    while line = r.gets do
      k << line
    end
    if k[@line_nr] == nil
     "This line does not exist."
    elsif k[@line_nr] == "\n"
      "Empty"
    else
      k[@line_nr]
    end
  end
end

#writeObject

Write to the custom_paths.txt file



93
94
95
96
97
# File 'lib/attic-cleanup/path/custom.rb', line 93

def write
  File.open(@file, 'a') do |w| 
    w.write("\n"+ @name + " " + @path)
  end
end