attr_bitwise
                                
                                
                                
                                    attr_bitwise copied to clipboard
                            
                            
                            
                        Bitwise attribute for ruby class and Rails model
:warning: NOT MAINTAINED :warning:
attr_bitwise 
 
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