A module in simple-lang is the same as a package in other programming languages; they server particular functions and mostly comprises of classes and blocks.
On the 14th of November, a board member of simple-lang implemented a particular package in simple-lang. In this blogpost, he’ll be walking you on how he authored the module and by the end this is what we’ll be building - NgBanks.
Prerequisites
For this practical guide, you’ll be needing the following:
- Simple-lang installed in your machine
- Code editor.
If you don’t have simple-lang installed, head over to the official installation guide.
What You’ll Be Building
In this practical guide, you’ll be creating a module that returns Nigerian banks information upon request. You can perform only CR
(Create and Read) operations for now.
Setting Up The Project
First, you create a new directory.
1 | mkdir NgNews |
Next, navigate into the directory
1 | cd NgBanks |
Create the installation file, module directory src
and tests directory test
.
1 | touch install.sim & mkdir src test |
With all that covered, create the module file NgBanks.sim
in the src folder:
1 | touch src/NgBanks.sim |
Writing the Module
To begin, open the project directory in your code editor of choice. I’m using Visual Studio Code together with the simple-lang extension.
Open NgBanks.sim
and register the module name:
1 | module NgBanks |
Next, declare the NgBanks
class , it has no constructor:
1 | class NgBanks |
After that, define the various methods the module would have :
getBanks()
which returns all banksgetBank(param)
which returns a bank based on the search param.addBank
- Adds a new bank on the fly ( current session ).
You’ll start by defining the getBanks()
method first.
1 | block getBanks() |
The getBanks()
method returns all the bank in the banks
variable.
Next, define the getBank(param)
method. To read a bank’s detail from the master list banks
, an identifier needs to be passed so it is looped into the main list to retrieve the results. This method accepts only one parameter which is either the bank’s slug code or bank code.
1 | block getBank(param) |
Next, define the addBank(...)
method that allows you add a new bank on the fly.
The addBank(...)
method takes in four (4) arguments:
- name : Bank Name
- code : Bank Code
- slug : Bank Slug
- ussd_code : Bank’s USSD code
With all the argument passed, it adds the new bank using the List addToList()
block. However, an exception will be thrown if you are adding a duplicate bank.
1 | block addBank(name, code, slug, ussd_code) |
With all that covered, your module is set but something is missing. The banks
variable holding some default contents.
Master List
The NgBanks
is 90% completed and needs the grace of a default banks variable.
Create a private variable banks
holding the details of some banks :
1 | private banks = [ |
With that covered, the module is fully ready to be functional.
Testing
For the first test before diving deeper into installation which I’ll be covering in the next post. Add this to the top of the NgBanks
class :
1 |
|
In The Next Post
In the next post, I’ll discuss how to install the module and also write the tests.