Day 07 - Ready for Challenge! To-do List by Ruby (Part 1)

During my pre-course at Makers, I learnt many concepts, tricks and tips about Ruby from the materials, but I think I need to practise instead of absorbing without action. Although Makers doesn't request me to start a project like this, I still want to set up a challenge for myself and hopefully, it could be one of the projects in my portfolio.

This series will be divided into several parts. Ideally, this app would be finished in 3 parts. In part 1, I would make a plan and set up all the essentials for the app.


What I'm going to do?

A to-do list is a good way to test my knowledge as I have to build the application without any given instruction. However, before diving into code, it's always a good practice when developers make their plans in advance. Let's make one!

Functionalities

A 'to-do list' in my plan should include the following functionalities:

  • Display a list of all to-dos

  • Create a to-do with storing user input

  • Edit the content of selected to-do

  • Delete a selected to-do

  • Store and read data from a CSV file

What tech will I use in this app?

Concepts & Principles

Here are the concepts and principles I am going to apply to this app:

Test-driven Development (TDD)

I will apply the concept of TDD to this app. TDD taught me to consider functionalities which are user requirements in this case and then set up tests before I start to code.

Difficulties I might encounter

  • Not familiar with methods in RSpec: I will check the documentation more often to understand how to use them and what I can use.

  • Not sure what scale I should set up for the test: Should I set up tests for every line of my codes or just a few for more high-level functionalities? I am not sure for now and will figure it out later.

Continuous Integration (CI)

CI is a new concept and tool for me. Adding this part to my workflow might be the biggest challenge in this app. However, I do believe once I learn how to use it, it will be a great tool throughout my journey as a developer. That's why I insist to do it even though the course didn't force me to. The sooner I am familiar with CI, the better.

Difficulties I might encounter

  • Take a long time to set up Travis CI: I know nothing about Travis CI. The first step should be to config a file named: travis.yml but it seems a bit complicated. It might take me some time to finish this part.

Steps

Step 1 - Initialise Project

First, I need to create a folder to store all files. To do this, I run mkdir to-do-list-ruby and cd to it.

Inside the folder, I run rspec --init to initialise the project. This command will automatically create the files for RSpec to run their tests. Here is the explanation on Relish.

Use the --init option on the command line to generate conventional files for
an RSpec project. It generates a .rspec and spec/spec_helper.rb with some
example settings to get you started.

After running these commands, here is the file structure I got:

At this moment, it's time to initial git by running git init. Before pushing the folder to Github, I have to create a new repository named to-do-list-ruby on GitHub.

I run touch README.md and simply put #To-do List by Ruby in it, then by git add and git commit -m , the folder is now on GitHub.

Step 2 - Set Up Tests

Before writing tests, I create 2 files that we will need by running the following commands:

  • touch todo.rb

  • touch spec/todo_spec.rb

Inside todo_spec.rb , the first thing we do is to link up the test file and Ruby code by using require or require_relative .

require_relative "../todo.rb"

I decided to create a class Todo with 4 methods. Each of them handles one functionality by following the Single Responsibility Principle (SOLID). Here are 4 context for each method.

describe Todo do
    context ".list" do
    end

    context ".add" do
    end

    context ".edit" do
    end

    context ".delete" do
    end
end

Here is the list of tests I should set up:

  • .list should list out all to-dos

  • For method .add :

    • should displays a message with ID when a new to-do is successfully created

    • should throw an error when no user input is found

  • For method .edit :

    • should update the selected todo and return a message after successfully updating

    • should throw an error when the user inputs a wrong to-do ID

    • should throw an error when no user input is found

  • For method .delete :

    • should delete a to-do and returns a message with the to-do ID

    • should throw an error when the user inputs a wrong to-do ID

    • should throw an error when no user input is found

For now, I put a simple expect().to be true for each test to make sure I successfully import the todo.rb and call the methods.

context ".list" do
    it "lists out all to-dos" do
        expect(@Todo.list).to be true
    end
end

Step 3 - Create Classes

To simply pass all tests, I just create methods which only return true .

class Todo
    def list
        true
    end

    def add
        true
    end

    def edit
        true
    end

    def delete
        true
    end
end

Step 4 - Commit to Github

That's all for part 1, before calling it a day, let me commit all changes and push to GitHub. I will continue the rest in part 2. See you there!

Did you find this article valuable?

Support Terry Cheng by becoming a sponsor. Any amount is appreciated!