A long-lived project that still receives updates
Handles booleans packed in an integer
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

 Project Readme

ActWithBooleans

Gem Version Downloads GitHub Build Ruby Style Guide MIT License

A Ruby gem handling booleans placed in an integer.

Defines setters and getters to access the booleans.

Installation

As usual:

# Gemfile
gem "act_with_booleans"

and run "bundle install".

Examples

Simple Example

#require "act_with_booleans"

class Foo
  include ActWithBooleans
  attr_accessor :booleans

  add_to_booleans :x, :y
end

foo = Foo.new
foo.x    # --> false
foo.x = true
foo.x    # --> true
foo.x?   # --> true
Foo.booleans_mask(:x, :y) # 3 (0x03)

Example Using Position

class Foo
  add_to_booleans z: 10
end

Foo.booleans_mask(:z) # 1024 (0x400 or 2 ** 10)

Additional Functions

foo.x = true
foo.y = false
foo.booleans_any?(:x, :y)  # true
foo.booleans_all?(:x, :y)  # false
foo.booleans_none?(:x, :y) # false

Using a Non Default Origin

#require "act_with_booleans"

class Foo
  include ActWithBooleans
  attr_accessor :flags

  add_to_booleans :x, :y, origin: :flags
end

foo = Foo.new
foo.x    # --> false

Internals

Foo.act_with_booleans.size # 11
Foo.act_with_booleans.position(:y) # 1
Foo.act_with_booleans.to_s

Rails

ActWithBooleans is PORO, i.e. it can and, usually, will be used in Rails.

The "origin" (default :booleans) is an integer containing the booleans.

Ruby supports pretty large integers, but your database has limitations. Therefore, it is strongly recommended to validate accordingly the "origin" in the model.

Testing

As "Best Practice" a test coverage of 100% has been achieved (and should be kept).

GitHub workflow enable tests for several configurations. Please, feel free to inspect the corresponding file.

Links

Further reading:

Miscellaneous

Copyright (c) 2023-2025 Dittmar Krall (www.matiq.com), released under the MIT license.