{"id":16834646,"url":"https://github.com/alexander-senko/magic-lookup","last_synced_at":"2026-03-04T17:30:54.729Z","repository":{"id":257820518,"uuid":"870537648","full_name":"Alexander-Senko/magic-lookup","owner":"Alexander-Senko","description":"Find a related class for an object (ex., a decorator, a presenter, a controller, or whatever).","archived":false,"fork":false,"pushed_at":"2025-05-20T14:02:50.000Z","size":44,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-03T12:39:22.499Z","etag":null,"topics":["ruby"],"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/Alexander-Senko.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","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,"zenodo":null}},"created_at":"2024-10-10T08:15:37.000Z","updated_at":"2025-05-20T14:03:15.000Z","dependencies_parsed_at":null,"dependency_job_id":"c2a8856a-32f8-4481-be73-319f68ea657f","html_url":"https://github.com/Alexander-Senko/magic-lookup","commit_stats":null,"previous_names":["alexander-senko/magic-lookup"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/Alexander-Senko/magic-lookup","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexander-Senko%2Fmagic-lookup","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexander-Senko%2Fmagic-lookup/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexander-Senko%2Fmagic-lookup/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexander-Senko%2Fmagic-lookup/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Alexander-Senko","download_url":"https://codeload.github.com/Alexander-Senko/magic-lookup/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexander-Senko%2Fmagic-lookup/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30087301,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T15:40:14.053Z","status":"ssl_error","status_checked_at":"2026-03-04T15:40:13.655Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["ruby"],"created_at":"2024-10-13T12:07:13.132Z","updated_at":"2026-03-04T17:30:54.705Z","avatar_url":"https://github.com/Alexander-Senko.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Code Climate maintainability](\n\thttps://img.shields.io/codeclimate/maintainability-percentage/Alexander-Senko/magic-lookup\n)\n![Code Climate coverage](\n\thttps://img.shields.io/codeclimate/coverage/Alexander-Senko/magic-lookup\n)\n\n## What is it for?\n\nA bit of history: this gem was inspired by digging deeper into [Draper](https://github.com/drapergem/draper) with an eye on refactoring.\n\nA common task for an application is to match its parts against each other, be it inferring a default model class for a controller, or looking up a decorator for a model, or whatever else.\n\nModern frameworks tend to use naming conventions over configuration to achieve that.\nUnfortunately, every one has to implement them on its own struggling through numerous bugs.\nMoreover, inconsistencies across these implementations lead to misunderstanding and endless tickets when actual behavior fails to meet user expectations based on other frameworks.\n\nSo, meet\n\n# 🔮 Magic Lookup\n\nIt’s meant to be The One to Rule Them All — the library to provide a generic name-based lookup for a plenty of cases.\n\n## Installation\n\nInstall the gem and add to the application's Gemfile by executing:\n\n    $ bundle add magic-lookup\n\nIf bundler is not being used to manage dependencies, install the gem by executing:\n\n    $ gem install magic-lookup\n\n## Usage\n\nThese are the steps to set up an automatic class inference:\n\n1. Define a base class extending `Magic::Lookup`.\n2. Define `.name_for` method for that class implementing your lookup logic.\n3. From the base class, inherit classes to be looked up.\n\n```ruby\nclass Scope\n  extend Magic::Lookup\n\n  def self.name_for object_class\n    object_class.name\n        .delete_suffix('Model')\n        .concat('Scope')\n  end\nend\n\nclass MyScope \u003c Scope\nend\n\nScope.for MyModel    # =\u003e MyScope\nScope.for OtherModel # =\u003e nil\n```\n\n### Exception handling\n\nWhen no class is found, `nil` is returned. If you need to raise an exception in this case, you can use `Magic::Lookup::Error` like this:\n\n```ruby\nscope_class = Scope.for(object.class) or\n    raise Magic::Lookup::Error.for(object, Scope)\n```\n\n`Magic::Lookup::Error` is never raised internally and is meant to be used in your code that implements the lookup logic.\n\n## 🔮 Magic\n\n### Inheritance\n\nLookup is provided not only for the class itself, but to any of its ancestors as well.\n\n### Namespaces\n\nBoth of matching classes — the target and its match — may be namespaced independently.\n\nOne can specify a namespace to look in:\n\n```ruby\nScope.for MyModel              # =\u003e MyScope\nScope.for MyModel, MyNamespace # =\u003e MyNamespace::MyScope\n```\n\nMultiple default lookup namespaces may be set for the base class:\n\n```ruby\nScope.namespaces \u003c\u003c MyNamespace # =\u003e [nil, MyNamespace]\nScope.for MyModel               # =\u003e MyNamespace::MyScope\n```\n\n\u003e [!TIP]\n\u003e Until a comprehensive documentation on all the use cases is released, the spec is recommended as further reading.\n\u003e One can access it by running `rake` in the gem directory.\n\u003e The output is quite descriptive to get familiar with the use cases.\n\n## Known issues\n\n### https://github.com/Alexander-Senko/magic-lookup/issues/1\n\n\u003e [!IMPORTANT]\n\u003e Magic Lookup doesn’t try to autoload any classes, it searches among already loaded ones instead.\n\u003e Thus, one should preload all classes that need to be accessible via the lookup.\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`.\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/Alexander-Senko/magic-lookup. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/Alexander-Senko/magic-lookup/blob/main/CODE_OF_CONDUCT.md).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the Magic Lookup project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Alexander-Senko/magic-lookup/blob/main/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexander-senko%2Fmagic-lookup","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexander-senko%2Fmagic-lookup","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexander-senko%2Fmagic-lookup/lists"}