https://github.com/charlypoly/attr_bitwise
Bitwise attribute for ruby class and Rails model
https://github.com/charlypoly/attr_bitwise
Last synced: 10 months ago
JSON representation
Bitwise attribute for ruby class and Rails model
- Host: GitHub
- URL: https://github.com/charlypoly/attr_bitwise
- Owner: charlypoly
- License: mit
- Created: 2016-02-18T16:08:01.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2021-12-15T15:23:32.000Z (over 4 years ago)
- Last Synced: 2024-12-02T00:24:12.498Z (over 1 year ago)
- Language: Ruby
- Size: 26.4 KB
- Stars: 68
- Watchers: 1
- Forks: 9
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# :warning: NOT MAINTAINED :warning:
# `attr_bitwise`  [](https://badge.fury.io/rb/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](https://medium.com/jobteaser-dev-team/rails-bitwise-enum-with-super-powers-5030bda5dbab)
## Install
### Inline
- `gem install attr_bitwise`
### Gemfile
- `gem 'attr_bitwise'`
## Usage
```ruby
attr_bitwise :, mapping: [, column_name: ]
```
Alternatively, you can explicitly specify your states by supplying a hash with the values.
```ruby
attr_bitwise :, mapping: {} [, 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
```ruby
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
# [...]
t.integer :locales_value
end
end
end
```
Model
```ruby
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](https://github.com/wittydeveloper) and [@FSevaistre](https://github.com/FSevaistre)