开发者

Ruby: Mock hash setter with class method?

Because of the way some piece of the application work (such as some pieces of Liquid not having access to instance variables) I want users to be able to access and set configuration variables in and out of开发者_如何转开发 the instance so right now I do:

module My_Module
  class My_Class
    attr_accessor :config

    def self.config
      @@config if @@config
    end

    def initialize(config)
      config[:root] = config[:root].rchomp('/')

      @@config = @config = {
        cache: 'flat',
        store: 'flat',
        plugins: 'plugins',
        pages: 'pages',
        posts: 'posts',
        static: 'static',
        templates: 'templates',
        destination: 'public' }

      @@config.deep_merge(config)
    end
  end
end

However that only gives them read only access, I was wondering if there was a way to mock hash My_Module::My_Class.config[:symbol] = value or if I should just make the setter have two attributes.


Why not just use a hash?

class MyClass
  def self.config
    @@config ||= {}
  end
end

That seems like it would achieve what you want

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜