Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/evanthegrayt/attribool
✅ Ruby macros for creating boolean methods for attributes that may or may not be booleans themselves.
https://github.com/evanthegrayt/attribool
attributes booleans gem macros ruby rubygem
Last synced: 3 months ago
JSON representation
✅ Ruby macros for creating boolean methods for attributes that may or may not be booleans themselves.
- Host: GitHub
- URL: https://github.com/evanthegrayt/attribool
- Owner: evanthegrayt
- License: mit
- Created: 2021-02-25T05:17:28.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-08-02T02:36:43.000Z (3 months ago)
- Last Synced: 2024-08-02T03:45:50.717Z (3 months ago)
- Topics: attributes, booleans, gem, macros, ruby, rubygem
- Language: Ruby
- Homepage:
- Size: 502 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Attribool
[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fevanthegrayt%2Fattribool%2Fbadge%3Fref%3Dmaster&style=flat)](https://actions-badge.atrox.dev/evanthegrayt/attribool/goto?ref=master)
[![Gem Version](https://badge.fury.io/rb/attribool.svg)](https://badge.fury.io/rb/attribool)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)Ruby macros for creating boolean methods for attributes that may or may not be
booleans themselves. This is done via either coercion based on truthiness, or a
user-defined condition.For example, if you have an attribute of `@name`, and you want to know if
`@name` is not `nil`, you can declare `bool_reader :name`, which will define the
method `name?`. This method will return true if `@name` is truthy.The `bool_reader` also comes with some handy options. For example, you can
[define a method name](#a-bool_reader-with-method-name) that makes
more sense. Using the same example as above, if your attribute is `@name`, but
you'd like for your boolean method to be called `named?`, you can use
`bool_reader :name, method_name: :named?`.
[Conditionals](#a-bool_reader-with-method-name-and-conditional) can also be set
with lambdas via the `condition:` keyword argument.The first argument is always the instance variable to check for truthiness.
Macros also exist for `bool_writer` and `bool_accessor`. When a writer
method is defined, the value will always be coerced into a boolean before
setting the attribute.You can read the documentation [here](https://evanthegrayt.github.io/attribool/).
## Installation
#### Via Gemfile
```ruby
gem "attribool"
```#### Via rubygems
```sh
gem install attribool
```#### From source
```sh
git clone https://github.com/evanthegrayt/attribool.git
cd attribool
bundle exec rake install
```## Examples
#### Standard bool_reader
```ruby
require "attribool"class Person
extend Attriboolattr_accessor :name
bool_reader :name
endperson = Person.new
person.name?
# false, because @name is nil.person.name = "John Smith"
person.name
# "John Smith"
person.name?
# true, because @name is truthy.
```#### A bool_reader with method name
```ruby
require "attribool"class Person
extend Attriboolattr_accessor :name
bool_reader :name, method_name: :named?
endperson = Person.new
person.named?
# false, because @name is nil.person.name = "John Smith"
person.named?
# true, because @name is truthy.
```#### A bool_reader with method name and conditional
```ruby
require "attribool"class Person
extend Attriboolattr_accessor :age
# In the condition lambdas, the argument refers to the attribute's value.
bool_reader :age, method_name: :adult?, condition: ->(a) { a.to_i >= 18 }
endperson = Person.new
person.adult?
# false, because @age is nil, which the to_i converts to 0.person.age = 10
person.adult?
# false, because @age is less than 18.person.age = 20
person.adult?
# true, because @age is greater than 18.
```Note that, the result of the proc will also be coerced into a boolean, so the
condition can just return a truthy or falsey value.#### Assigning more than one bool_reader with a method name at once
```ruby
require "attribool"class Person
extend Attriboolattr_accessor :name, :age
# When creating multiple readers at once, if you want to specify a
# method_name, you must provide a Proc as the argument, where the attribute
# name is the argument.
bool_reader :age, :name, method_name: ->(m) { "has_#{m}?" }
endperson = Person.new
person.has_age?
person.has_name?
# Both false, because @age and @name are nil.person.age = 10
person.has_age?
# true, because @age is not nil.person.name = "Bob"
person.has_name?
# true, because @name is not nil.
```#### Standard bool_accessor
```ruby
require "attribool"class Person
extend Attriboolbool_accessor :living
endperson = Person.new
person.living?
# false, because @living is nil.person.living = true
person.living?
# true, because @living is true.
# Be aware -- if you pass anything truthy, it will be coerced to true!
```#### Standard bool_writer
In most cases where you'd use a `bool_writer`, you'd probably want to just use
`bool_accessor`. This example is to show that, even when using `bool_accessor`,
the value is coerced to a boolean when the value is set by `bool_writer`.
```ruby
require "attribool"class Person
extend Attriboolattr_reader :living
bool_writer :living
endperson = Person.new
person.living
# nilperson.living = "blah"
person.living
# true, because "blah" was coerced into a boolean because strings are truthy.
```## Reporting Bugs and Requesting Features
If you have an idea or find a bug, please [create an
issue](https://github.com/evanthegrayt/attribool/issues/new). Just make sure
the topic doesn't already exist. Better yet, you can always submit a Pull
Request.## Self-Promotion
I do these projects for fun, and I enjoy knowing that they're helpful to people.
Consider starring [the repository](https://github.com/evanthegrayt/attribool)
if you like it! If you love it, follow me [on
GitHub](https://github.com/evanthegrayt)!