Skip to content

Commit 8f7aec5

Browse files
authored
feat: export run/0 for param-less commands (#12)
* feat: export run/0 for param-less commands * ci: update workflows to main ref * docs: update CHANGELOG
1 parent d6083d2 commit 8f7aec5

File tree

6 files changed

+64
-5
lines changed

6 files changed

+64
-5
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: CI
22

33
on:
44
push:
5-
branches: ["master"]
5+
branches: ["main"]
66
pull_request:
7-
branches: ["master"]
7+
branches: ["main"]
88

99
jobs:
1010
prettier:

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## Unreleased
9+
10+
### Added
11+
12+
- New `run/0` function for commands that don't define any parameters.
13+
814
## [0.4.1] - 2020-06-26
915

1016
### Fixed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
[![CI](https://github.com/codedge-llc/commandex/actions/workflows/ci.yml/badge.svg)](https://github.com/codedge-llc/commandex/actions/workflows/ci.yml)
66
[![Version](https://img.shields.io/hexpm/v/commandex.svg)](https://hex.pm/packages/commandex)
77
[![Total Downloads](https://img.shields.io/hexpm/dt/commandex.svg)](https://hex.pm/packages/commandex)
8-
[![License](https://img.shields.io/hexpm/l/commandex.svg)](https://github.com/codedge-llc/commandex/blob/master/LICENSE)
9-
[![Last Updated](https://img.shields.io/github/last-commit/codedge-llc/commandex.svg)](https://github.com/codedge-llc/commandex/commits/master)
8+
[![License](https://img.shields.io/hexpm/l/commandex.svg)](https://github.com/codedge-llc/commandex/blob/main/LICENSE)
9+
[![Last Updated](https://img.shields.io/github/last-commit/codedge-llc/commandex.svg)](https://github.com/codedge-llc/commandex/commits/main)
1010
[![Documentation](https://img.shields.io/badge/documentation-gray)](https://hexdocs.pm/commandex/)
1111

1212
Commandex structs are a loose implementation of the command pattern, making it easy
@@ -146,4 +146,4 @@ Git commit subjects use the [Karma style](http://karma-runner.github.io/5.0/dev/
146146

147147
Copyright (c) 2020-2024 Codedge LLC (https://www.codedge.io/)
148148

149-
This library is MIT licensed. See the [LICENSE](https://github.com/codedge-llc/commandex/blob/master/LICENSE) for details.
149+
This library is MIT licensed. See the [LICENSE](https://github.com/codedge-llc/commandex/blob/main/LICENSE) for details.

lib/commandex.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,17 @@ defmodule Commandex do
196196
Commandex.parse_params(%__MODULE__{}, opts)
197197
end
198198

199+
if Enum.empty?(params) do
200+
@doc """
201+
Runs given pipelines in order and returns command struct.
202+
"""
203+
@spec run :: t
204+
def run do
205+
new()
206+
|> run()
207+
end
208+
end
209+
199210
@doc """
200211
Runs given pipelines in order and returns command struct.
201212

test/commandex_test.exs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ defmodule CommandexTest do
5858
end
5959
end
6060

61+
describe "run/0" do
62+
test "is defined if no params are defined" do
63+
assert Kernel.function_exported?(Commandex.GenerateReport, :run, 0)
64+
65+
command = Commandex.GenerateReport.run()
66+
assert command.success
67+
assert command.data.total_valid > 0
68+
assert command.data.total_invalid > 0
69+
end
70+
71+
test "is not defined if params are defined" do
72+
refute Kernel.function_exported?(Commandex.RegisterUser, :run, 0)
73+
end
74+
end
75+
6176
defp assert_params(command) do
6277
assert command.params.email == @email
6378
assert command.params.password == @password

test/support/generate_report.ex

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
defmodule Commandex.GenerateReport do
2+
@moduledoc """
3+
Example command that generates fake data.
4+
5+
Used for testing parameter-less commands.
6+
"""
7+
8+
import Commandex
9+
10+
command do
11+
data :total_valid
12+
data :total_invalid
13+
14+
pipeline :calculate_valid
15+
pipeline :calculate_invalid
16+
end
17+
18+
def calculate_valid(command, _params, _data) do
19+
command
20+
|> put_data(:total_valid, :rand.uniform(1_000_000))
21+
end
22+
23+
def calculate_invalid(command, _params, _data) do
24+
command
25+
|> put_data(:total_invalid, :rand.uniform(1_000_000))
26+
end
27+
end

0 commit comments

Comments
 (0)