Flags (ex_esdb v0.0.2-alfa)

This module is used to manipulate bitwise flags.

Inspired by: Flags in C#

Event souurced systems often rely on flags to indicate the state of the aggregate at any given time. In this module, we define a set of functions that can be used to manipulate these flags.

Summary

Functions

Returns the bitwise OR of two flags. In other words, it sets the bit that corresopnds to the flag GIVEN: original_state is 0b00100100 (integer: 36) WHEN the flag to be set is 0b01000000 (integer: 64) THEN the result is 0b01100100 (integer: 100)

Returns the bitwise OR of multiple flags against a given state. In other words, it sets the bits that corresopnds to the flags GIVEN: original_state is 0b00100100 (integer: 36) WHEN the flags to be set are [0b01000000, 0b10000000] (integers: 64, 128) THEN the result is 0b11100100 (integer: 228)

Returns the bitwise AND of two flags. In other words, it unsets the bit that corresopnds to the flag GIVEN: original_state is 0b01100100 (integer: 100) WHEN the flag to be unset is 0b01000000 (integer: 64) THEN the result is 0b00100100 (integer: 36)

Functions

decompose(target)

has?(target, flag)

has_all?(status, flags)

has_any?(status, flags)

has_not?(target, flag)

highest(n, flag_map)

lowest(n, flag_map)

set(target, flag)

Returns the bitwise OR of two flags. In other words, it sets the bit that corresopnds to the flag GIVEN: original_state is 0b00100100 (integer: 36) WHEN the flag to be set is 0b01000000 (integer: 64) THEN the result is 0b01100100 (integer: 100)

Example: iex> Flags.set(36, 64) 100

set_all(target, flags)

Returns the bitwise OR of multiple flags against a given state. In other words, it sets the bits that corresopnds to the flags GIVEN: original_state is 0b00100100 (integer: 36) WHEN the flags to be set are [0b01000000, 0b10000000] (integers: 64, 128) THEN the result is 0b11100100 (integer: 228)

Example: iex> Flags.set_all(36, [64, 128]) 228

to_list(n, flag_map)

to_string(n, flag_map)

unset(target, flag)

Returns the bitwise AND of two flags. In other words, it unsets the bit that corresopnds to the flag GIVEN: original_state is 0b01100100 (integer: 100) WHEN the flag to be unset is 0b01000000 (integer: 64) THEN the result is 0b00100100 (integer: 36)

Example: iex> Flags.unset(100, 64) 36

unset_all(target, flags)