ActWithBooleans
A Ruby gem handling booleans placed in an integer.
Defines setters and getters to access the booleans.
Installation
As usual:
gem "act_with_booleans"
and run "bundle install".
Examples
Simple Example
class Foo
include ActWithBooleans
attr_accessor :booleans
add_to_booleans :x, :y
end
foo = Foo.new
foo.x
foo.x = true
foo.x
foo.x?
Foo.booleans_mask(:x, :y)
Example Using Position
class Foo
add_to_booleans z: 10
end
Foo.booleans_mask(:z)
Additional Functions
foo.x = true
foo.y = false
foo.booleans_any?(:x, :y)
foo.booleans_all?(:x, :y)
foo.booleans_none?(:x, :y)
Using a Non Default Origin
class Foo
include ActWithBooleans
attr_accessor :flags
add_to_booleans :x, :y, origin: :flags
end
foo = Foo.new
foo.x
Internals
Foo.act_with_booleans.size
Foo.act_with_booleans.position(:y)
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-2024 Dittmar Krall (www.matiq.com),
released under the MIT license.