Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make adapters config work more natively #12

Closed
wants to merge 4 commits into from

Conversation

dsalahutdinov
Copy link
Contributor

A little bit clearly code of adapters config

@dsalahutdinov
Copy link
Contributor Author

dsalahutdinov commented Mar 5, 2018

Supposing profit of this PR is:

  1. less code, less problems :), especially when you have method_missing 😄
  2. it fails more clearly with NoMethodError, in case you fogot to require some lib (and the adapter was not plugged in) 😏
  3. it works much faster 🚀

@palkan
Copy link
Owner

palkan commented Mar 5, 2018

Having adapters behave like a Hash has some advantages: we can list (iterate) adapters, delete and re-register them.

it fails more clearly with NoMethodError,

That makes sense. Maybe, it's better to make adapters a wrapper over Hash instance and delegate Enumerable methods to it (i.e. include Enumerable and define each method).

it works much faster

It doesn't matter in our case)

@dsalahutdinov
Copy link
Contributor Author

Made some changes about wrapping the hash, does it now look better?

class Config
include Enumerable
extend Forwardable
def_delegators :hash_config, :each, :[]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use a shorter name, e.g. store


def register(id, adapter)
raise "Adapter already registered: #{id}" if respond_to?(id)
define_singleton_method(id) { self[id] }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the previous method_missing approach but without fallback to super (in order to raise NoMethodError when no such key).

The provided approach doesn't play well with deleting keys: after a key has been deleted we except to got an exception; and now it returns nil instead.

@dsalahutdinov
Copy link
Contributor Author

I've changed code for use method_missing

raise "Adapter already registered: #{id}" if Isolator.adapters.key?(id.to_s)
adapter = AdapterBuilder.call(**options)
Isolator.adapters[id.to_s] = adapter
AdapterBuilder.call(**options).tap do |adapter|
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should raise error before .call, 'cause we're patching a target module in .call.

@palkan
Copy link
Owner

palkan commented Mar 26, 2018

Let's leave everything as is; no need for complexity

@palkan palkan closed this Mar 26, 2018
@dsalahutdinov
Copy link
Contributor Author

👍 yes, it became too complicated. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants