module SknUtils::Configurable

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"
cookie_name { "#{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"
  cookie_name { "#{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

with(*user_attrs, enable_root: true|false) - defaults to enable of Main Class Attrs ## User-Defined Attrs MyThing.with(:name1, :name2, …)

##

Public Class Methods

with(*config_attrs, **root_options) click to toggle source
# File lib/skn_utils/configurable.rb, line 76
def self.with(*config_attrs, **root_options)
  _not_provided = Object.new
  _root_options = root_options.empty? || root_options.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 _root_options
      # 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
        @__metadata ||= nil
      end
      def metadata=(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

Public Instance Methods

configure(&block) click to toggle source
# File lib/skn_utils/configurable.rb, line 109
def configure(&block)
  config.instance_eval(&block)
end
env() click to toggle source
# File lib/skn_utils/configurable.rb, line 121
def env
  @__env ||= ::SknUtils::EnvStringHandler.new( ENV.fetch('RACK_ENV', 'development') )
end
env=(str) click to toggle source
# File lib/skn_utils/configurable.rb, line 124
def env=(str)
  @__env = ::SknUtils::EnvStringHandler.new( str || ENV.fetch('RACK_ENV', 'development') )
end
logger() click to toggle source
# File lib/skn_utils/configurable.rb, line 133
def logger
  @__logger ||= 'No Logger Assigned.'
end
logger=(obj) click to toggle source
# File lib/skn_utils/configurable.rb, line 136
def logger=(obj)
  @__logger = obj
end
metadata() click to toggle source

Maybe Platform Metadata

# File lib/skn_utils/configurable.rb, line 149
def metadata
  @__metadata ||= nil
end
metadata=(obj) click to toggle source
# File lib/skn_utils/configurable.rb, line 152
def metadata=(obj)
  @__metadata = obj
end
metrics() click to toggle source

Metrics container for any use

# File lib/skn_utils/configurable.rb, line 165
def metrics
  @__metrics ||= nil
end
metrics=(obj) click to toggle source
# File lib/skn_utils/configurable.rb, line 168
def metrics=(obj)
  @__metrics = obj
end
registry() click to toggle source

Enable Rails<Like>.env and Rails.logger like feature:

  • MyClass.env.production? or MyClass.logger or MyClass.root

# File lib/skn_utils/configurable.rb, line 115
def registry
  @__registry ||= ::SknRegistry.new
end
registry=(obj_instance) click to toggle source
# File lib/skn_utils/configurable.rb, line 118
def registry=(obj_instance)
  @__registry = obj_instance
end
romDB() click to toggle source

Any Platform Database

# File lib/skn_utils/configurable.rb, line 141
def romDB
  @__db ||= nil
end
root() click to toggle source
# File lib/skn_utils/configurable.rb, line 127
def root
  @__root ||= ::SknUtils::EnvStringHandler.new( Dir.pwd )
end
root=(path) click to toggle source
# File lib/skn_utils/configurable.rb, line 130
def root=(path)
  @__root = ::SknUtils::EnvStringHandler.new( path || Dir.pwd )
end
userdata() click to toggle source

Userdata container for any use

# File lib/skn_utils/configurable.rb, line 157
def userdata
  @__userdata ||= nil
end
userdata=(obj) click to toggle source
# File lib/skn_utils/configurable.rb, line 160
def userdata=(obj)
  @__userdata = obj
end