Skip to content

Commit

Permalink
Add changeset function, use real db in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daytonn committed May 5, 2020
1 parent 0857a4b commit c55d65e
Show file tree
Hide file tree
Showing 21 changed files with 1,429 additions and 607 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Security

[1.3.0]
------------

### Added
- `changeset` convenience function added

### Changed

### Removed

### Fixed

### Security

[1.2.0] - 2019-09-17
--------------------
### Added
Expand Down
22 changes: 19 additions & 3 deletions lib/ecto_resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ defmodule EctoResource do

:change ->
@doc """
Creates a #{schema_name} changeset.
Creates a #{schema_name} changeset from an existing schema struct.
#{name}(%#{schema_name}{}, %{})
Expand All @@ -132,8 +132,23 @@ defmodule EctoResource do
ResourceFunctions.change(unquote(schema), changeable, changes)
end

@spec changeset() :: Ecto.Changeset.t()
def changeset() do
:changeset ->

@doc """
Creates a blank changeset.
changeset()
#Ecto.Changeset<
action: nil,
changes: %{},
errors: [],
data: ##{schema_name}<>,
valid?: true
>
"""
@spec unquote(name)() :: Ecto.Changeset.t()
def unquote(name)() do
ResourceFunctions.changeset(unquote(schema))
end

Expand Down Expand Up @@ -164,6 +179,7 @@ defmodule EctoResource do
#{name}(%{invalid: "invalid"})
** (Ecto.InvalidChangesetError)
"""
@spec unquote(name)(map()) :: Ecto.Schema.t() | Ecto.InvalidChangesetError
def unquote(name)(attributes) do
ResourceFunctions.create!(@repo, unquote(schema), attributes)
end
Expand Down
68 changes: 47 additions & 21 deletions lib/option_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ defmodule EctoResource.OptionParser do
alias EctoResource.Helpers

@functions [
{ "update!", 2 },
{ "update", 2 },
{ "get_by!", 2 },
{ "get_by", 2 },
{ "get!", 2 },
{ "get", 2 },
{ "delete!", 1 },
{ "delete", 1 },
{ "create!", 1 },
{ "create", 1 },
{ "change", 1 },
{ "all", 1 }
{"all", 1},
{"change", 1},
{"changeset", 0},
{"create!", 1},
{"create", 1},
{"delete!", 1},
{"delete", 1},
{"get!", 2},
{"get", 2},
{"get_by!", 2},
{"get_by", 2},
{"update!", 2},
{"update", 2}
]

@spec parse(String.t(), list() | atom()) :: map()
Expand All @@ -32,8 +33,23 @@ defmodule EctoResource.OptionParser do

def parse(suffix, :read), do: parse(suffix, only: [:all, :get, :get!, :get_by, :get_by!])

def parse(suffix, :read_write),
do: parse(suffix, only: [:all, :get, :get!, :get_by, :get_by!, :change, :create, :create!, :update, :update!])
def parse(suffix, :read_write) do
parse(suffix,
only: [
:all,
:get,
:get!,
:get_by,
:get_by!,
:change,
:changeset,
:create,
:create!,
:update,
:update!
]
)
end

def parse(suffix, options) do
@functions
Expand All @@ -47,7 +63,7 @@ defmodule EctoResource.OptionParser do
end

@spec create_suffix(module, list()) :: String.t()
def create_suffix(_, [suffix: false]), do: ""
def create_suffix(_, suffix: false), do: ""
def create_suffix(schema, _), do: Helpers.underscore_module_name(schema)

defp function_name(function, ""), do: String.to_atom(function)
Expand All @@ -61,16 +77,20 @@ defmodule EctoResource.OptionParser do
String.to_atom("get_#{suffix}_by!")
end

defp function_name("changeset", suffix) do
String.to_atom("#{suffix}_changeset")
end

defp function_name(function, suffix) do
case function =~ ~r/!/ do
true -> String.replace_suffix(function, "!", "") <> "_" <> suffix <> "!"
false -> function <> "_" <> suffix
end
|> String.to_atom
|> String.to_atom()
end

defp function_description(function, arity, "") do
function <> "/" <> Integer.to_string(arity)
function <> "/" <> Integer.to_string(arity)
end

defp function_description("all", arity, suffix) do
Expand All @@ -79,30 +99,36 @@ defmodule EctoResource.OptionParser do

defp function_description("get_by", arity, suffix) do
arity = Integer.to_string(arity)
"get_" <> suffix <> "_by" <> "/" <> arity
"get_" <> suffix <> "_by" <> "/" <> arity
end

defp function_description("get_by!", arity, suffix) do
arity = Integer.to_string(arity)
arity = Integer.to_string(arity)
"get_" <> suffix <> "_by!" <> "/" <> arity
end

defp function_description("changeset", arity, suffix) do
arity = Integer.to_string(arity)
suffix <> "_changeset" <> "/" <> arity
end

defp function_description(function, arity, suffix) do
arity = Integer.to_string(arity)

case function =~ ~r/!/ do
true -> String.replace_suffix(function, "!", "") <> "_" <> suffix <> "!" <> "/" <> arity
false -> function <> "_" <> suffix <> "/" <> arity
end
end

defp filter_functions(functions, [except: filters]) do
defp filter_functions(functions, except: filters) do
Enum.reject(functions, fn {function, _} ->
function = String.to_atom(function)
Enum.member?(filters, function)
end)
end

defp filter_functions(functions, [only: filters]) do
defp filter_functions(functions, only: filters) do
Enum.filter(functions, fn {function, _} ->
function = String.to_atom(function)
Enum.member?(filters, function)
Expand Down
2 changes: 1 addition & 1 deletion lib/resource_functions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule EctoResource.ResourceFunctions do
schema.changeset(struct(schema), %{})
end

@spec create!(Ecto.Repo.t(), module, map()) ::
@spec create(Ecto.Repo.t(), module, map()) ::
{:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
def create(repo, schema, attributes) do
schema
Expand Down
6 changes: 3 additions & 3 deletions priv/test_repo/migrations/20200410023114_create_people.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ defmodule EctoResource.TestRepo.Migrations.CreatePeople do

def change do
create table(:people) do
add(:first_name, :string)
add(:last_name, :string)
add(:age, :integer)
add(:first_name, :string, null: false)
add(:last_name, :string, null: false)
add(:age, :integer, null: false)
end
end
end
Loading

0 comments on commit c55d65e

Please sign in to comment.