Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

magic-lookup

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

magic-lookup

  • 0.2.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Code Climate maintainability Code Climate coverage

What is it for?

A bit of history: this gem was inspired by digging deeper into Draper with an eye on refactoring.

A 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.

Modern frameworks tend to use naming conventions over configuration to achieve that. Unfortunately, every one has to implement them on its own struggling through numerous bugs. Moreover, inconsistencies across these implementations lead to misunderstanding and endless tickets when actual behavior fails to meet user expectations based on other frameworks.

So, meet

🔮 Magic Lookup

It’s meant to be The One to Rule Them All — the library to provide a generic name-based lookup for a plenty of cases.

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add magic-lookup

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install magic-lookup

Usage

These are the steps to set up an automatic class inference:

  1. Define a base class extending Magic::Lookup.
  2. Define .name_for method for that class implementing your lookup logic.
  3. From the base class, inherit classes to be looked up.
class Scope
  extend Magic::Lookup

  def self.name_for object_class
    object_class.name
        .delete_suffix('Model')
        .concat('Scope')
  end
end

class MyScope < Scope
end

Scope.for MyModel    # => MyScope
Scope.for OtherModel # => nil

Exception handling

When 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:

scope_class = Scope.for(object.class) or
    raise Magic::Lookup::Error.for(object, Scope)

Magic::Lookup::Error is never raised internally and is meant to be used in your code that implements the lookup logic.

🔮 Magic

Inheritance

Lookup is provided not only for the class itself, but to any of its ancestors as well.

Namespaces

Both of matching classes — the target and its match — may be namespaced independently.

One can specify a namespace to look in:

Scope.for MyModel              # => MyScope
Scope.for MyModel, MyNamespace # => MyNamespace::MyScope

Multiple default lookup namespaces may be set for the base class:

Scope.namespaces << MyNamespace # => [nil, MyNamespace]
Scope.for MyModel               # => MyNamespace::MyScope

[!TIP] Until a comprehensive documentation on all the use cases is released, the spec is recommended as further reading. One can access it by running rake in the gem directory. The output is quite descriptive to get familiar with the use cases.

Development

After 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.

To install this gem onto your local machine, run bundle exec rake install.

Contributing

Bug 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.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Magic Lookup project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

FAQs

Package last updated on 19 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc