Skip to content

Custom Namespaces

Jeff Felchner edited this page Aug 14, 2019 · 3 revisions

All of the official plugins automatically load the environment and the hostname as a namespace, but what about if you wanted your own?

Let's say that we had a setting that we only wanted to apply on Fridays. Why? I have no idea, but it would be an amazing prank.

The setting we want to change is the port of the development server. On any given day it's 5000 but on Fridays we're going to make it 6000.

Anyone going to the app on a Friday would see that it isn't connecting. Early weekend!

How would we do this?

# settings/http.yml

development:
  http:
    # Snip...
    port:           5000
    # Snip...

test:
  http:
    # Snip...

production:
  http:
    # Snip...

friday:
  http:
    port:           6000

Then, in our app, we would load our Chamber settings like so:

module MyRailsApp
  class Application < Rails::Application
    initializer 'myapp.chamber', after: 'chamber.load', before: :load_environment_config do
      Chamber.load basepath: ::Rails.root.join('config'),
                   namespaces: {
                     environment: -> { ::Rails.env },
                     hostname:    -> { ::Socket.gethostname },
                     prank:       -> { ::Rails.env.development? ? Time.now.strftime('%A').downcase : nil }
                   }
    end
  end
end

and our prank namespace would always be loaded after everything else and therefore could override any other setting.

Note: We have the development environment check in there. We don't want to change the port in production! ;)

Clone this wiki locally