Localize.Ecto.Type.IntegerRange (Localize SQL v1.0.0)

Copy Markdown View Source

An Ecto.Type storing an Elixir integer Range.t/0 in a PostgreSQL int8range column.

A range such as 1..10 stores and loads as itself, so a bounded span of integers is a first-class value in a schema rather than a pair of columns. int8range is a built-in PostgreSQL type, so no migration beyond the column is needed, and the column can be queried with the range operators (@>, &&) and indexed with a GiST index.

Schema

# in a migration
add :levels, :int8range

# in a schema
field :levels, Localize.Ecto.Type.IntegerRange

Semantics

Elixir ranges are inclusive of both ends, so 1..10 covers 1 through 10; it is stored as the PostgreSQL range [1,10] and read back as 1..10 regardless of the bound form PostgreSQL canonicalizes to (it stores int8range as [lower, upper)).

Only ascending unit-step ranges are storable — a range carrying a step (1..10//2) or descending (an empty range like 10..1//1) has no int8range equivalent and is rejected on cast and dump.

Summary

Functions

Callback implementation for Ecto.Type.embed_as/1.

Callback implementation for Ecto.Type.equal?/2.

Functions

embed_as(_)

Callback implementation for Ecto.Type.embed_as/1.

equal?(term1, term2)

Callback implementation for Ecto.Type.equal?/2.