Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

construct functions to be defined as well as {thing}_factory #433

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

byronanderson
Copy link

order of attempts:

{thing}_factory/2
{thing}_factory/1
construct/2
construct/1

(if construct/2 is defined, it will always be used instead of construct/1)

@jfeeny was interested in introspecting metadata attached to struct modules in order to provide sane defaults without needing manually to register person_factory, for example, you could do something like this:

defmodule Factory do
  use ExMachina
  def construct(type) do
    struct = Module.camelize(type)
    fields = struct.describe() # __schema__ in ecto, for example
    fields
    |> Enum.map(fn {name, type} ->
      {name, default_for(type)}
    end)
    |> struct!(struct)
  end

  defp default_for(:string) do
    generate_fake_cat_name()
  end
end

order of attempts:

{thing}_factory/2
{thing}_factory/1
construct/2
construct/1

(if construct/2 is defined, it will always be used instead of construct/1)
@doomspork
Copy link
Member

Howdy @byronanderson! Now that ThoughtBot has passed this library on to us we're in the process of getting things updated 🎉 I was looking at your PR but I what I lack is the context for this change, can you point towards any issues or discussions around the need for these new functions?

Separately, if we are going to add new capabilities I would like us to include documentation along with the changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants