Skip to content

Custom Namespaces

Jeff Felchner edited this page Jan 9, 2018 · 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 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:

Chamber.load(basepath:   ::Rails.root.join('config'),
              namespaces: {
                environment: -> { ::Rails.env },
                hostname:    -> { Socket.gethostname },
                prank:       -> { ::Rails.env.development? ? Time.now.strftime('%A').downcase : nil }
              }
            )

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