Current section

Files

Jump to
understated lib understated state_path.ex
Raw

lib/understated/state_path.ex

defmodule Understated.StatePath do
@doc ~S"""
Pushes a new state onto the current state path.
## Examples
iex> Understated.StatePath.push_state("", "new_state")
"new_state"
iex> Understated.StatePath.push_state("old_state", "new_state")
"old_state.new_state"
"""
def push_state(path, new_state) when path == "", do: new_state
def push_state(path, new_state), do: "#{path}.#{new_state}"
@doc ~S"""
Pops a state off the current state path.
## Examples
iex> Understated.StatePath.pop_state("", "to_pop")
""
iex> Understated.StatePath.pop_state("to_pop", "to_pop")
""
iex> Understated.StatePath.pop_state("not_to_pop.to_pop", "to_pop")
"not_to_pop"
iex> Understated.StatePath.pop_state("not_to_pop", "to_pop")
"not_to_pop"
"""
def pop_state(path, to_pop) when path == to_pop, do: ""
def pop_state(path, to_pop), do: String.replace_trailing(path, ".#{to_pop}", "")
end