-
-
Save devdame/0d8ea3a97e8f1e94f95e to your computer and use it in GitHub Desktop.
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
| class ToDoList # => Our controller! the main thing | |
| def initialize | |
| end | |
| end | |
| class Input # => | |
| def initialize | |
| end | |
| end | |
| class Output # => gets info from everything else and outputs it to the screen | |
| def initialize | |
| end | |
| end | |
| class List # => Model? this stores the data! | |
| def initialize | |
| end | |
| end | |
| class Task | |
| end | |
| =begin | |
| start up via the controller - initialize (it already knows the file) | |
| controller creates the list (model--list, tasks--data layer) | |
| ui as a module on controller or as a class | |
| hard code in the file | |
| take in the argv and figure out what to do | |
| display that output via a view thing class | |
| =end | |
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
| =begin | |
| _____ _____ _____ _____ | |
| || l ||| e ||| t'||| s || | |
| ||__ |||__ |||__ ||| __|| | |
| |/___\|/___\|/___\|/___\| | |
| __ | |
| __/\ \__ | |
| __ __ __ _ __ /\_\ \ ,_\ __ | |
| /\ \/\ \/\ \/\`'__\/\ \ \ \/ /'__`\ | |
| \ \ \_/ \_/ \ \ \/ \ \ \ \ \_/\ __/ | |
| \ \___x___/'\ \_\ \ \_\ \__\ \____\ | |
| \/__//__/ \/_/ \/_/\/__/\/____/ | |
| .o8 .o. | |
| "888 888 | |
| oooo d8b oooo oooo 888oooo. oooo ooo 888 | |
| `888""8P `888 `888 d88' `88b `88. .8' Y8P | |
| 888 888 888 888 888 `88..8' `8' | |
| 888 888 888 888 888 `888' .o. | |
| d888b `V88V"V8P' `Y8bod8P' .8' Y8P | |
| .o..P' | |
| `Y8P' | |
| # What classes do you need? | |
| # Remember, there are four high-level responsibilities, each of which have multiple sub- | |
| #responsibilities: | |
| # 1. Gathering user input and taking the appropriate action (controller) | |
| # 2. Displaying information to the user (view) | |
| # 3. Reading and writing from the todo.txt file (model) | |
| # 4. Manipulating the in-memory objects that model a real-life TODO list (domain-specific | |
| #model) | |
| # Note that (4) is where the essence of your application lives. | |
| Initialize an empty TODO list list = List.new | |
| Add a task to a TODO list list.add(Task.new("walk the dog")) | |
| Get all the tasks on a TODO list tasks = list.tasks | |
| Delete a particular task from a TODO list ??? | |
| Complete a particular task on a TODO list ??? | |
| Parse the command-line arguments and take the appropriate action ??? | |
| Parse the todo.csv file and wrap each entry in easier-to-manipulate Ruby objects ??? | |
| There are other responsibilities. What are they? | |
| =end | |
| require 'csv' | |
| module TaskIO | |
| def add_tasks_from_csv(file) | |
| self.file = file | |
| task_info = parse_csv(file) | |
| task_info.each do |task| | |
| self.tasks << Task.new(task[0]) | |
| end | |
| end | |
| def parse_csv(task_info) | |
| CSV.read(task_info, :headers => false) | |
| end | |
| def write_task_to_csv(task_name) | |
| # DO THE THING | |
| end | |
| def delete_task_from_csv(task)#_name?) | |
| end | |
| end | |
| class ToDoList # generate and store tasks | |
| include TaskIO | |
| attr_reader :tasks | |
| attr_accessor :file | |
| def initialize(tasks = []) | |
| @tasks = tasks | |
| @completed_tasks = [] | |
| @file = '' | |
| end | |
| def add_task(task_name) | |
| self.tasks << Task.new(task_name) | |
| write_task_to_csv(task_name) | |
| end | |
| def delete_task(task_index) | |
| self.tasks.delete_at(task_index-1) | |
| delete_task_from_csv # MAYBE?????????????? | |
| end | |
| def to_s | |
| to_do_list_string = "" | |
| tasks.each_with_index do |task, index| | |
| to_do_list_string << "\n#{index+1}. #{task.task_name}" | |
| end | |
| to_do_list_string | |
| end | |
| def complete(task_index) | |
| task = self.tasks.delete_at(task_index) | |
| task.completed = true | |
| self.completed_tasks << task | |
| end | |
| end | |
| class Task | |
| attr_reader :task_name | |
| attr_accessor :completed | |
| def initialize(task_name) | |
| @task_name = task_name | |
| @completed = false | |
| end | |
| end | |
| class Interface | |
| def initialize(list) | |
| @list = list | |
| @command = ARGV[0] | |
| get_command | |
| end | |
| private | |
| def get_command | |
| case ARGV[0] | |
| when 'list' | |
| display_list | |
| when 'delete' | |
| delete | |
| when 'add' | |
| add | |
| when 'complete' | |
| complete | |
| else | |
| command_not_found | |
| end | |
| end | |
| def display_list | |
| puts list.to_s | |
| end | |
| def delete | |
| raise "You must enter a valid task number to delete." unless list.tasks[ARGV[1].to_i+1] | |
| list.delete_task(ARGV[1].to_i) | |
| display_list | |
| end | |
| def add | |
| list.add_task(ARGV[1]) | |
| display_list | |
| end | |
| def complete | |
| raise "You must enter a valid task number to complete." unless list.tasks[ARGV[1].to_i+1] | |
| list.complete_task(ARGV[1].to_i+1) | |
| end | |
| def command_not_found | |
| raise "That was not a valid request. Next time, say 'list' to view your to-do list, 'add '<task name>'' to add a task, or 'delete <task number>' to delete the task at <task number>." | |
| end | |
| attr_accessor :list | |
| attr_reader :command | |
| end | |
| list = ToDoList.new | |
| list.add_tasks_from_csv('todo.csv') | |
| p list.file | |
| # Interface.new(list) if ARGV.any? | |
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
| =begin | |
| _____ _____ _____ _____ | |
| || l ||| e ||| t'||| s || | |
| ||__ |||__ |||__ ||| __|| | |
| |/___\|/___\|/___\|/___\| | |
| __ | |
| __/\ \__ | |
| __ __ __ _ __ /\_\ \ ,_\ __ | |
| /\ \/\ \/\ \/\`'__\/\ \ \ \/ /'__`\ | |
| \ \ \_/ \_/ \ \ \/ \ \ \ \ \_/\ __/ | |
| \ \___x___/'\ \_\ \ \_\ \__\ \____\ | |
| \/__//__/ \/_/ \/_/\/__/\/____/ | |
| .o8 .o. | |
| "888 888 | |
| oooo d8b oooo oooo 888oooo. oooo ooo 888 | |
| `888""8P `888 `888 d88' `88b `88. .8' Y8P | |
| 888 888 888 888 888 `88..8' `8' | |
| 888 888 888 888 888 `888' .o. | |
| d888b `V88V"V8P' `Y8bod8P' .8' Y8P | |
| .o..P' | |
| `Y8P' | |
| # What classes do you need? | |
| # Remember, there are four high-level responsibilities, each of which have multiple sub- | |
| #responsibilities: | |
| # 1. Gathering user input and taking the appropriate action (controller) | |
| # 2. Displaying information to the user (view) | |
| # 3. Reading and writing from the todo.txt file (model) | |
| # 4. Manipulating the in-memory objects that model a real-life TODO list (domain-specific | |
| #model) | |
| # Note that (4) is where the essence of your application lives. | |
| Initialize an empty TODO list list = List.new | |
| Add a task to a TODO list list.add(Task.new("walk the dog")) | |
| Get all the tasks on a TODO list tasks = list.tasks | |
| Delete a particular task from a TODO list ??? | |
| Complete a particular task on a TODO list ??? | |
| Parse the command-line arguments and take the appropriate action ??? | |
| Parse the todo.csv file and wrap each entry in easier-to-manipulate Ruby objects ??? | |
| There are other responsibilities. What are they? | |
| =end | |
| require 'csv' | |
| class Controller | |
| def initialize | |
| end | |
| # takes in the argv and figures out what we need to do, then delegates the tasks at hand | |
| end | |
| class Viewer | |
| # given data by other classes, shares that data with the user. in charge of displaying. | |
| def display_list(list) | |
| puts list.to_s | |
| end | |
| end | |
| class Model | |
| # deals with the csv file. reads and writes it. controller will tell the model what it | |
| # needs to do. | |
| end | |
| class Manipulator | |
| # Actually manipulates the information we get from the model. will probably be given info | |
| # from the model, change it, then pass it back to the model to write. | |
| end | |
| class FileWriter | |
| def initialize | |
| end | |
| end | |
| class ToDoList # generate and store tasks | |
| attr_reader :tasks | |
| def initialize(tasks = []) | |
| @tasks = tasks | |
| end | |
| def add_tasks_from_csv(file) | |
| task_info = parse_csv(file) | |
| task_info.each do |task| | |
| self.tasks << Task.new(task[0]) | |
| end | |
| end | |
| def parse_csv(task_info) | |
| CSV.read(task_info, :headers => false) | |
| end | |
| def add_task(task_name) | |
| self.tasks << Task.new(task_name) | |
| write_task_to_csv(task_name) | |
| end | |
| def delete_task(task_index) | |
| self.tasks.delete_at(task_index-1) | |
| end | |
| def write_task_to_csv(task_name) | |
| # DO THE THING | |
| end | |
| def to_s | |
| to_do_list_string = "" | |
| tasks.each_with_index do |task, index| | |
| to_do_list_string << "\n#{index+1}. #{task.task_name}" | |
| end | |
| to_do_list_string | |
| end | |
| end | |
| class Task | |
| attr_reader :task_name, :completed, :id | |
| @@id_count = 0 | |
| def initialize(task_name) | |
| @task_name = task_name | |
| @completed = false | |
| @id = generate_id # or is this simply the task's index in the tasks array? | |
| end | |
| def generate_id | |
| @@id_count += 1 | |
| end | |
| end | |
| # user --gives command to--> controller | |
| # controller --delegates work to model?manipulator?--> that | |
| list = ToDoList.new | |
| list.add_tasks_from_csv('todo.csv') | |
| if ARGV.any? | |
| case ARGV[0] | |
| when 'list' | |
| puts list.to_s | |
| when 'delete' | |
| list.delete_task(ARGV[1].to_i) | |
| puts list.to_s | |
| when 'add' | |
| list.add_task(ARGV[1]) | |
| end | |
| end | |
| # list.tasks => ["one", "two", "three"] | |
| # task that is "two" | |
| # task.id = list.tasks. index of element that has value ("two") => 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment