문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top