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

Add base class for wrapping another adapter #846

Merged
merged 2 commits into from
Feb 18, 2024
Merged

Conversation

bkeepers
Copy link
Collaborator

@bkeepers bkeepers commented Feb 18, 2024

This adds Flipper::Adapters::Wrapper, which can be used as a base class for an adapter that just wraps another adapter. By default, all methods delegate to the wrapped adapter. Implement #wrap to customize the behavior of all delegated methods, or override individual methods as needed.

class MyNewAdapter < Flipper::Adapters::Wrapper
  # Override a speciific method to change its behavior
  def enable(feature, gate, resource)
    # custom behavior
    super # invoke wrapped adapter
  end

  # Override this method to customize the behavior of all delegated methods, and just yield to
  # the block to call the wrapped adapter.
  def wrap(method, *args, **kwargs)
     if some_condition?(method)
       yield # call original adapter method
    else
       # do something else
    end
  end
end

And then you can configure Flipper to use the new adapter:

Flipper.configure do |config|
  config.use MyNewAdapter
end

@jnunemaker jnunemaker merged commit 0bdd58d into main Feb 18, 2024
72 checks passed
@jnunemaker jnunemaker deleted the chance-the-wrapper branch February 18, 2024 20:01
@jnunemaker
Copy link
Collaborator

💯 on the branch name.

Merging because I need this in my cache branch.

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