configatron shorthand for namespaces
-
01-03-2021 - |
Question
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?
Solution
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow