Class: Hiptask::List
- Inherits:
-
Object
- Object
- Hiptask::List
- Defined in:
- lib/hiptask/list.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Instance Method Summary collapse
- #add(content) ⇒ Object
- #delete(id) ⇒ Object
- #do(id) ⇒ Object
- #find_by_id(id) ⇒ Object
-
#initialize(filename) ⇒ List
constructor
A new instance of List.
- #undo(id) ⇒ Object
- #update(id, content) ⇒ Object
Constructor Details
#initialize(filename) ⇒ List
Returns a new instance of List.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/hiptask/list.rb', line 7 def initialize(filename) @filename = filename # Create file if it doesn't exist File.open(filename, "w+") unless File.exists?(filename) # Load Content from File @items = [] @file = File.open(filename, "r") @file.each do |line| @items.push line.strip end @file.close end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
5 6 7 |
# File 'lib/hiptask/list.rb', line 5 def items @items end |
Instance Method Details
#add(content) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/hiptask/list.rb', line 39 def add(content) File.open(@filename, "a") { |file| file.puts content } @items.push content end |
#delete(id) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/hiptask/list.rb', line 75 def delete(id) id = id.to_i - 1 raise "ID must be greater than 0" if id < 0 raise "Task not found" unless @items[id] @items.delete_at id save end |
#do(id) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/hiptask/list.rb', line 47 def do(id) id = id.to_i - 1 raise "ID must be greater than 0" if id < 0 raise "Task not found" unless @items[id] @items[id] = ">" + @items[id] unless @items[id].start_with? ">" save end |
#find_by_id(id) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/hiptask/list.rb', line 25 def find_by_id(id) id = id.to_i - 1 raise "ID must be greater than 0" if id < 0 task = nil @items.each_with_index { |line, index| if index == id task = line break end } return task end |
#undo(id) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/hiptask/list.rb', line 56 def undo(id) id = id.to_i - 1 raise "ID must be greater than 0" if id < 0 raise "Task not found" unless @items[id] @items[id] = @items[id][1, @items[id].length - 1] if @items[id].start_with? ">" save end |
#update(id, content) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/hiptask/list.rb', line 65 def update(id, content) id = id.to_i - 1 raise "ID must be greater than 0" if id < 0 raise "Task not found" unless @items[id] done = @items[id].start_with? ">" @items[id] = (done ? ">" : "") + content save end |