Skip to content

Commit

Permalink
Removed get_field_as_decimal helper, added credits for from_struct he…
Browse files Browse the repository at this point in the history
…lper and removed :skip option.
  • Loading branch information
dkln committed May 1, 2024
1 parent 4bfcc53 commit cdb0848
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 35 deletions.
38 changes: 4 additions & 34 deletions lib/wuunder_utils/maps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ defmodule WuunderUtils.Maps do
@moduledoc """
Contains a set of helpers to deal with some complex stuff with Maps and Structs
"""
alias WuunderUtils.Numbers
alias WuunderUtils.Presence

@type map_key() :: atom() | binary()
Expand Down Expand Up @@ -60,34 +59,6 @@ defmodule WuunderUtils.Maps do
end
end

@doc """
Lazy helper that grabs the field of a map and tries to convert it to a decimal in one pass
## Examples
iex> WuunderUtils.Maps.get_field_as_decimal(%{value: "50"}, :value)
Decimal.new("50")
iex> WuunderUtils.Maps.get_field_as_decimal(%{value: Decimal.new("1337.5")}, :value)
Decimal.new("1337.5")
iex> WuunderUtils.Maps.get_field_as_decimal(%{value: 50}, :value)
Decimal.new("50")
iex> WuunderUtils.Maps.get_field_as_decimal(%{value: 50}, :weight)
Decimal.new("0")
iex> WuunderUtils.Maps.get_field_as_decimal(%{value: nil}, :value)
Decimal.new("0")
"""
@spec get_field_as_decimal(map(), map_key()) :: Decimal.t()
def get_field_as_decimal(map, key) when is_map(map) and is_valid_map_key(key) do
map
|> get_field(key)
|> Numbers.to_decimal()
end

@doc """
Acts as an IndifferentMap. Put a key/value regardless of the key type. If the map
contains keys as atoms, the value will be stored as atom: value. If the map contains
Expand Down Expand Up @@ -229,10 +200,12 @@ defmodule WuunderUtils.Maps do
This function deep structs, maps, lists etc. to a map
and uses a set of default transformers as defined in `default_struct_fransform/0`.
It's also able to convert Ecto models to flat maps. It uses the defined Ecto fields for that.
There is also an option to omit the `transform` option to add an extra set of transformers.
Took some inspiration from this great lib: https://github.com/prodis/miss-elixir/blob/0.1.5/lib/miss/map.ex
Note: It's also able to convert Ecto models to flat maps. It uses the defined Ecto fields for that.
## Examples
iex> WuunderUtils.Maps.from_struct(%TestStruct{
Expand Down Expand Up @@ -316,9 +289,6 @@ defmodule WuunderUtils.Maps do

fun when is_function(fun, 1) ->
fun.(struct)

:skip ->
struct
end
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule WuunderUtils.MixProject do
def project do
[
app: :wuunder_utils,
version: "0.1.0",
version: "0.1.1",
elixir: "~> 1.14",
organization: "wuunder",
name: "Wuunder Utils",
Expand Down

0 comments on commit cdb0848

Please sign in to comment.