I am not sure that this way is simple. I usually use for gem configuration this code
require 'active_support/configurable'
module GemModule
include ActiveSupport::Configurable
class << self
def setup
yield config
end
end
end
And then you can write
GemModule.setup do |config|
config.option1 = "value1"
config.option2 = 2
end
And
GemModule.config.option1
# => "value1"
Moreover, you can define method_missing in you module and delegate unknown methods to self.config. Then you will be able to write
GemModule.option1
# => "value1"
That works, but it has the following disadvantages over the method I described:
1. If you use ActiveSupport::Configurable you are tied to using activesupport/Rails. Many times you want to write the Gem so that it isn't Rails-specific and want to limit dependencies.
2. I know Rails uses the "pass the config into the block thing", but it isn't as DRY as not typing "config." before each parameter.
3. By defining the attr_accessors, you limit the chance that a user will mistype/misspell a variable.
4. The way you described allows the user to define config parameters with procs and lambdas, but the way I describe allows the user to define "config parameters" as procs, lambdas, and methods, i.e. the user can actually just define a variable as:
def some_variable_name
# some really involved implementation here, even refactored into various methods, etc. in the block.
end
See more at:
http://stufftohelpyouout.blogspot.com/2012/09/forget-struct-and-openstruct-flexible.htmlHope that helps.