Skip to content

Commit

Permalink
Refactor and extract functions in alchemist-project
Browse files Browse the repository at this point in the history
I extracted a bunch of functions from alchemist-project to
alchemist-utils and added tests for them.
  • Loading branch information
whatyouhide committed Jun 28, 2015
1 parent 26bb9b9 commit dd3e9be
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
19 changes: 2 additions & 17 deletions alchemist-project.el
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,6 @@ Point is left in a convenient location."
(goto-char (point-min))
(beginning-of-line 3))

(defun alchemist-project--underscore-to-camelcase (string)
"Convert an underscore_string to a CamelCase string."
(mapconcat 'capitalize (split-string string "_") ""))

(defun alchemist-project--path-to-module-name (path)
"Convert a `path' like my_lib/foo.ex to a module name like MyLib.Foo."
(let* ((path (split-string path "/"))
(path (mapcar (lambda (el) (replace-regexp-in-string "\.ex$" "" el)) path)))
(mapconcat 'alchemist-project--underscore-to-camelcase path ".")))

(defun alchemist-project--maybe-add-file-extension-to-path (path)
(if (string-match-p "\.ex$" path)
path
(concat abs-path ".ex")))

(defun alchemist-project-run-tests-for-current-file ()
"Run the tests related to the current file."
(interactive)
Expand All @@ -164,14 +149,14 @@ The newly created buffer is filled with a module definition based on the file na
(message "You're not in a Mix project")
(let* ((lib-path (concat root "lib/"))
(abs-path (read-file-name "New file in lib/: " lib-path))
(abs-path (alchemist-project--maybe-add-file-extension-to-path abs-path))
(abs-path (alchemist-utils--add-ext-to-path-if-not-present abs-path ".ex"))
(relative-path (file-relative-name abs-path lib-path)))
(if (file-readable-p abs-path)
(message "%s already exists" relative-path)
(make-directory (file-name-directory abs-path) t)
(find-file abs-path)
(insert (concat "defmodule "
(alchemist-project--path-to-module-name relative-path)
(alchemist-utils--path-to-module-name relative-path)
" do\n"
" \n"
"end\n"))
Expand Down
23 changes: 23 additions & 0 deletions alchemist-utils.el
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,29 @@ It walks the directory tree until it finds a elixir project root indicator."
(let* ((modules (mapconcat #'identity modules ",")))
(format "[%s]" modules)))

(defun alchemist-utils--snakecase-to-camelcase (str)
"Convert a snake_case string `STR' to a CamelCase string.
This function is useful for converting file names like my_module to Elixir
module names (MyModule)."
(mapconcat 'capitalize (split-string str "_") ""))

(defun alchemist-utils--add-ext-to-path-if-not-present (path ext)
"Add `EXT' to `PATH' if `PATH' doesn't already ends with `EXT'."
(if (string-suffix-p ext path)

This comment has been minimized.

Copy link
@whatyouhide

whatyouhide Jun 28, 2015

Author Contributor

string-suffix-p is builtin in >= 24.4, so we should be fine.

path
(concat path ext)))

(defun alchemist-utils--path-to-module-name (path)
"Convert `PATH' to its Elixir module name equivalent.
For example, convert 'my_app/my_module.ex' to 'MyApp.MyModule'."
(let* ((path (file-name-sans-extension path))
(path (split-string path "/"))
(path (remove-if (lambda (str) (equal str "")) path)))

This comment has been minimized.

Copy link
@whatyouhide

whatyouhide Jun 28, 2015

Author Contributor

cl should be included in >= 24.4 so remove-if should be always available.

(mapconcat #'alchemist-utils--snakecase-to-camelcase path ".")))


(provide 'alchemist-utils)

;;; alchemist-utils.el ends here
13 changes: 13 additions & 0 deletions test/alchemist-utils-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ a text")))
(list "Ek" "Behaviour" "Plug.Conn"))))
(should (equal "[]" (alchemist-utils--prepare-modules-for-elixir '("")))))

(ert-deftest test-utils/snakecase-to-camelcase ()
(should (equal "MyCustomModule" (alchemist-utils--snakecase-to-camelcase "my_custom_module")))
(should (equal "Foo" (alchemist-utils--snakecase-to-camelcase "foo"))))

(ert-deftest test-utils/add-ext-to-path-if-not-present ()
(should (equal "foo.ex" (alchemist-utils--add-ext-to-path-if-not-present "foo.ex" ".ex")))
(should (equal "foo.ex" (alchemist-utils--add-ext-to-path-if-not-present "foo" ".ex")))
(should (equal "foo.ex.exs" (alchemist-utils--add-ext-to-path-if-not-present "foo.ex" ".exs"))))

(ert-deftest test-utils/path-to-module-name ()
(should (equal "MyApp.MyModule" (alchemist-utils--path-to-module-name "my_app/my_module.ex")))
(should (equal "Foo.Bar" (alchemist-utils--path-to-module-name "/foo/bar")))
(should (equal "Foo" (alchemist-utils--path-to-module-name "//foo//"))))

(provide 'alchemist-utils-tests)

Expand Down

0 comments on commit dd3e9be

Please sign in to comment.