Git Product home page Git Product logo

ecto_bitfield's Introduction

EctoBitfield

Build Status

Provides functionality similar to Ruby's bitfields

Motivation

Say you have a user schema that can have one or more of the following service policy [:create_user, :update_user, :delete_user]

To pack that information into a bit we'd do something like this

policies = [create_user: 2^0, update_user: 2^1, delete_user: 2^2]

create_user update_user delete_user BIT
F F F 0
T F F 1
F T F 2
T T F 3
F F T 4
T F T 5
F T T 6
T T T 7

This saves you a couple of migrations and headache and is also is memory efficient.

Installation

def deps do
  [
    {:ecto_bitfield, "~> 0.1.0"}
  ]
end

Usage

Run a migration to add a bitfield to the schema

alter table(:users) do
  add :policies, :integer, default: 0, null: false
end

Define the Ecto type and use it as the type for the migrated field

defmodule User do
  use Ecto.Schema

  import EctoBitfield

  # takes in a list or a keyword list for explicitly setting the mappings
  defbitfield Policies, [:create_user, :update_user, :delete_user]

  schema "users" do
    field :policies, User.Policies
  end
end

Reading

query = from u in User, where: u.policies == ^[:create_user, :update_user]
#> %Ecto.Queryable{...}

user = Repo.one(query)
#> %User{..., policies: [:create_user, :update_user]}

Writing

changeset = Ecto.Changeset.cast(user, %{policies: [:create_user]}, [:policies])
#> %Ecto.Changeset{..., changes: %{policices: 1}}

Repo.update(changeset)
#> {:ok, %User{..., policices: [:create_user]}}

ecto_bitfield's People

Contributors

imranismail avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.