{"id":13747260,"url":"https://github.com/evanthegrayt/attribool","last_synced_at":"2025-05-09T08:32:10.155Z","repository":{"id":42125936,"uuid":"342131334","full_name":"evanthegrayt/attribool","owner":"evanthegrayt","description":"✅ Ruby macros for creating boolean methods for attributes that may or may not be booleans themselves.","archived":false,"fork":false,"pushed_at":"2024-08-02T02:36:43.000Z","size":514,"stargazers_count":1,"open_issues_count":4,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-02T09:48:01.986Z","etag":null,"topics":["attributes","booleans","gem","macros","ruby","rubygem"],"latest_commit_sha":null,"homepage":"","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/evanthegrayt.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-25T05:17:28.000Z","updated_at":"2024-08-02T02:28:59.000Z","dependencies_parsed_at":"2024-01-13T02:57:25.606Z","dependency_job_id":"6f85c9dd-9c83-417b-8be2-66031c37fc35","html_url":"https://github.com/evanthegrayt/attribool","commit_stats":{"total_commits":24,"total_committers":1,"mean_commits":24.0,"dds":0.0,"last_synced_commit":"227d1df0d663e6d919f6e0ab23b035ded1ae3e67"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evanthegrayt%2Fattribool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evanthegrayt%2Fattribool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evanthegrayt%2Fattribool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evanthegrayt%2Fattribool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/evanthegrayt","download_url":"https://codeload.github.com/evanthegrayt/attribool/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224842660,"owners_count":17379009,"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":["attributes","booleans","gem","macros","ruby","rubygem"],"created_at":"2024-08-03T06:01:23.057Z","updated_at":"2024-11-15T20:31:24.693Z","avatar_url":"https://github.com/evanthegrayt.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# Attribool\n[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fevanthegrayt%2Fattribool%2Fbadge%3Fref%3Dmaster\u0026style=flat)](https://actions-badge.atrox.dev/evanthegrayt/attribool/goto?ref=master)\n[![Gem Version](https://badge.fury.io/rb/attribool.svg)](https://badge.fury.io/rb/attribool)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nRuby macros for creating boolean methods for attributes that may or may not be\nbooleans themselves. This is done via either coercion based on truthiness, or a\nuser-defined condition.\n\nFor example, if you have an attribute of `@name`, and you want to know if\n`@name` is not `nil`, you can declare `bool_reader :name`, which will define the\nmethod `name?`. This method will return true if `@name` is truthy.\n\nThe `bool_reader` also comes with some handy options. For example, you can\n[define a method name](#a-bool_reader-with-method-name) that makes\nmore sense. Using the same example as above, if your attribute is `@name`, but\nyou'd like for your boolean method to be called `named?`, you can use\n`bool_reader :name, method_name: :named?`.\n[Conditionals](#a-bool_reader-with-method-name-and-conditional) can also be set\nwith lambdas via the `condition:` keyword argument.\n\nThe first argument is always the instance variable to check for truthiness.\n\nMacros also exist for `bool_writer` and `bool_accessor`. When a writer\nmethod is defined, the value will always be coerced into a boolean before\nsetting the attribute.\n\nYou can read the documentation [here](https://evanthegrayt.github.io/attribool/).\n\n## Installation\n#### Via Gemfile\n```ruby\ngem \"attribool\"\n```\n\n#### Via rubygems\n```sh\ngem install attribool\n```\n\n#### From source\n```sh\ngit clone https://github.com/evanthegrayt/attribool.git\ncd attribool\nbundle exec rake install\n```\n\n## Examples\n#### Standard bool_reader\n```ruby\nrequire \"attribool\"\n\nclass Person\n  extend Attribool\n\n  attr_accessor :name\n  bool_reader :name\nend\n\nperson = Person.new\nperson.name?\n# false, because @name is nil.\n\nperson.name = \"John Smith\"\nperson.name\n# \"John Smith\"\nperson.name?\n# true, because @name is truthy.\n```\n\n#### A bool_reader with method name\n```ruby\nrequire \"attribool\"\n\nclass Person\n  extend Attribool\n\n  attr_accessor :name\n  bool_reader :name, method_name: :named?\nend\n\nperson = Person.new\nperson.named?\n# false, because @name is nil.\n\nperson.name = \"John Smith\"\nperson.named?\n# true, because @name is truthy.\n```\n\n#### A bool_reader with method name and conditional\n```ruby\nrequire \"attribool\"\n\nclass Person\n  extend Attribool\n\n  attr_accessor :age\n  # In the condition lambdas, the argument refers to the attribute's value.\n  bool_reader :age, method_name: :adult?, condition: -\u003e(a) { a.to_i \u003e= 18 }\nend\n\nperson = Person.new\nperson.adult?\n# false, because @age is nil, which the to_i converts to 0.\n\nperson.age = 10\nperson.adult?\n# false, because @age is less than 18.\n\nperson.age = 20\nperson.adult?\n# true, because @age is greater than 18.\n```\n\nNote that, the result of the proc will also be coerced into a boolean, so the\ncondition can just return a truthy or falsey value.\n\n#### Assigning more than one bool_reader with a method name at once\n```ruby\nrequire \"attribool\"\n\nclass Person\n  extend Attribool\n\n  attr_accessor :name, :age\n  # When creating multiple readers at once, if you want to specify a\n  # method_name, you must provide a Proc as the argument, where the attribute\n  # name is the argument.\n  bool_reader :age, :name, method_name: -\u003e(m) { \"has_#{m}?\" }\nend\n\nperson = Person.new\nperson.has_age?\nperson.has_name?\n# Both false, because @age and @name are nil.\n\nperson.age = 10\nperson.has_age?\n# true, because @age is not nil.\n\nperson.name = \"Bob\"\nperson.has_name?\n# true, because @name is not nil.\n```\n\n#### Standard bool_accessor\n```ruby\nrequire \"attribool\"\n\nclass Person\n  extend Attribool\n\n  bool_accessor :living\nend\n\nperson = Person.new\nperson.living?\n# false, because @living is nil.\n\nperson.living = true\nperson.living?\n# true, because @living is true.\n# Be aware -- if you pass anything truthy, it will be coerced to true!\n```\n\n#### Standard bool_writer\nIn most cases where you'd use a `bool_writer`, you'd probably want to just use\n`bool_accessor`. This example is to show that, even when using `bool_accessor`,\nthe value is coerced to a boolean when the value is set by `bool_writer`.\n```ruby\nrequire \"attribool\"\n\nclass Person\n  extend Attribool\n\n  attr_reader :living\n  bool_writer :living\nend\n\nperson = Person.new\nperson.living\n# nil\n\nperson.living = \"blah\"\nperson.living\n# true, because \"blah\" was coerced into a boolean because strings are truthy.\n```\n\n## Reporting Bugs and Requesting Features\nIf you have an idea or find a bug, please [create an\nissue](https://github.com/evanthegrayt/attribool/issues/new). Just make sure\nthe topic doesn't already exist. Better yet, you can always submit a Pull\nRequest.\n\n## Self-Promotion\nI do these projects for fun, and I enjoy knowing that they're helpful to people.\nConsider starring [the repository](https://github.com/evanthegrayt/attribool)\nif you like it! If you love it, follow me [on\nGitHub](https://github.com/evanthegrayt)!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevanthegrayt%2Fattribool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fevanthegrayt%2Fattribool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevanthegrayt%2Fattribool/lists"}