From 5cfb8616d98a2d029d14be0026d941343045cfa3 Mon Sep 17 00:00:00 2001 From: Chris Laskey Date: Thu, 1 Apr 2021 16:44:05 -0400 Subject: [PATCH] Update artemis.gen.resource mix task --- apps/artemis/lib/mix/task_helpers/strings.ex | 20 +++++++++++++ .../lib/mix/tasks/artemis.gen.resource.ex | 29 +++++++++++++------ 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/apps/artemis/lib/mix/task_helpers/strings.ex b/apps/artemis/lib/mix/task_helpers/strings.ex index 7f606927..76199224 100644 --- a/apps/artemis/lib/mix/task_helpers/strings.ex +++ b/apps/artemis/lib/mix/task_helpers/strings.ex @@ -81,4 +81,24 @@ defmodule Mix.TaskHelpers.Strings do |> Enum.map(&String.capitalize/1) |> Enum.join("") end + + @doc """ + Returns true if value is a single word + """ + def single_word?(value) do + count = + value + |> snakecase() + |> String.split("_") + |> length() + + count == 1 + end + + @doc """ + Returns true if value contains multiple words + """ + def multi_word?(value) do + !single_word?(value) + end end diff --git a/apps/artemis/lib/mix/tasks/artemis.gen.resource.ex b/apps/artemis/lib/mix/tasks/artemis.gen.resource.ex index 48de2ca1..4b4690a3 100644 --- a/apps/artemis/lib/mix/tasks/artemis.gen.resource.ex +++ b/apps/artemis/lib/mix/tasks/artemis.gen.resource.ex @@ -195,15 +195,26 @@ defmodule Mix.Tasks.Artemis.Gen.Resource do source_single_dashcase = dashcase(source_single_snakecase_lower) target_single_dashcase = dashcase(target_single_snakecase_lower) - case_order = [ - :spacecase, - :modulecase, - :dashcase, - :snakecase_lower, - :snakecase_upper, - :spacecase_lower, - :spacecase_upper - ] + case_order = + case single_word?(source_single_spacecase) && multi_word?(target_single_spacecase) do + true -> + [ + :modulecase, + :snakecase_lower, + :snakecase_upper + ] + + false -> + [ + :spacecase, + :modulecase, + :dashcase, + :snakecase_lower, + :snakecase_upper, + :spacecase_lower, + :spacecase_upper + ] + end cases = %{ source: %{