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
-or- 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
# these are the root_enable default settings
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
-
registry = SknRegistry instance
-
logger = Assigned Logger instance
-
romDB = var for Rom-DB if used or Any Platform Database
-
metadata = platform metadata container
-
userdata = user area
-
metrics = platform metrics container
#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(*config_attrs, **root_options) ⇒ Object
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/skn_utils/configurable.rb', line 75 def self.with(*config_attrs, **) _not_provided = Object.new = .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 config_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 *config_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 # Enable Rails<Like>.env and Rails.logger like feature: # - MyClass.env.production? or MyClass.logger or MyClass.root def registry @__registry ||= ::SknRegistry.new end def registry=(obj_instance) @__registry = obj_instance end 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 # Any Platform Database def romDB @__db ||= nil end def romDB(obj) @__db = obj end # Maybe Platform Metadata def @__metadata ||= nil end def (obj) @__metadata = obj end # Userdata container for any use def userdata @__userdata ||= nil end def userdata=(obj) @__userdata = obj end # Metrics container for any use def metrics @__metrics ||= nil end def metrics=(obj) @__metrics = 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 |