Compare commits

...

5 commits

Author SHA1 Message Date
James Harton 78d51a091c chore: release version v0.4.0
All checks were successful
continuous-integration/drone/push Build is passing
2023-09-19 22:44:44 +00:00
Barnabas Jovanovics cb3455bf47
feat: add template for constant values
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2023-09-20 10:37:53 +12:00
James Harton d6dad83013
docs: improve the smokestack moduledoc to elaborate about templates.
All checks were successful
continuous-integration/drone/push Build is passing
2023-09-09 20:35:53 +12:00
James Harton cf5565e2a4
chore: update readme re hex publication.
All checks were successful
continuous-integration/drone/push Build is passing
2023-09-09 19:49:24 +12:00
James Harton cbe223ec03
chore: fix hex release CI step.
All checks were successful
continuous-integration/drone/push Build is passing
2023-09-09 19:37:08 +12:00
8 changed files with 94 additions and 22 deletions

View file

@ -363,17 +363,17 @@ steps:
refs:
include:
- refs/tags/v*
depends_on:
- build artifacts
environment:
MIX_ENV: test
HEX_HOME: /drone/src/.hex
MIX_HOME: /drone/src/.mix
REBAR_BASE_DIR: /drone/src/.rebar3
ASDF_DATA_DIR: /drone/src/.asdf
ASDF_DIR: /root/.asdf
HEX_API_KEY:
from_secret: HEX_API_KEY
commands:
- . $ASDF_DIR/asdf.sh
- mix hex.publish --yes
depends_on:
- build artifacts
environment:
MIX_ENV: test
HEX_HOME: /drone/src/.hex
MIX_HOME: /drone/src/.mix
REBAR_BASE_DIR: /drone/src/.rebar3
ASDF_DATA_DIR: /drone/src/.asdf
ASDF_DIR: /root/.asdf
HEX_API_KEY:
from_secret: HEX_API_KEY
commands:
- . $ASDF_DIR/asdf.sh
- mix hex.publish --yes

View file

@ -5,6 +5,15 @@ See [Conventional Commits](Https://conventionalcommits.org) for commit guideline
<!-- changelog -->
## [v0.4.0](https://code.harton.nz/james/smokestack/compare/v0.3.1...v0.4.0) (2023-09-19)
### Features:
* add template for constant values
## [v0.3.1](https://code.harton.nz/james/smokestack/compare/v0.3.0...v0.3.1) (2023-09-09)

View file

@ -32,20 +32,18 @@ end
## Installation
Smokestack is not yet ready to be published to [Hex](https://hex.pm) so in the
mean time if you want to try it you need to add a git-based dependency:
Smokestack is available on [Hex](https://hex.pm/packages/smokestack) you can
add it directly to your `mix.exs`:
```elixir
def deps do
[
{:smokestack, git: "https://code.harton.nz/cinder/cinder", tag: "v0.1.0"}
{:smokestack, "~> 0.3"},
]
end
```
Since the package hasn't been published, there are no docs available on
[HexDocs](https://hexdocs.pm/), but you can access the latest version
[here](https://docs.harton.nz/james/smokestack).
Documentation for the latest release is available on [HexDocs](http://hexdocs.pm/smokestack).
## Github Mirror

View file

@ -26,6 +26,14 @@ defmodule Smokestack do
end
```
## Templates
Each attribute uses a template to generate a value when building a factory.
Templates can be anything that implements the `Smokestack.Template` protocol.
This protocol is automatically implemented for functions with arities zero
through two - meaning you can just drop in your own functions - or use one of
the built-in helpers from `Smokestack.Dsl.Template`.
## Variants
Sometimes you need to make slightly different factories to build a resource

View file

@ -17,6 +17,13 @@ defmodule Smokestack.Dsl.Template do
def choose(options, mapper \\ nil) when is_mapper(mapper),
do: %Template.Choose{options: options, mapper: mapper}
@doc """
Select a constant value
"""
@spec constant(element, mapper) :: Template.t()
def constant(value, mapper \\ nil) when is_mapper(mapper),
do: %Template.Constant{value: value, mapper: mapper}
@doc """
Cycle sequentially between a list of options.
"""

View file

@ -0,0 +1,16 @@
defmodule Smokestack.Template.Constant do
@moduledoc false
defstruct value: nil, mapper: nil
@type t :: %__MODULE__{value: any, mapper: Smokestack.Template.mapper()}
defimpl Smokestack.Template do
def init(constant), do: constant
def generate(constant, _, _) when is_function(constant.mapper, 1),
do: constant.mapper(constant.value)
def generate(constant, _, _),
do: constant.value
end
end

View file

@ -1,7 +1,7 @@
defmodule Smokestack.MixProject do
use Mix.Project
@version "0.3.1"
@version "0.4.0"
@moduledoc """
Test factories for Ash resources.
@ -21,7 +21,10 @@ defmodule Smokestack.MixProject do
source_url: "https://code.harton.nz/james/smokestack",
homepage_url: "https://code.harton.nz/james/smokestack",
aliases: aliases(),
dialyzer: [plt_add_apps: [:faker]]
dialyzer: [plt_add_apps: [:faker]],
docs: [
main: "Smokestack"
]
]
end

View file

@ -0,0 +1,31 @@
defmodule Smokestack.Template.ChooseTest do
@moduledoc false
use ExUnit.Case, async: true
alias Smokestack.{Template, Template.Constant}
describe "Smokestack.Template.init/1" do
test "it doesn't do anything" do
constant = %Constant{value: 1, mapper: &Function.identity/1}
assert ^constant = Template.init(constant)
end
end
describe "Smokestack.Template.generate/3" do
test "it returns the same value" do
value =
%Constant{value: 1}
|> Template.generate(nil, nil)
assert value == 1
end
test "it can map the chosen value" do
value =
%Constant{value: 1, mapper: &(&1 * 3)}
|> Template.generate(nil, nil)
assert value == 3
end
end
end