Genesis.Value (genesis v0.10.0)

Copy Markdown View Source

Helper functions to handle property values for Components.

Summary

Functions

Defines a property for a Component.

Functions

prop(name, type, opts \\ [])

(macro)

Defines a property for a Component.

Types

The following types are currently supported:

  • :any
  • :atom
  • :string
  • :boolean
  • :float
  • :integer

Because we don't provide an exhaustive list of types, :any can be used as an wildcard where the other types don't seem appropriate. However, it should be used sparingly as it doesn't provide any typing or casting guarantees. Keep in mind that whenever possible, using scalar types should still be preferred as it enforces good ECS design practices.

For instance, you might be tempted to use :any to model complex relationships between entities, such as one-to-many associations, by storing complex data structures in a single property. A more idiomatic approach would be to create separate entities and link them through components instead. This adheres to ECS principles and enhances the maintainability, scalability, and extensibility of your architecture.

Examples

prop :name, :string, required: true
prop :amount, :integer, default: 0
prop :status, :atom, values: [:active, :inactive]
prop :email, :string, format: ~r/@/
prop :bonus, :float, min: 0.0, max: 95.5
prop :alive, :boolean, default: true
prop :created_at, Date, required: true

Options

  • :required - marks the property as required.
  • :default - the default value if one is not provided.
  • :values - restricts accepted values to the given list.
  • :format - regex used to validate string properties.
  • :min - the minimum number allowed (or minimum string length).
  • :max - the maximum number allowed (or maximum string length).