Module: SknUtils::Configurable
- Defined in:
- lib/skn_utils/configurable.rb
Overview
In the end, we have got ourselves a pretty neat module for making an arbitrary class configurable and then specifying those configuration values using a clean and simple DSL that also lets us reference one configuration attribute from another:
Inside Target component
class MyApp
include SknUtils::Configurable.with(:app_id, :title, :cookie_name)
# ...
end
Inside Initializer
MyApp.configure do
app_id "my_app"
title "My App"
{ "#{app_id}_session" }
end
Usage:
MyApp.config.app_id # ==> “my_app”
Here is the final version of the module that implements our DSL—a total of 36 lines of code:
Class Method Summary collapse
Class Method Details
.with(*attrs) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/skn_utils/configurable.rb', line 39 def self.with(*attrs) not_provided = Object.new # Define the class/module methods config_class = Class.new do attrs.each do |attr| define_method attr do |value = not_provided, &block| if value === not_provided && block.nil? result = instance_variable_get("@#{attr}") result.is_a?(Proc) ? instance_eval(&result) : result else instance_variable_set("@#{attr}", block || value) end end end attr_writer *attrs end # Define the runtime access methods class_methods = Module.new do define_method :config do @config ||= config_class.new end def configure(&block) config.instance_eval(&block) end end # Apply the custom configuration Module.new do singleton_class.send :define_method, :included do |host_class| host_class.extend class_methods end end end |