Refactor modules into contexts (#78)
* [WIP] break out a few contexts, start refactoring fast index modules * [WIP] more contexts, this time around slow indexing and downloads * [WIP] got all tests passing * [WIP] Added moduledocs * Built a genserver to rename old jobs on boot * Added a module naming check; moved things around * Fixed specs
This commit is contained in:
1 parent
7fa5c4da63
commit
33baa99aae
66 files changed
+1092
-568
No files matched your search
@@ -0,0 +1,42 @@
|
||||
defmodule Pinchflat.Downloading.OutputPath.Parser do
|
||||
@moduledoc """
|
||||
Parses liquid-ish-style strings into a rendered string
|
||||
|
||||
Used for turning filepath templates into real filepaths
|
||||
"""
|
||||
|
||||
use Pinchflat.Downloading.OutputPath.Base
|
||||
|
||||
@doc """
|
||||
Parses a string into a rendered string, using the provided variables. Optionally
|
||||
takes a custom fetcher function for handling missing variables.
|
||||
|
||||
Variable identifiers are surrounded by {{ and }}. The variable keys MUST be strings.
|
||||
If an identifier is not found in the provided variables, it will be removed from the string.
|
||||
|
||||
Returns `{:ok, binary()}` or `{:error, binary()}`.
|
||||
"""
|
||||
def parse(string, variables, value_fetch_fn \\ &default_fetcher/2) do
|
||||
# `do_parse` comes from `RenderedString.Base`
|
||||
case do_parse(string) do
|
||||
{:ok, parsed, _, _, _, _} ->
|
||||
{:ok, build_string(parsed, variables, value_fetch_fn)}
|
||||
|
||||
{:error, message, _, _, _, _} ->
|
||||
{:error, message}
|
||||
end
|
||||
end
|
||||
|
||||
defp build_string(parsed, variables, value_fetch_fn) do
|
||||
Enum.reduce(parsed, "", fn element, acc ->
|
||||
case element do
|
||||
{:text, text} -> acc <> text
|
||||
{:interpolation, {:identifier, identifier}} -> acc <> value_fetch_fn.(identifier, variables)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
def default_fetcher(identifier, variables) do
|
||||
Map.get(variables, identifier, "")
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user