View Source Omnipresence protocol (Omnipresence v0.1.0)

A protocol for presence checking. This is intended to be imported when necessary.

implementing

Implementing

If you have a struct that you wish to have custom presence check for, you can implement it rather quickly.

defmodule Dog do
  defstruct [:name, :age]
end

defimpl Omnipresence, for: Dog do
  def present?(%Dog{name: nil, age: nil}), do: false
  def present?(_), do: true

  def blank?(dog), do: !present?(dog)

  def presence(%Dog{name: nil, age: nil}), do: nil
  def presence(dog), do: dog
end

examples

Examples

import Omnipresence

if present?(calculated_value()) do
  IO.puts("I'm present")
else
  IO.puts("I'm blank")
end

if blank?(nil) do
  IO.puts("I'm blank")
end
raw_value
|> some_function()
|> Stream.map(&Omnipresence.presence/1)
|> Stream.reject(&is_nil/1)
|> Enum.each(fn value ->
  IO.inspect(value)
end)
assert presence(" ") == nil

Link to this section Summary

Link to this section Types

Link to this section Functions

@spec blank?(term()) :: boolean()
@spec presence(term()) :: term() | nil
@spec present?(term()) :: boolean()

Check if a value is present