Giter VIP home page Giter VIP logo

attr_bitwise's Introduction

⚠️ NOT MAINTAINED ⚠️

attr_bitwise https://circleci.com/gh/wittydeveloper/attr_bitwise.png?circle-token=7f58370c3b13faaf1954b9e8fe6c7b1fb329daf2 Gem Version

Bitwise attribute for ruby class and Rails model

Features

  • bitwise attribute + helpers, useful for storing multiple states in one place
  • ActiveRecord compatible

Please read this article for some concrete examples

Install

Inline

  • gem install attr_bitwise

Gemfile

  • gem 'attr_bitwise'

Usage

attr_bitwise :<name>, mapping: <values_sym> [, column_name: <column_name>]

Alternatively, you can explicitly specify your states by supplying a hash with the values.

attr_bitwise :<name>, mapping: {<sym1: 1, sym2: 2, sym3: 4>} [, column_name: <column_name>]

Example

You have a website with many locales (English, French, German...) with specific content in each locale. You want your users to be able to chose which content they want to see and you want to be able to query the users by the locales they have chosen.

Start with migration

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      # [...]
      t.integer :locales_value
    end
  end
end

Model

class User < ActiveRecord::Base
  include AttrBitwise

  attr_bitwise :locales, mapping: [:en, :fr, :de]

  scope :with_any_locales, lambda { |*locales_sym|
    where(locales_value: bitwise_union(*locales_sym, 'locales'))
  }

  scope :with_all_locales, lambda { |*locales_sym|
    where(locales_value: bitwise_intersection(*locales_sym, 'locales'))
  }

end

###

# return all users who can see at least english or french content
User.with_any_locales(:en, :fr)

# return all users who can see english and french content
User.with_all_locales(:en, :fr)

API

Examples with name = 'locales'

High level methods

Method Return Description
Class#locales [, ...] Return values as symbols
Class#locales=([value_or_sym, ..]) [, ...] Given an array of values (Fixnum or Symbol) or single value (Fixnum or Symbol) add them to value.
Class#locale == fixnum_or_sym Boolean Return true if value contains only Fixnum or Symbol
Class#locale?(fixnum_or_sym) Boolean Return true if value contains Fixnum or Symbol
Class#add_locale(value_or_sym) Fixnum Add Fixnum or Symbol to value
Class#remove_locale(value_or_sym) Fixnum Remove Fixnum or Symbol to value
Class#locales_union([value_or_sym, ..]) [Fixnum, ..] Given an array of values (Fixnum or Symbol), return bitwise union computation
Return all possible values (mask) for an union of given values
Class#locales_intersection([value_or_sym, ..]) [Fixnum, ..] Given an array of values (Fixnum or Symbol), return bitwise intersection computation
Return all possible values (mask) for an intersection of given values
Class::LOCALES_MAPPING Hash Return Symbol -> Fixnum mapping

Low level methods

Theses methods are static, so a name parameters is mandatory in order to fetch mapping

Method Return Description
Class.to_bitwise_values(object, name) [Fixnum, ...] Given an Object and a attribute name, return Fixnum value depending on mapping
Class.bitwise_union([Fixnum, ..], name) [Fixnum, ..] Given an array of values (Fixnum or Symbol) and a attribute name, return bitwise union computation
Return all possible values (mask) for an union of given values
Class.bitwise_intersection([Fixnum, ..], name) [Fixnum, ..] Given an array of values (Fixnum or Symbol) and a attribute name, return bitwise intersection computation
Return all possible values (mask) for an intersection of given values

Maintainers : @wittydeveloper and @FSevaistre

attr_bitwise's People

Contributors

achinn avatar blid avatar charlypoly avatar fsevaistre avatar kmanzana avatar scottwater avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

attr_bitwise's Issues

Multiple Mapping Columns

Any thoughts to supporting more than one column per model?

By a quick look, if the column name were passed to add_value, remove_value, etc it looks like multiple columns might work.

-Scott

Querying `any` does not returns null values

How should I do the following (using #13 as an example too) :

return all users who can see at least english and french content

user 1 => NULL
user 2 => en

I assume NULL means any cause : User.first.languages => [:en, :fr, :ca]
but : User.with_any_languages(:fr) => []

I am using the same scope :

  scope :with_any_languages, lambda { |*types_sym|
    where(languages_value: bitwise_union(*types_sym, 'languages'))
  }

Is this intended or a bug ?

User Defined Mappings

I wanted to ensure we could explicitly manage our mapping values.

Example:

attr_bitwise :fruits, mapping: {banana: 2, kiwi: 4, apple: 1}

I forked the project and created a branch with this work.

See: https://github.com/scottwater/attr_bitwise/tree/define_values

Would you be interested in a pull request? If so, any suggestion for how you would want to see the specs? Currently I simply copied the existing specs and updated them for the more defined mapping.

Querying data

How would I go about

return all users who can see at least english and french content

user 1 => en, fr, ca
user 2 => en, fr
user 3 => en

I wanna query users with both en and fr, so I wanna get user 1 and user 2

also nice would be opposite: users that do not have them.

ways to migrate to something else

Any suggestions where to migrate? I got a small project and was thinking the gem is a good idea but as it is not maintained anymore I need a way to move to something else?

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.