View Source Omnipresence protocol (Omnipresence v1.0.0)

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

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

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

Summary

Types

t()

All the types that implement this protocol.

Types

@type t() :: term()

All the types that implement this protocol.

Functions

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

Check if a value is present