assaf / vanity

Experiment Driven Development for Ruby
http://vanity.labnotes.org
MIT License
1.55k stars 269 forks source link

Be able to configure default callbacks #367

Closed frostmark closed 2 years ago

frostmark commented 2 years ago

Hi there!

I would like to suggest adding the ability for configuring default callbacks (for example, on_assignment)

I see an interface something like this:

Vanity.configure do |config|
  config.on_assignment = proc do |...params|
    # does something useful 
  end

  config.after_assignment = proc do |...params|
    # does something useful 
  end
end

Also, we can redefine the default callback if we define that inside some test https://github.com/assaf/vanity/blob/4e3b416b5a759af4f71d85ee8da3e923d46dd82e/lib/vanity/experiment/base.rb#L90

That will help define some logic once. In my case: I want to make pub/sub logic and broadcast events from after_assignment callback:

class VanityEventPublisher
  include Wisper::Publisher

  def after_assignment(assignment)
    broadcast(:after_assignment, assignment)
  end
end

class AbTestEventListener
  def after_assignment(assignment)
    Analytics::Tracker.track!(:some_event)
  end
end

Vanity.configure do |config|
  config.after_assignment = proc do |assignment|
    VanityEventPublisher.new.after_assignment(assignment)
  end
end

Also, I think this would be helpful for some loggers and so on