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 ‘#’



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/attic-cleanup/path/custom.rb', line 31

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



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/attic-cleanup/path/custom.rb', line 90

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



116
117
118
119
120
# File 'lib/attic-cleanup/path/custom.rb', line 116

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



110
111
112
113
114
# File 'lib/attic-cleanup/path/custom.rb', line 110

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

Instance Method Details

#check_shortcut(input) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/attic-cleanup/path/custom.rb', line 18

def check_shortcut(input)
  if input[0..0] == "@"
    c = AtticCleanup::Path::Custom.new
    # The input without the first character (the @ sign)
    c.name = input[1..-1]
    c.file = MyAttic::CUSTOM
    input = c.find_custom
  end
  input
end

#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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/attic-cleanup/path/custom.rb', line 65

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 '@#{@name}'"
      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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/attic-cleanup/path/custom.rb', line 45

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



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

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