Skip to content

Instantly share code, notes, and snippets.

@jgaskins
Created May 11, 2022
Embed
What would you like to do?
Project-local requires in Crystal
# Experiment with macros to allow for project-local requires
# https://forum.crystal-lang.org/t/project-relative-require/4617
{% begin %}
ROOT = "{{system("pwd").strip.id}}"
{% end %}
macro spec(file)
macro load_spec
\{% path = __DIR__.gsub(%r{\A{{ROOT.id}}}, "").gsub(%r{[^/]+}, "..").id %}
require "\{{path[1..]}}/spec/{{file.id}}"
end
load_spec
end
macro src(file)
macro load_src
\{% path = __DIR__.gsub(%r{\A{{ROOT.id}}}, "").gsub(%r{[^/]+}, "..").id %}
require "\{{path[1..]}}/src/{{file.id}}"
end
load_src
end
# Load spec_helper to ensure the macro is loaded
require "../spec_helper"
spec "factories/user"
src "queries/user_query"
describe UserQuery do
# ...
end
require "../src/config/require"
# Macro is loaded downstream from environment configuration
src "config/env"
src "queries/query"
src "models/user"
struct UserQuery < Query(User)
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment