Building Modules

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.

NgBanks Test Running

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 banks
  • getBank(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
2
block getBanks()
return this.banks

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
2
3
4
5
6
7
8
9
10
11
12
13
block getBank(param)
param_type = :slug
if (isNumber(param)) {
param_type = :code
}

for bank in this.banks {
if (bank[param_type] == param){
return bank
}
}
return null
end

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
2
3
4
5
6
7
8
9
block addBank(name, code, slug, ussd_code)
for bank in this.banks {
if(bank[:code] == code || bank[:slug] == slug) {
throw("Bank Already Exist With This Code or Slug")
}
}
bank = [:name = toUpperCase(name), :code = code, :slug = slug, :ussd_code = ussd_code]
addToList(this.banks, bank)
return banks

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private banks = [
[:name = 'ACCESS BANK PLC', :code = 044, :slug = 'ACC', :ussd_code = '*901#'],
[:name = 'CITIBANK NIGERIA PLC', :code = 023, :slug = 'CBN', :ussd_code = null],
[:name = 'DIAMOND BANK PLC', :code = 063, :slug = 'DMB', :ussd_code = '*426#'],
[:name = 'ECOBANK NIGERIA PLC', :code = 064, :slug = 'EBN', :ussd_code = '*326#'],
[:name = 'FIDELITY BANK PLC', :code = 070, :slug = 'FDB', :ussd_code = '*770#'],
[:name = 'FIRST BANK NIGERIA LIMITED', :code = 011, :slug = 'FBN', :ussd_code = '*894#'],
[:name = 'FIRST CITY MONUMENT BANK PLC', :code = 214, :slug = 'FCB', :ussd_code = '*329#'],
[:name = 'GUARANTY TRUST BANK PLC', :code = 058, :slug = 'GTB', :ussd_code = '*737#'],
[:name = 'HERITAGE BANK PLC', :code = 030, :slug = 'HTB', :ussd_code = '*322#'],
[:name = 'KEY STONE BANK', :code = 082, :slug = 'KSB', :ussd_code = '*533#'],
[:name = 'MAINSTREET BANK', :code = 014, :slug = 'MSB', :ussd_code = null],
[:name = 'POLARIS BANK LIMITED', :code = 076, :slug = 'PLB', :ussd_code = null],
[:name = 'PROVIDUS BANK LIMITED', :code = 101, :slug = 'PVB', :ussd_code = null],
[:name = 'STANBIC IBTC BANK LTD', :code = 221, :slug = 'SIB', :ussd_code = '*909#'],
[:name = 'STANDARD CHARTERED BANK NIGERIA LTD', :code = 068, :slug = 'SCB', :ussd_code = null],
[:name = 'STERLING BANK PLC', :code = 232, :slug = 'STB', :ussd_code = '*822#'],
[:name = 'SUNTRUST BANK NIGERIA PLC', :code = 100, :slug = 'SBN', :ussd_code = null],
[:name = 'UNION BANK OF NIGERIA PLC', :code = 032, :slug = 'UBN', :ussd_code = '*826#'],
[:name = 'UNITED BANK FOR AFRICA PLC', :code = 033, :slug = 'UBA', :ussd_code = '*919#'],
[:name = 'UNITY BANK PLC', :code = 215, :slug = 'UNB', :ussd_code = '*7799#'],
[:name = 'WEMA BANK PLC', :code = 035, :slug = 'WEM', :ussd_code = '*945#'],
[:name = 'ZENITH BANK PLC', :code = 057, :slug = 'ZIB', :ussd_code = '*966#']
]

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
2
3
4
5
6

block main()
bank = new NgBanks

// Get All Banks
@ bank.getBanks()

In The Next Post

In the next post, I’ll discuss how to install the module and also write the tests.