nhpip

nhpip

Dynamic modules / module "aliasing"

Hey,

I know there are other ways to do this, but I went down a deep rabbit hole last night trying to figure this one out :slight_smile: … this is purely a though experiment.

Say you have 2 almost identical dependencies, but you only want to use one of them at a time. Maybe the choice of dependency is geographic based?

Both dependencies have an example module hierarchy like:

 M0
  |
  M0.M1 - M0.M2 - M0.M3
            |
         M0.M2.M4

Now of course a simple solution to ensure the code in the rest of the application selects the correct dependency would be something like this in all the modules that use it:

  config = Application.compile_env(:my_app, SpecialDependency, [])
  @dependency Keyword.get(config, :special_dep)

  # And code would do:
  @dependency.foo(a, b, c)

Seems a bit hokey…what would be nice is to create a new (set) of stub modules that, for want of a better word “alias” the correct dependency. All the application code would call the stub which would get mapped onto the correct target dependency.

For example (yes, I can also use defdelegate)

defmodule SpecialDependencyStub do

  config = Application.compile_env(:my_app, SpecialDependency, [])
  @dependency Keyword.get(config, :special_dep)

 # Generate the same functions as the real dependency
  Enum.each(@dependency.__info__(:functions), fn({fun, arity}) ->
     args = Macro.generate_arguments(arity, __MODULE__)
     def unquote(fun)(unquote_splicing(args)), do: unquote(@dependency).unquote(fun)(unquote_splicing(args))
  end)
end

Now that works fine…where I am stumped is how to handle all the modules under the SpecialDependency. I want those modules to retain the same name as the real dependency, and I’d rather not have to repeat the above for every module. Something like

Enum.each(modules, fn(mod) -> WizzardsBeHere end)

Hopefully that all makes sense

First Post!

hst337

hst337

Why would you want to write Stub.foo(bar) instead of @stub.foo(bar)?

Last Post!

Hermanverschooten

Hermanverschooten

If these modules have the same interface, couldn’t you use a macro that would alias the correct module and you’d use that alias in your code?

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement