{"id":16178171,"url":"https://github.com/charlypoly/attr_bitwise","last_synced_at":"2025-08-17T15:31:38.860Z","repository":{"id":45403456,"uuid":"52019960","full_name":"charlypoly/attr_bitwise","owner":"charlypoly","description":"Bitwise attribute for ruby class and Rails model","archived":false,"fork":false,"pushed_at":"2021-12-15T15:23:32.000Z","size":27,"stargazers_count":68,"open_issues_count":6,"forks_count":9,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-02T00:24:12.498Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/charlypoly.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-02-18T16:08:01.000Z","updated_at":"2024-04-08T22:31:57.000Z","dependencies_parsed_at":"2022-07-21T02:34:56.462Z","dependency_job_id":null,"html_url":"https://github.com/charlypoly/attr_bitwise","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charlypoly%2Fattr_bitwise","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charlypoly%2Fattr_bitwise/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charlypoly%2Fattr_bitwise/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charlypoly%2Fattr_bitwise/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/charlypoly","download_url":"https://codeload.github.com/charlypoly/attr_bitwise/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229843872,"owners_count":18132993,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T05:12:47.895Z","updated_at":"2024-12-17T15:20:44.649Z","avatar_url":"https://github.com/charlypoly.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# :warning: NOT MAINTAINED :warning:\n\n# `attr_bitwise` ![https://circleci.com/gh/wittydeveloper/attr_bitwise.png?circle-token=7f58370c3b13faaf1954b9e8fe6c7b1fb329daf2](https://circleci.com/gh/wittydeveloper/attr_bitwise.png?circle-token=7f58370c3b13faaf1954b9e8fe6c7b1fb329daf2) [![Gem Version](https://badge.fury.io/rb/attr_bitwise.svg)](https://badge.fury.io/rb/attr_bitwise)\nBitwise attribute for ruby class and Rails model\n\n## Features\n\n- bitwise attribute + helpers, useful for storing multiple states in one place\n- ActiveRecord compatible\n\n[Please read this article for some concrete examples](https://medium.com/jobteaser-dev-team/rails-bitwise-enum-with-super-powers-5030bda5dbab)\n\n\n## Install\n\n\n### Inline\n\n- `gem install attr_bitwise`\n\n### Gemfile\n\n- `gem 'attr_bitwise'`\n\n\n## Usage\n\n```ruby\nattr_bitwise :\u003cname\u003e, mapping: \u003cvalues_sym\u003e [, column_name: \u003ccolumn_name\u003e]\n```\n\nAlternatively, you can explicitly specify your states by supplying a hash with the values.\n\n```ruby\nattr_bitwise :\u003cname\u003e, mapping: {\u003csym1: 1, sym2: 2, sym3: 4\u003e} [, column_name: \u003ccolumn_name\u003e]\n```\n\n\n## Example\n\nYou 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.\n\nStart with migration\n```ruby\nclass CreateUsers \u003c ActiveRecord::Migration\n  def change\n    create_table :users do |t|\n      # [...]\n      t.integer :locales_value\n    end\n  end\nend\n```\n\nModel\n```ruby\n\nclass User \u003c ActiveRecord::Base\n  include AttrBitwise\n\n  attr_bitwise :locales, mapping: [:en, :fr, :de]\n\n  scope :with_any_locales, lambda { |*locales_sym|\n    where(locales_value: bitwise_union(*locales_sym, 'locales'))\n  }\n\n  scope :with_all_locales, lambda { |*locales_sym|\n    where(locales_value: bitwise_intersection(*locales_sym, 'locales'))\n  }\n\nend\n\n###\n\n# return all users who can see at least english or french content\nUser.with_any_locales(:en, :fr)\n\n# return all users who can see english and french content\nUser.with_all_locales(:en, :fr)\n\n```\n\n\n## API\n\n**Examples with \u003ccode\u003ename = 'locales'\u003c/code\u003e**\n\n### High level methods\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003cth\u003eMethod\u003c/th\u003e\n    \u003cth\u003eReturn\u003c/th\u003e\n    \u003cth\u003eDescription\u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eClass#locales\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003e[\u003cSymbol\u003e, ...]\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      Return values as symbols\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eClass#locales=([value_or_sym, ..])\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003e[\u003cSymbol\u003e, ...]\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      Given an array of values (Fixnum or Symbol) or single value (Fixnum or Symbol) add them to value.\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eClass#locale == fixnum_or_sym\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eBoolean\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      Return true if value contains only Fixnum or Symbol\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eClass#locale?(fixnum_or_sym)\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eBoolean\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      Return true if value contains Fixnum or Symbol\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eClass#add_locale(value_or_sym)\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eFixnum\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      Add Fixnum or Symbol to value\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eClass#remove_locale(value_or_sym)\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eFixnum\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      Remove Fixnum or Symbol to value\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eClass#locales_union([value_or_sym, ..])\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003e[Fixnum, ..]\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      Given an array of values (Fixnum or Symbol), return bitwise union computation \u003cbr\u003e\n      Return all possible values (mask) for an union of given values\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eClass#locales_intersection([value_or_sym, ..])\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003e[Fixnum, ..]\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      Given an array of values (Fixnum or Symbol), return bitwise intersection computation \u003cbr\u003e\n      Return all possible values (mask) for an intersection of given values\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eClass::LOCALES_MAPPING\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eHash\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      Return \u003ccode\u003eSymbol\u003c/code\u003e -\u003e \u003ccode\u003eFixnum\u003c/code\u003e mapping\n    \u003c/td\u003e\n  \u003c/tr\u003e\n\n\n\u003c/table\u003e\n\n\n### Low level methods\n\n*Theses methods are static, so a \u003ccode\u003ename\u003c/code\u003e parameters is mandatory in order to fetch mapping*\n\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003cth\u003eMethod\u003c/th\u003e\n    \u003cth\u003eReturn\u003c/th\u003e\n    \u003cth\u003eDescription\u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eClass.to_bitwise_values(object, name)\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003e[Fixnum, ...]\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      Given an Object and a attribute name, return Fixnum value depending on mapping\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eClass.bitwise_union([Fixnum, ..], name)\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003e[Fixnum, ..]\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      Given an array of values (Fixnum or Symbol) and a attribute name, return bitwise union computation \u003cbr\u003e\n      Return all possible values (mask) for an union of given values\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\n      \u003ccode\u003eClass.bitwise_intersection([Fixnum, ..], name)\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      \u003ccode\u003e[Fixnum, ..]\u003c/code\u003e\n    \u003c/td\u003e\n    \u003ctd\u003e\n      Given an array of values (Fixnum or Symbol) and a attribute name, return bitwise intersection computation \u003cbr\u003e\n      Return all possible values (mask) for an intersection of given values\n    \u003c/td\u003e\n  \u003c/tr\u003e\n\n\u003c/table\u003e\n\n\n----------------------------------------\nMaintainers :  [@wittydeveloper](https://github.com/wittydeveloper) and [@FSevaistre](https://github.com/FSevaistre)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcharlypoly%2Fattr_bitwise","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcharlypoly%2Fattr_bitwise","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcharlypoly%2Fattr_bitwise/lists"}