-
Notifications
You must be signed in to change notification settings - Fork 145
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
Refactor Some Code #205
Refactor Some Code #205
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a ton. Nice simple refactors that help clean things up 👍 Just one thing that would be great if you changed. Reverting the start_link
and reset
functions https://github.com/thoughtbot/ex_machina/pull/205/files#r105686541
lib/ex_machina.ex
Outdated
defp build_function_name(factory_name) do | ||
factory_name | ||
|> Atom.to_string | ||
<> "_factory" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you do Kernel.<>("_factory")
here instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's what we're doing. It's taking Atom.to_string(factory_name) <> "_factory"
. Can you expand more on what you mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the confusion. I meant to type |> Kernel.<>("_factory")
that way you can keep the pipe chain going
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, good catch. Running this code in my iex
and seeing that it doesn't actually work...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And fixed :p
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for jumping to the party and possibly silly question, but why not simply:
"#{factory_name}_factory"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pdawczak that's a very very good question :p
end | ||
|
||
defp do_merge(%{__struct__: _} = record, attrs), do: struct!(record, attrs) | ||
defp do_merge(record, attrs), do: Map.merge(record, attrs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
@@ -177,20 +180,16 @@ defmodule ExMachina do | |||
build_list(3, :user) | |||
""" | |||
def build_list(module, number_of_factories, factory_name, attrs \\ %{}) do | |||
Enum.map(1..number_of_factories, fn(_) -> | |||
Enum.map 1..number_of_factories, fn(_) -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that looks nicer 👍
@doc """ | ||
Raises a helpful error if no factory is defined. | ||
""" | ||
@doc "Raises a helpful error if no factory is defined." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch
@@ -230,7 +226,7 @@ defmodule ExMachina.Ecto do | |||
defp insert_built_belongs_to_assoc(module, association, record) do | |||
case Map.get(record, association.field) do | |||
built_relation = %{__meta__: %{state: :built}} -> | |||
relation = built_relation |> module.insert | |||
relation = module.insert(built_relation) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. That reads better
) | ||
Enum.reduce struct, Map.new, fn({key, value}, acc) -> | ||
Map.put(acc, to_string(key), value) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much better
lib/ex_machina/sequence.ex
Outdated
def reset do | ||
Agent.update(__MODULE__, fn(_) -> Map.new end) | ||
end | ||
def reset, do: Agent.update(__MODULE__, fn(_) -> Map.new end) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this and start_link
I think I prefer the previous version. This is a bit too busy IMO
1864647
to
81ffde1
Compare
@paulcsmith I've reverted and rebased the changes with |
81ffde1
to
62c0aa2
Compare
Thanks a ton @BenMorganIO! |
Hey guys,
I was going through the open source code and reasoning about the implementation. I noticed there was some code that could have been refactored. I hope this helps :)