m33lky
m33lky

Reputation: 7275

configatron shorthand for namespaces

Is there a way to avoid typing the namespace each time when using the configatron gem? Say, you have

  configatron.email.pop.address = 1
  configatron.email.pop.port = 2

Can I configure port and address by somehow typing configatron.email.pop only once?

Upvotes: 1

Views: 263

Answers (1)

AboutRuby
AboutRuby

Reputation: 8116

One easy thing you can do is this.

configatron.email.pop.tap do|pop|
  pop.address = 'localhost'
  pop.port = 22
end

Or even this.

pop = configatron.email.pop
pop.address = 'localhost'
pop.port = 22

You could try adding singleton methods to the configatron object too.

class << configatron
  def pop; email.pop; end
end

configatron.pop.address = 'localhost'
configatron.pop.port = 22

Or even this.

class << configatron
  def pop_address; email.pop.address; end
  def pop_address=(addr); email.pop.address = addr; end
end

configatron.pop_address = 'address'

But this may end up breaking things (I'm assuming configatron works on method_missing). And it doesn't save much typing, but it can let you make some handy shortcuts to things hidden several namespaces deep.

Maybe you should just stick it out with the verbosity here though.

Upvotes: 1

Related Questions