{"id":18044792,"url":"https://github.com/will/switchbox","last_synced_at":"2025-06-13T18:33:07.995Z","repository":{"id":422602,"uuid":"42522","full_name":"will/switchbox","owner":"will","description":"declarative ruby conditions","archived":false,"fork":false,"pushed_at":"2008-08-22T05:34:45.000Z","size":77,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-05T03:27:25.365Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/will.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2008-08-14T04:33:27.000Z","updated_at":"2019-08-13T13:31:35.000Z","dependencies_parsed_at":"2022-08-16T10:25:11.352Z","dependency_job_id":null,"html_url":"https://github.com/will/switchbox","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/will/switchbox","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/will%2Fswitchbox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/will%2Fswitchbox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/will%2Fswitchbox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/will%2Fswitchbox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/will","download_url":"https://codeload.github.com/will/switchbox/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/will%2Fswitchbox/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259698957,"owners_count":22898166,"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-30T18:10:45.848Z","updated_at":"2025-06-13T18:33:07.965Z","avatar_url":"https://github.com/will.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"SwitchBox\n=========\n\nWill Leinweber – [blog](http://bitfission.com)\n\nAbout\n-----\n\nSwitchBox lets you declare simple conditions and combine them into more complex conditions. These are used to decide which action to run.\n\nThe upshot is that messy if/elsif/else blocks are no longer necessary. All conditions are lazily evaluated and will only be ran once.\n\nConditions\n----------\n\nConditions have a name and a block. They are only run when checking for which action to call, and their result is cached and only ran once. The block doesn't necessarily have to evaluate a boolean, but SwitchBox forces the result into a boolean value before caching.\n\n    record = some_active_record_object\n    \n    box = SwitchBox.new\n    box.condition(:saved) { record.save }\n\nIn this example, a condition named \"saved\" is created. Behind-the-scenes, you also get a \"not_saved\" condition.\n\nActions\n-------\n\nActions are stored in the order they're declared, and the very first action to meet all of its conditions will be executed, and no others.\n\n    # ... continuing from the above code\n    box.action(:saved)     { render :text =\u003e \"the record was saved!\" }\n    box.action(:not_saved) { render :text =\u003e \"there was a problem\" }\n    box.go # will result in calling the first block\n    \nSwitchBox#go will look at the conditions for each action, and run appropriate action.\n\nAlternate syntax\n----------------\n\nThe above syntax is nice because you can pass the box around as needed, but for simple things, you can just do it all in one block\n\n    SwitchBox.new do\n      condition(:always_true) { true }\n      action(:always_true) { puts \"always printed\" }\n    end\n    \nIn this case you don't need to call #go, it gets called at the end of the block.\n      ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwill%2Fswitchbox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwill%2Fswitchbox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwill%2Fswitchbox/lists"}