Skip to content

Commit

Permalink
Merge pull request #145 from peek-travel/feature/access_behaviour
Browse files Browse the repository at this point in the history
Implement access behaviour
  • Loading branch information
vanvoljg authored Feb 10, 2022
2 parents eda20e6 + 8a8c6a1 commit 68897a2
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 51 deletions.
63 changes: 35 additions & 28 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on: push

env:
MIX_ENV: test
OTP_VERSION_SPEC: "24.2.1"
ELIXIR_VERSION_SPEC: "1.13.3"

jobs:
compile:
Expand All @@ -13,22 +15,23 @@ jobs:

steps:
- uses: actions/checkout@v2
- uses: actions/[email protected]
- uses: erlef/setup-beam@v1
id: setup
with:
otp-version: 22.x
elixir-version: 1.10.x
- uses: actions/cache@v1
otp-version: ${{ env.OTP_VERSION_SPEC }}
elixir-version: ${{ env.ELIXIR_VERSION_SPEC }}
- uses: actions/cache@v2
id: cache-mix
with:
path: deps
key: ${{ runner.os }}-mix-v5-${{ hashFiles('mix.lock') }}
restore-keys: |
${{ runner.os }}-mix-v5-
- uses: actions/cache@v1
- uses: actions/cache@v2
id: cache-build
with:
path: _build
key: ${{ runner.os }}-build-v3-${{ hashFiles('lib/**/*.ex') }}${{ hashFiles('test/**/*.ex') }}${{ hashFiles('mix.lock') }}
key: ${{ runner.os }}-build-v4-${{ hashFiles('lib/**/*.ex') }}${{ hashFiles('test/**/*.ex') }}${{ hashFiles('mix.lock') }}
restore-keys: |
${{ runner.os }}-build-v3-
- name: Install dependencies
Expand All @@ -47,11 +50,12 @@ jobs:

steps:
- uses: actions/checkout@v2
- uses: actions/[email protected]
- uses: erlef/setup-beam@v1
id: setup
with:
otp-version: 22.x
elixir-version: 1.10.x
- uses: actions/cache@v1
otp-version: ${{ env.OTP_VERSION_SPEC }}
elixir-version: ${{ env.ELIXIR_VERSION_SPEC }}
- uses: actions/cache@v2
with:
path: deps
key: ${{ runner.os }}-mix-v5-${{ hashFiles('mix.lock') }}
Expand All @@ -78,23 +82,24 @@ jobs:

steps:
- uses: actions/checkout@v2
- uses: actions/[email protected]
- uses: erlef/setup-beam@v1
id: setup
with:
otp-version: 22.x
elixir-version: 1.10.x
- uses: actions/cache@v1
otp-version: ${{ env.OTP_VERSION_SPEC }}
elixir-version: ${{ env.ELIXIR_VERSION_SPEC }}
- uses: actions/cache@v2
with:
path: deps
key: ${{ runner.os }}-mix-v5-${{ hashFiles('mix.lock') }}
- uses: actions/cache@v1
- uses: actions/cache@v2
with:
path: _build
key: ${{ runner.os }}-build-v3-${{ hashFiles('lib/**/*.ex') }}${{ hashFiles('test/**/*.ex') }}${{ hashFiles('mix.lock') }}
key: ${{ runner.os }}-build-v4-${{ hashFiles('lib/**/*.ex') }}${{ hashFiles('test/**/*.ex') }}${{ hashFiles('mix.lock') }}
- name: Run tests
run: mix coveralls.json
env:
DATABASE_URL: ecto://postgres:postgres@localhost/ecto_diff_test
- uses: codecov/codecov-action@v1
- uses: codecov/codecov-action@v2
with:
token: ${{secrets.CODECOV_TOKEN}}
fail_ci_if_error: true
Expand All @@ -108,18 +113,19 @@ jobs:

steps:
- uses: actions/checkout@v2
- uses: actions/[email protected]
- uses: erlef/setup-beam@v1
id: setup
with:
otp-version: 22.x
elixir-version: 1.10.x
- uses: actions/cache@v1
otp-version: ${{ env.OTP_VERSION_SPEC }}
elixir-version: ${{ env.ELIXIR_VERSION_SPEC }}
- uses: actions/cache@v2
with:
path: deps
key: ${{ runner.os }}-mix-v5-${{ hashFiles('mix.lock') }}
- uses: actions/cache@v1
- uses: actions/cache@v2
with:
path: _build
key: ${{ runner.os }}-build-v3-${{ hashFiles('lib/**/*.ex') }}${{ hashFiles('test/**/*.ex') }}${{ hashFiles('mix.lock') }}
key: ${{ runner.os }}-build-v4-${{ hashFiles('lib/**/*.ex') }}${{ hashFiles('test/**/*.ex') }}${{ hashFiles('mix.lock') }}
- name: Run credo
run: mix credo --strict

Expand All @@ -133,18 +139,19 @@ jobs:

steps:
- uses: actions/checkout@v2
- uses: actions/[email protected]
- uses: erlef/setup-beam@v1
id: setup
with:
otp-version: 22.x
elixir-version: 1.10.x
- uses: actions/cache@v1
otp-version: ${{ env.OTP_VERSION_SPEC }}
elixir-version: ${{ env.ELIXIR_VERSION_SPEC }}
- uses: actions/cache@v2
id: cache-build-dev
with:
path: _build
key: ${{ runner.os }}-build-dev-v4-${{ hashFiles('lib/**/*.ex') }}${{ hashFiles('test/**/*.ex') }}${{ hashFiles('mix.lock') }}
restore-keys: |
${{ runner.os }}-build-dev-v4-
- uses: actions/cache@v1
- uses: actions/cache@v2
with:
path: plts
key: ${{ runner.os }}-plts-v4-${{ hashFiles('lib/**/*.ex') }}${{ hashFiles('test/**/*.ex') }}${{ hashFiles('mix.lock') }}
Expand Down
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
elixir 1.9.1-otp-22
erlang 22.0.7
elixir 1.13.3-otp-24
erlang 24.2.1
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Nothing yet

## [0.3.0][] - 2022-02-09

### Added

- Implement Access behaviour for EctoDiff structs

## [0.2.2][] - 2019-06-23

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ To demonstrate the basic use-case for EctoDiff, let's look at a simple example.
like the following `Pet` with many `Skill`s. Importantly, we've chosen to `cast_assoc` the skills in the pet's changeset
function, and we've opted to use `on_replace: :delete` on the has_many skills association.

EctoDiff structs implement the Access behaviour for working with deeply-nested data.

```elixir
defmodule Pet do
use Ecto.Schema
Expand Down
11 changes: 11 additions & 0 deletions lib/ecto_diff.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule EctoDiff do
For details on what the generated struct looks like, see: `t:EctoDiff.t/0`.
"""

@behaviour Access

alias Ecto.Association.NotLoaded

@typedoc """
Expand Down Expand Up @@ -341,6 +343,15 @@ defmodule EctoDiff do
defp no_changes?(_), do: false

defp primary_key_nil?(key), do: Enum.all?(key, fn {_key, value} -> is_nil(value) end)

@impl Access
defdelegate fetch(diff, key), to: Map

@impl Access
defdelegate get_and_update(diff, key, update_fn), to: Map

@impl Access
defdelegate pop(diff, key), to: Map
end

defimpl Inspect, for: EctoDiff do
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule EctoDiff.MixProject do
use Mix.Project

@version "0.2.2"
@version "0.3.0"
@source_url "https://github.com/peek-travel/ecto_diff"

def project do
Expand Down Expand Up @@ -67,7 +67,7 @@ defmodule EctoDiff.MixProject do
defp package do
[
files: ["lib", ".formatter.exs", "mix.exs", "README.md", "LICENSE.md", "CHANGELOG.md"],
maintainers: ["Chris Dosé <chris.dose@gmail.com>"],
maintainers: ["Peek Travel <noreply@peek.com>"],
licenses: ["MIT"],
links: %{
"GitHub" => @source_url,
Expand Down
Loading

0 comments on commit 68897a2

Please sign in to comment.