Class: Toga::Togafile

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

Constant Summary collapse

HEADING_REGEX =
/^[A-Z\ ]{3,}+/

Class Method Summary collapse

Class Method Details

.append_to_group(group_name, string) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/toga/togafile.rb', line 30

def append_to_group(group_name, string)
  strip!
  
  lines = self.to_a
  range = group_range(group_name)
  last_index = range.last
  
  # Insert string at the end of the group
  last_index += 1 if lines[last_index].strip != ""
  lines.insert(last_index, string)
  
  # Write array back to file
  overwrite(lines)
end

.file_handle(mode = 'r') ⇒ Object



18
19
20
21
22
23
24
# File 'lib/toga/togafile.rb', line 18

def file_handle(mode='r')
  if block_given?
    File.open(self.path, mode) { yield }
  else
    File.open(self.path, mode)
  end
end

.group_at(offset) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/toga/togafile.rb', line 141

def group_at(offset)
  group = ""
  file_handle.each_with_index do |line, i|
    if line.match(HEADING_REGEX)
      group = line[0../\W/ =~ line].strip
    end
    
    if i >= offset
      return group
    end
  end
  
  group
end

.group_range(group_name) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/toga/togafile.rb', line 114

def group_range(group_name)
  group_name = group_name.to_s

  lines = []
  in_group = false      
  i = 0
  range_start = 0
  range_end = i

  file_handle.each do |line|
    is_heading = line.match(HEADING_REGEX)
    first_line = line.starts_with?(group_name, case_sensitive: false)
    is_other_heading = !first_line
    in_group = first_line || in_group && !is_heading
    
    range_start = i if first_line
    
    if in_group
      range_end = i
    end
  
    i += 1
  end
  
  Range.new(range_start, range_end)
end

.lines_in_group(group_name) ⇒ Object



26
27
28
# File 'lib/toga/togafile.rb', line 26

def lines_in_group(group_name)
  self.to_a[group_range(group_name)]
end

.move(prefix, move_hash, options) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/toga/togafile.rb', line 79

def move(prefix, move_hash, options)
  from  = move_hash.keys.first
  to    = move_hash.values.first
  if !lines_in_group(from).includes_prefix?(prefix)
    return false # Didn't contain this, can't move it
  end
  
  full = self.remove_from_group(from, prefix)
  if options[:prepend]
    self.prepend_to_group(to, full)
  else
    self.append_to_group(to, full)
  end          
end

.overwrite(lines) ⇒ Object



187
188
189
190
191
# File 'lib/toga/togafile.rb', line 187

def overwrite(lines)
  handle = file_handle('w')
  lines.each {|l| handle.puts(l) }
  handle.close
end

.pathObject



8
9
10
# File 'lib/toga/togafile.rb', line 8

def self.path
  @@path ||= File.expand_path(File.join(Dir.getwd, Toga::TOGAFILE_NAME))
end

.path=(path) ⇒ Object



12
13
14
# File 'lib/toga/togafile.rb', line 12

def self.path=(path)
  @@path = path
end

.prepend_to_group(group_name, string) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/toga/togafile.rb', line 45

def prepend_to_group(group_name, string)
  strip!
  
  lines = self.to_a
  range = group_range(group_name)
  first_index = range.first + 1 # Add one to bypass title
  
  # Insert string at the end of the group
  lines.insert(first_index+1, string)
  
  # Write array back to file
  overwrite(lines)
end

.promote(prefix) ⇒ Object

Moves a task to the top of its group



104
105
106
107
108
# File 'lib/toga/togafile.rb', line 104

def promote(prefix)
  full, group_name, offset_in_group = search(prefix)
  self.remove_from_group(group_name, prefix)
  self.prepend_to_group(group_name, full)
end

.remove_from_group(group_name, string) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/toga/togafile.rb', line 59

def remove_from_group(group_name, string)
  offset = lines_in_group(group_name).includes_prefix?(string)
  if !offset
    puts "[Warning] Task '#{string}' doesn't exist in #{group_name}."
    return
  end
  range = group_range(group_name)
  index = range.first + offset
  
  # Insert string at the end of the group
  lines = self.to_a
  full = lines.delete_at(index)
  
  # Write array back to file
  overwrite(lines)
  
  # Return full task that was removed
  full
end

.search(prefix) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/toga/togafile.rb', line 94

def search(prefix)
  lines = self.to_a
  offset = lines.includes_prefix?(prefix)
  group_name = group_at(offset)
  full = lines[offset]
  offset_in_group = offset - group_range(group_name).first
  [full, group_name, offset_in_group]
end

.strip!Object

Cleans all the whitespace in the file.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/toga/togafile.rb', line 157

def strip!
  lines = self.to_a
  line_after_heading = false
  in_group = ""
  copy = []
  lines.each_with_index do |line, i|
    if line_after_heading == true
      # Insert a blank line before the start of a group
      copy << ""
      
      line_after_heading = false
    end
    
    unless line == ""
      # Copy the line
      copy << line
    end
    
    in_group = line.match(HEADING_REGEX)
    if in_group
      # Insert a blank line after the end of a group
      copy.insert copy.length-1, "" unless i == 0
      line_after_heading = true
    end
    
  end
  
  overwrite(copy)
end

.to_aObject



110
111
112
# File 'lib/toga/togafile.rb', line 110

def to_a
  file_handle.collect { |l| l.strip }
end