{"id":17492092,"url":"https://github.com/imranismail/ecto_bitfield","last_synced_at":"2025-04-22T20:16:31.309Z","repository":{"id":57492862,"uuid":"147312712","full_name":"imranismail/ecto_bitfield","owner":"imranismail","description":"Use bitfields with ecto schemas https://hexdocs.pm/ecto_bitfield/EctoBitfield.html","archived":false,"fork":false,"pushed_at":"2020-04-24T15:56:04.000Z","size":14,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-22T20:16:24.707Z","etag":null,"topics":["bitfield","elixir"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/imranismail.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-04T08:24:02.000Z","updated_at":"2021-11-29T06:08:39.000Z","dependencies_parsed_at":"2022-08-28T13:30:34.552Z","dependency_job_id":null,"html_url":"https://github.com/imranismail/ecto_bitfield","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imranismail%2Fecto_bitfield","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imranismail%2Fecto_bitfield/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imranismail%2Fecto_bitfield/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imranismail%2Fecto_bitfield/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imranismail","download_url":"https://codeload.github.com/imranismail/ecto_bitfield/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250316067,"owners_count":21410476,"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":["bitfield","elixir"],"created_at":"2024-10-19T08:07:41.113Z","updated_at":"2025-04-22T20:16:31.264Z","avatar_url":"https://github.com/imranismail.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EctoBitfield\n\n[![Build Status](https://travis-ci.org/imranismail/ecto_bitfield.svg?branch=master)](https://travis-ci.org/imranismail/ecto_bitfield)\n\nProvides functionality similar to [Ruby's bitfields](https://github.com/grosser/bitfields/)\n\n## Motivation\n\nSay you have a user schema that can have one or more of the following service policy `[:create_user, :update_user, :delete_user]`\n\nTo pack that information into a bit we'd do something like this\n\n`policies = [create_user: 2^0, update_user: 2^1, delete_user: 2^2]`\n\n| create_user    | update_user    | delete_user    | BIT |\n|----------------|----------------|----------------|-----|\n| F              | F              | F              | 0   |\n| T              | F              | F              | 1   |\n| F              | T              | F              | 2   |\n| T              | T              | F              | 3   |\n| F              | F              | T              | 4   |\n| T              | F              | T              | 5   |\n| F              | T              | T              | 6   |\n| T              | T              | T              | 7   |\n\nThis saves you a couple of migrations and headache and is also is memory efficient.\n\n## Installation\n\n```elixir\ndef deps do\n  [\n    {:ecto_bitfield, \"~\u003e 0.1.0\"}\n  ]\nend\n```\n\n## Usage\n\nRun a migration to add a bitfield to the schema\n\n```elixir\nalter table(:users) do\n  add :policies, :integer, default: 0, null: false\nend\n```\n\nDefine the Ecto type and use it as the type for the migrated field\n\n```elixir\ndefmodule User do\n  use Ecto.Schema\n\n  import EctoBitfield\n\n  # takes in a list or a keyword list for explicitly setting the mappings\n  defbitfield Policies, [:create_user, :update_user, :delete_user]\n\n  schema \"users\" do\n    field :policies, User.Policies\n  end\nend\n```\n\n**Reading**\n\n```elixir\nquery = from u in User, where: u.policies == ^[:create_user, :update_user]\n#\u003e %Ecto.Queryable{...}\n\nuser = Repo.one(query)\n#\u003e %User{..., policies: [:create_user, :update_user]}\n```\n\n**Writing**\n\n```elixir\nchangeset = Ecto.Changeset.cast(user, %{policies: [:create_user]}, [:policies])\n#\u003e %Ecto.Changeset{..., changes: %{policices: 1}}\n\nRepo.update(changeset)\n#\u003e {:ok, %User{..., policices: [:create_user]}}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimranismail%2Fecto_bitfield","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimranismail%2Fecto_bitfield","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimranismail%2Fecto_bitfield/lists"}