Module: SknUtils::Configurable
- Defined in:
- lib/skn_utils/configurable.rb
Overview
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) # or {root_enable: false})
# ... default=true for root|env|logger
end
Inside Initializer
MyApp.configure do
app_id "my_app"
title "My App"
{ "#{app_id}_session" }
end
During Definition
class MyApp
include SknUtils::Configurable.with(:app_id, :title, :cookie_name, {root_enable: true})
# ...
configure do
app_id "my_app"
title "My App"
{ "#{app_id}_session" }
end
self.logger = Logger.new
self.env = ENV.fetch('RACK_ENV', 'development')
self.root = Dir.pwd
end
Usage:
MyApp.config.app_id # ==> “my_app” MyApp.logger # ==> <Logger.class> MyApp.env.test? # ==> true
############### Syntax ############### Main Class Attrs
-
root = application rood directory as Pathname
-
env = string value from RACK_ENV
-
logger = Assiigned Logger instance
#with(*user_attrs, enable_root: true|false) - defaults to enable of Main Class Attrs ## User-Defined Attrs MyThing.with(:name1, :name2, …)
##
Class Method Summary collapse
Class Method Details
.with(*attrs, **options) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/skn_utils/configurable.rb', line 67 def self.with(*attrs, **) _not_provided = Object.new _app_main = .empty? || .values.any?{|v| v == true} # Define the config class/module methods config_class = Class.new do # add hash notation define_method :[] do |attr| instance_variable_get("@#{attr}") end define_method :[]= do |attr, val| instance_variable_set("@#{attr}", val) end 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 if _app_main # Enable Rails<Like>.env and Rails.logger like feature: # - MyClass.env.production? or MyClass.logger or MyClass.root def env @__env ||= ::SknUtils::EnvStringHandler.new( ENV.fetch('RACK_ENV', 'development') ) end def env=(str) @__env = ::SknUtils::EnvStringHandler.new( str || ENV.fetch('RACK_ENV', 'development') ) end def root @__root ||= ::SknUtils::EnvStringHandler.new( Dir.pwd ) end def root=(path) @__root = ::SknUtils::EnvStringHandler.new( path || Dir.pwd ) end def logger @__logger ||= 'No Logger Assigned.' end def logger=(obj) @__logger = obj end 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 |