Skip to content

Learning Elixir through building a vending machine: clean architecture, OTP patterns, and comprehensive testing with ExUnit

Notifications You must be signed in to change notification settings

alenm/vending_machine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ji-seongkwang-lvu7gpzIT8k-unsplash

image source

Vending Machine

Most people understand the concept of vending machines. I think building one in Elixir can help illustrate how it can be done in a functional language. The project is not completed but I think I have enough to illustrate some concepts.

This project will be broken into two parts.

Overview

  • Example of functional concepts like immutable state.
  • Example of set_up and describe writing unit tests.
  • Example of how to write errors and the conventions.

Part 1 - The module

We will build this with one module and walk through how to:

  • Load up a machine
  • Insert funds.
  • Refund funds.
  • Interacting with sold out or not enough inventory available.

Part 2 - Holding state

We will then incorporate the module from Part 1 into a GenServer. This will be able to hold state and do some other interesting things for us.

The test directory

The test directory illustrates all the use cases and does a good overview of how setup and testing works. I think one of the nice things about Elixir is how easy it is to test your projects.

Overview

  Final Clean Architecture:
  VendingMachine              # Public API - clean interface
  ├── VendingMachine.Core     # Business logic - pure functions
  ├── VendingMachine.Server   # Process management - GenServer
  ├── VendingMachine.Inventory # Data structures
  └── VendingMachine.Validations # Validation rules
cd vending_machine
mix test

..........

Finished in 0.06 seconds (0.00s async, 0.06s sync)
10 tests, 0 failures

About

Learning Elixir through building a vending machine: clean architecture, OTP patterns, and comprehensive testing with ExUnit

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages