Hacker News new | ask | show | jobs
by guptaneil 4480 days ago
This is a great approach for setting config variables. I also like to enable the user to add custom functionality to the gem by accepting a block that is called with any relevant variables for the user to access.

For example, in Exceptionally[1], the user can add an initializer that looks like:

    Exceptionally::Handler.before_render do |message, status, error, params|
      # put any code here
    end
In the gem, this translates to:

    def self.before_render(&block)
      &&callback = Proc.new(&block)
    end
The gem can then call the user's block and give it access to the same variables the rest of the gem does. This approach is a lot simpler than setting up an entire Middleware stack, as suggested in the blog, and makes more sense for simpler gems.

1: https://github.com/neilgupta/exceptionally