-
-
Save nibanez80/b0877b94078c5ec6827a to your computer and use it in GitHub Desktop.
Ruby Todos 1.0: Core Features
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Move with Lil to the black mountain hills of Dakota | |
| Lose Lil to Danny | |
| Get hit in the eye by Danny | |
| Walk into town seeking revenge | |
| Book room at local saloon | |
| Check into room and read Gideon's bible | |
| Drink too much gin | |
| Overhear Lil and Danny in neighboring room | |
| Burst into neighboring room and declare a showdown | |
| Get shot by Danny and collapse in corner | |
| Visit doctor | |
| Return to room and read Gideon's bible | |
| Sing along! "D'do d'do d'do do do d'do d'do d'do" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'csv' | |
| class List | |
| attr_accessor :file, :tasks | |
| def initialize(file) | |
| @file = file | |
| @tasks = [] | |
| end | |
| def list(x) | |
| CSV.foreach(@file) do |row| | |
| @tasks << Task.new(row) | |
| end | |
| x = 0 | |
| @tasks.map! do |row| | |
| r = row.instance_variable_get(:@entry).join | |
| x +=1 | |
| if r[0] == 'x' | |
| r = r.gsub(/^x/, '') | |
| "#{x}. [x] #{r}" | |
| else | |
| "#{x}. [ ] #{r}" | |
| end | |
| end | |
| @tasks | |
| end | |
| def add(entry) | |
| File.open("todo.csv", 'ab') do |csv| | |
| entry = entry + "\n" | |
| csv << entry | |
| end | |
| list('') | |
| end | |
| def delete(entry) | |
| task_array = [] | |
| entry = entry.to_i - 1 | |
| CSV.foreach(@file) do |row| | |
| task_array << row | |
| end | |
| task_array = task_array.flatten | |
| task_array.delete_at(entry) | |
| save_to_csv(task_array) | |
| list('') | |
| end | |
| def save_to_csv(task_array) | |
| File.open("todo.csv", 'wb') do |csv| | |
| task_array.each do |y| | |
| csv << y + "\n" | |
| end | |
| end | |
| end | |
| def complete(entry) | |
| task_array = [] | |
| entry = entry.to_i - 1 | |
| CSV.foreach(@file) do |row| | |
| task_array << row | |
| end | |
| task_array = task_array.flatten | |
| t = task_array[entry] | |
| if t[0] == 'x' | |
| puts "already has x" | |
| else | |
| task_array[entry] = "x" + task_array[entry] | |
| end | |
| save_to_csv(task_array) | |
| list('') | |
| end | |
| end | |
| class Task | |
| attr_accessor :entry | |
| def initialize(row) | |
| @entry = row | |
| end | |
| end | |
| ####### DRIVER CODE ########## | |
| list = List.new('todo.csv') | |
| command = ARGV[0] | |
| argument = ARGV[1] | |
| unless command | |
| puts "Please enter a command" | |
| else | |
| puts eval("list.#{command}('#{argument}')") | |
| end | |
| # run file with "list" | |
| # return contents of list numbered | |
| # run file with add | |
| # return "task added" | |
| # return contents of list numbered | |
| # run file with delete + task number | |
| # return "task deleted" | |
| # run file with complete + task number | |
| # return "task completed" | |
| # run file with "completed" | |
| # return contents of list with only completed | |
| # run file with "list" | |
| # return contents of remaining tasks |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment