ijverig

ijverig

Say I want to make a mix task named URLShorten.

Thus: defmodule Mix.Tasks.URLShorten do

However, I saw that it is then named url_shorten when displaying mix help.

But mix url_shorten doesn’t work, because it looks for the task in a module named Mix.Tasks.UrlShorten, instead of Mix.Tasks.URLShorten.

How can I deal with these inflections?

Showing Posts 1 to 4

peerreynders

peerreynders

I think you are dealing with a reversable transformation issue:

PascalCase ↔ snake_case

How would we know to transform url_shorten to URLShorten? The only way to resolve it, is to stick with:

UrlShortenurl_shorten


Macro.underscore/1:

In general, underscore can be thought of as the reverse of camelize, however, in some cases formatting may be lost

ijverig

ijverig OP

Yes, but I think I saw somewhere there’s a way to “hardcode” it. To tell elixir/mix system that url_shorten should be inflected to URLShorten.

I can’t find it, though.

Using UrlShorten is one solution, for sure. But the naming convention itself tell us to stick to acronyms like URL…

peerreynders

peerreynders

The functions that are doing the “work”:

Mix.Utils.module_name_to_command/2
Mix.Utils.command_to_module_name/1

Naming Conventions

For aliases, capital letters are kept in acronyms, like ExUnit.CaptureIO or Mix.SCM.

However:

elixir/lib/mix/test/mix/task_test.exs at main · elixir-lang/elixir · GitHub

  message =
      "The task \"acronym.http\" could not be found because the module is named " <>
        "Mix.Tasks.Acronym.HTTP instead of Mix.Tasks.Acronym.Http as expected. " <>
        "Please rename it and try again"

    assert_raise Mix.NoTaskError, message, fn ->
      Mix.Task.run("acronym.http")
    end

So it would seem that mix task modules are an exception to the convention (i.e. conventions tend to be broken if there is a good reason - in this case to keep the logic of transforming mix task module names to command names and vice versa as simple as possible).

Zmy846

Zmy846

Wow, I am terribly late to the party, however I think I have your solution…

In your mix project configuration, you can configure a custom namespace for your app:

config :url_shorten,
  namespace: URLShorten

Like @peerreynders mentioned, you’re doing something contrary to the defaults, so mix tooling doesn’t know how to generate the “correct” PascalCase form. Explicitly configuring the :namespace config option seems to fix all of mix’s broken assumptions.

With that being said, it’s a good idea to stick to the defaults for this reason. :grin:

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews