{"id":13393259,"url":"https://github.com/KamilLelonek/exnumerator","last_synced_at":"2025-03-13T19:31:31.673Z","repository":{"id":57500750,"uuid":"43909284","full_name":"KamilLelonek/exnumerator","owner":"KamilLelonek","description":"Enumerable type in Elixir","archived":false,"fork":false,"pushed_at":"2020-02-27T19:45:56.000Z","size":33,"stargazers_count":65,"open_issues_count":0,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-03T07:03:23.431Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KamilLelonek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-08T18:41:45.000Z","updated_at":"2024-05-14T22:16:56.000Z","dependencies_parsed_at":"2022-08-30T21:01:38.907Z","dependency_job_id":null,"html_url":"https://github.com/KamilLelonek/exnumerator","commit_stats":null,"previous_names":["kamillelonek/exnumerable"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KamilLelonek%2Fexnumerator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KamilLelonek%2Fexnumerator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KamilLelonek%2Fexnumerator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KamilLelonek%2Fexnumerator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KamilLelonek","download_url":"https://codeload.github.com/KamilLelonek/exnumerator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243469154,"owners_count":20295697,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T17:00:47.902Z","updated_at":"2025-03-13T19:31:30.539Z","avatar_url":"https://github.com/KamilLelonek.png","language":"Elixir","funding_links":[],"categories":["ORM and Datamapping","Elixir"],"sub_categories":[],"readme":"# exnumerator\n\nEnum type in Elixir known from [Java](https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html) or [C#](https://msdn.microsoft.com/en-us/library/vstudio/cc138362).\n\n[![Build Status](https://travis-ci.org/KamilLelonek/exnumerator.svg)](https://travis-ci.org/KamilLelonek/exnumerator)\n\nEither in Java or in C# there is `enum` type available. It is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of `NORTH`, `SOUTH`, `EAST`, and `WEST`) and the days of the week. You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time—for example, the choices on a menu, command line flags, and so on.\n\nSee more:\n\n* \u003chttps://docs.oracle.com/javase/tutorial/java/javaOO/enum.html\u003e\n* \u003chttps://msdn.microsoft.com/en-us/library/vstudio/cc138362\u003e\n\n## Rationale\n\nImagine a question that can be either \"pending”, \"answered, or \"flagged”. Or a phone number that’s a \"home”, \"office”, \"mobile”, or \"fax” (if it’s 1982).\n\nSome models call for this kind of data. An attribute that can have only one of a few different values. And that set of values almost never changes. It’s a situation where, if it were plain Elixir, you’d just use an atom.\n\nWhen it comes to a database though, you could create a `PhoneNumberType` or `QuestionStatus` table and a `belongs_to` relationship to hold these values, but that doesn’t seem worth it. You can use [`Ecto.Type`](http://hexdocs.pm/ecto/Ecto.Type.html) behaviour for implementing custom types, but it expects 4 functions to be implemented, and it all becomes an overhead when you need to have many of custom enumerated types.\n\nHere `exnumerator` steps in to give you a handy way to define enumerable types that can be used together with your database. They are kept as `string` type under the hood so whatever database you use (if not postgres for some reason) you can get full benefits from this library.\n\n## Installation\n\nThe package can be installed as:\n\n```elixir\ndef deps do\n  [\n    {:exnumerator, \"~\u003e 1.6\"},\n    # ...\n  ]\nend\n```\n\n## Usage\n\nThis project is helpful if you have both [`ecto`](https://github.com/elixir-lang/ecto) and [`postgrex`](https://github.com/ericmj/postgrex) in your project. It makes no sense to use it without a database.\n\n### Custom type\n\n```elixir\n# VALUES as STRINGS\ndefmodule MyProject.Message.StatusAsString do\n  use Exnumerator,\n    values: [\"sent\", \"read\", \"received\", \"delivered\"]\nend\n\n# VALUES as ATOMS\ndefmodule MyProject.Message.StatusAsAtom do\n  use Exnumerator,\n    values: [:sent, :read, :received, :delivered]\nend\n\n# VALUES as KEYWORD\ndefmodule MyProject.Message.StatusAsKeyword do\n  use Exnumerator,\n    values: [sent: \"S\", read: \"R\", received: \"RE\", delivered: \"D\"]\nend\n```\n\n### Database migration\n\n```elixir\ndefmodule MyProject.Repo.Migrations.CreateMessage do\n  use Ecto.Migration\n\n  def change do\n    create table(:messages) do\n      add :status, :string\n    end\n  end\nend\n```\n\n### Database schema\n\n```elixir\ndefmodule MyProject.Message do\n  use MyProject.Web, :schema\n\n  schema \"messages\" do\n    field :status, MyProject.Message.StatusAsString\n  end\nend\n```\n\n### Operations\n\n**You can see all available values:**\n\n```elixir\niex(1)\u003e MyProject.Message.StatusAsString.values()\n[\"sent\", \"read\", \"received\", \"delivered\"]\n```\n\n```elixir\niex(1)\u003e MyProject.Message.StatusAsAtom.values()\n[:sent, :read, :received, :delivered]\n```\n\n```elixir\niex(1)\u003e MyProject.Message.StatusAsKeyword.values()\n[sent: \"S\", read: \"R\", received: \"RE\", delivered: \"D\"]\n```\n\nWhen you try to insert a record with some value that is not defined, you will get the following error:\n\n```elixir\n# Status should be a String or an Atom, depending of what you use.\n\niex(1)\u003e %MyProject.Message{status: \"invalid\"} |\u003e MyProject.Repo.insert!()\n** (Ecto.ChangeError) value `\"invalid\"` for `MyProject.Message.status`\n   in `insert` does not match type MyProject.Message.status\n```\n\n**You can also pick a random value from the predefined set:**\n\n```elixir\niex(1)\u003e MyProject.Message.StatusAsString.sample()\n\"delivered\"\n\niex(1)\u003e MyProject.Message.StatusAsAtom.sample()\n:delivered\n\niex(1)\u003e MyProject.Message.StatusAsKeyword.sample()\n{:delivered, \"D\"}\n```\n\n**You can pick the first value from the predefined set too:**\n\n```elixir\niex(1)\u003e MyProject.Message.StatusAsString.first()\n\"sent\"\n\niex(1)\u003e MyProject.Message.StatusAsAtom.first()\n:sent\n\niex(1)\u003e MyProject.Message.StatusAsKeyword.first()\n{:sent, \"S\"}\n```\n\n## Testing\n\nTo test this project you need to run:\n\n    mix test\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKamilLelonek%2Fexnumerator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FKamilLelonek%2Fexnumerator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKamilLelonek%2Fexnumerator/lists"}