If you are new to Ruby or even if you aren’t, You are probably seeding data to your database to make sure your program is working correctly. Creating 10, 20, 30 or more records in your database can be repetitive and take up more time than you planned to spend. That is where Faker comes in. Faker is a Ruby Gem that was created by Benjamin Curtis all the way back in 2009. The purpose of Faker is to generate real looking data into you database so you can accurately test the functionality of your application. Although Faker has many subjects to choose from, sometimes you might wish they had a specific TV Show, Movie, etc…to use for your testing.
I will start by saying that Faker does have a Contributing guide that explains how to submit a pull request if you add a class to the gem. However, I have not yet went through this process, so I will just explain how you can add a class to your own forked Faker gem and use it in your own programs.
For this example, let’s create a new class under TV Shows. Being a child of the 90’s I was a huge fan of Save By The Bell…I mean, who wasn’t? So let’s create a generator in Faker for the that TV Show. The first thing we should do is create a yml file (YAML).
We will create the yml file inside the en (english) directory. To get there, we go into lib/locales/en. Once we are there we can click on the new file icon and name the file using the name of the tv show. In my example I named my file save_by_the_bell.yml. The yml file will store all our data that we will use in our class file. So start out with the basic format below. I created 3 fields (characters, quotes, and locations) but you can add as many field as you want.
side not — Each file in this directory corresponds to the Faker class of the same name. That is,
save_by_the_bell.yml` in this directory contains the data for the methods in `Faker::TvShows::SaveByTheBell`.
Now that we have our yml file finished, we can move on to our class file. To create our class file, we need to go to the faker directory inside the lib folder. Here, we can select where our class file will go. Since I am creating a TV Show class, I will create a new class file inside the tv_shows folder. In my newly created save_by_the_bell.rb file, we can create the template for our class file.
First line we write in the file will be module Faker. The next line we write will be the class of whatever our Ruby file will be inside. Since I am in the tv_shows directory, I will write class TvShows. We now will have our class on the following line inherit from the Base class, which will look like class SaveByTheBell < Base. Below that line we will write flexible :save_by_the_bell (flexible :name_of_file). Finally, we will push this into class.
Now the only thing left is to create the methods for our class so we can retrieve the data from the yml file. Since we have characters, quotes, and locations we will define methods with those names, but remember to make the method name singular (character).
For our first method, we can write a method called character and inside the method we’ll write fetch(‘save_by_the_bell.characters’). We will do this same thing for the following 2 methods except change the .characters to .quotes, and .locations.
Your class should look like the below image when you finish.
Congratulations! You have completed the process of creating a new class in Faker. Now to use it, just commit it in your terminal and push it up to your github account. Now whenever you want to seed your data, you can grab your Faker repo link from your github and place the link in the Gemfile of your app.