GithubHelp home page GithubHelp logo

foca / flagpole Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 1.0 118 KB

Store multiple flags in an Integer field by using a bitmap

License: MIT License

Makefile 15.61% Ruby 84.39%
lesscode ruby bitfield flags

flagpole's Introduction

Flagpole: Simple bitmap for storing flags

Flagpole allows bundling a bunch of flags into a single integer field, by storing each flag as a bit.

notifications_via = Flagpole.new([:email, :sms, :phone_push])
notifications_via.to_h #=> { email: false, sms: false, phone_push: false }

notifications_via[:email] = true
notifications_via[:sms] = true

notifications_via.to_h #=> { email: true, sms: true, phone_push: false }
notifications_via.to_i #=> 3

Now you can just store that 3 in your database. In order to get that back, just pass it to the constructor:

notifications_via = Flagpole.new(3, [:email, :sms, :phone_push])
notifications_via.to_h #=> { email: true, sms: true, phone_push: false }

Install

gem install flagpole

What kind of sorcery is this?!

If you consider each flag as a "bit" (1 when true, 0 when false), then the above example could be represented as [1, 1, 0]. If you reverse this, you get the binary representation of the number 3 (11).

All Flagpole does is give you a nice API to treat sets of named binary settings ("flags") as an integer, by doing the marshaling for you.

Read more about bitmaps on Wikipedia.

I want to add a new flag

Easy! Just add a new name at the end of the array of flag names. So, In the example above, if you later add support for twitter notifications, just do this:

notifications_via = Flagpole.new(3, [:email, :sms, :phone_push, :twitter])
notifications_via.to_h #=> { email: true, sms: true, phone_push: false, twitter: false }

As you see, the values are maintained.

If you wanted the new flag to default to true, you just need to increment all the values. You do this by using #value_of:

flags = Flagpole([:email, :sms, :phone_push, :twitter])
flags.value_of(:twitter) #=> 8

Now all you have to do is add 8 to all your stored values, and voila, everyone has twitter notifications enabled.

I want to remove an existing flag

In order to remove a flag, you will need to modify the integer values from your storage. Assuming the example from above, let's say you no longer wish to support SMS notifications.

In order to find out the value to substract, you can use #value_of:

flags = Flagpole([:email, :sms, :phone_push, :twitter])
flags.value_of(:sms) #=> 2

Now you need to substract 2 from all the values that have the SMS flag set. In order to do this, you need to do a bitwise and between the value and 2. Those that are non-zero, are the ones that have it set. In SQL this would be:

UPDATE users
SET notification_settings = notification_settings - 2
WHERE notification_settings & 2 <> 0

License

This project is shared under the MIT license. See the attached LICENSE file for details.

Photo credit: Cesarexpo via Wikimedia Commons.

flagpole's People

Contributors

foca avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

ddd-ruby

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.