Skip to content

Commit

Permalink
feat: export run/0 for param-less commands (#12)
Browse files Browse the repository at this point in the history
* feat: export run/0 for param-less commands

* ci: update workflows to main ref

* docs: update CHANGELOG
  • Loading branch information
hpopp authored Sep 6, 2024
1 parent d6083d2 commit 8f7aec5
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: CI

on:
push:
branches: ["master"]
branches: ["main"]
pull_request:
branches: ["master"]
branches: ["main"]

jobs:
prettier:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Added

- New `run/0` function for commands that don't define any parameters.

## [0.4.1] - 2020-06-26

### Fixed
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
[![CI](https://github.com/codedge-llc/commandex/actions/workflows/ci.yml/badge.svg)](https://github.com/codedge-llc/commandex/actions/workflows/ci.yml)
[![Version](https://img.shields.io/hexpm/v/commandex.svg)](https://hex.pm/packages/commandex)
[![Total Downloads](https://img.shields.io/hexpm/dt/commandex.svg)](https://hex.pm/packages/commandex)
[![License](https://img.shields.io/hexpm/l/commandex.svg)](https://github.com/codedge-llc/commandex/blob/master/LICENSE)
[![Last Updated](https://img.shields.io/github/last-commit/codedge-llc/commandex.svg)](https://github.com/codedge-llc/commandex/commits/master)
[![License](https://img.shields.io/hexpm/l/commandex.svg)](https://github.com/codedge-llc/commandex/blob/main/LICENSE)
[![Last Updated](https://img.shields.io/github/last-commit/codedge-llc/commandex.svg)](https://github.com/codedge-llc/commandex/commits/main)
[![Documentation](https://img.shields.io/badge/documentation-gray)](https://hexdocs.pm/commandex/)

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

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

This library is MIT licensed. See the [LICENSE](https://github.com/codedge-llc/commandex/blob/master/LICENSE) for details.
This library is MIT licensed. See the [LICENSE](https://github.com/codedge-llc/commandex/blob/main/LICENSE) for details.
11 changes: 11 additions & 0 deletions lib/commandex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,17 @@ defmodule Commandex do
Commandex.parse_params(%__MODULE__{}, opts)
end

if Enum.empty?(params) do
@doc """
Runs given pipelines in order and returns command struct.
"""
@spec run :: t
def run do
new()
|> run()
end
end

@doc """
Runs given pipelines in order and returns command struct.
Expand Down
15 changes: 15 additions & 0 deletions test/commandex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ defmodule CommandexTest do
end
end

describe "run/0" do
test "is defined if no params are defined" do
assert Kernel.function_exported?(Commandex.GenerateReport, :run, 0)

command = Commandex.GenerateReport.run()
assert command.success
assert command.data.total_valid > 0
assert command.data.total_invalid > 0
end

test "is not defined if params are defined" do
refute Kernel.function_exported?(Commandex.RegisterUser, :run, 0)
end
end

defp assert_params(command) do
assert command.params.email == @email
assert command.params.password == @password
Expand Down
27 changes: 27 additions & 0 deletions test/support/generate_report.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Commandex.GenerateReport do
@moduledoc """
Example command that generates fake data.
Used for testing parameter-less commands.
"""

import Commandex

command do
data :total_valid
data :total_invalid

pipeline :calculate_valid
pipeline :calculate_invalid
end

def calculate_valid(command, _params, _data) do
command
|> put_data(:total_valid, :rand.uniform(1_000_000))
end

def calculate_invalid(command, _params, _data) do
command
|> put_data(:total_invalid, :rand.uniform(1_000_000))
end
end

0 comments on commit 8f7aec5

Please sign in to comment.