Skip to content

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

3 participants