class CushionDefaults::Configuration
Effectively a singleton class, @Configuration@ keeps track of various configuration options for @CushionDefaults@.
In addition, it also keeps track of certain state data for @CushionDefaults@.
For configuration options, see the attribute writers.
Attributes
@return [boolean] true if CushionDefaults
should automatically check whether a YAML file exists for each class that
includes it, false otherwise. Def. true.
@see ClassMethods.defaults_from_yaml
defauls_from_yaml
@return [boolean] if true, all calls to {ClassMethods#cushion_reader}, implicit or explicit, will also call
{ClassMethods#bang_reader}.
@return [boolean] if true, cushion_writers will also not record blank strings. Has no effect if
{#ignore_attempts_to_set_nil} is false. Def. true.
@example Ignored Call to cushion_writer When blank_str_is_nil
== false
obj.var = '' # has no effect obj.var # 'default value'
Has no effect. @deprecated
@return [boolean] if true, calls to cushion_writer methods will not set the instance variable if the value passed in
is nil (thus allowing cushion_reader to still return the default value, rather than nil). Def. true.
@example Ignored Call to cushion_writer When ignore_attempts_to_set_nil
== true
obj.var = nil # has no effect obj.var # 'default value'
@see blank_str_is_nil
@return [Logger] returns the Logger object to which CushionDefaults
sends log entries.
@return [boolean] if true, CushionDefaults
makes log entries. Def. true.
@return [boolean] true if readers should automatically be added, false otherwise. Def. false.
@return [boolean] true if writers should automatially be added, false otherwise. Def. false.
@return [boolean] if true, cushion_writers will issue a warning when you attempt to set an instance variable to
nil or '' (as applicable). Has no effect if {#ignore_attempts_to_set_nil} == false. Def. false.
@return [boolean] if true, CushionDefaults
will complain (warning level) when it cannot find a YAML configuration
file for a class. If false, it only posts this message at the debug level. Def. false.
@return [string] the location, relative to the directory of the first file to include CushionDefaults
, where YAML
config files are looked for by default. Def. 'config/cushion_defaults/'
@return [string] the full path to the directory where YAML config files are looked for. If specified, this overrides
{#yaml_source_folder}. Def. nil.
Public Class Methods
Initializes with the values specified in {#defaults!}
# File lib/cushion_defaults/configuration.rb, line 105 def initialize defaults! @we_have_a_pushy = false end
Public Instance Methods
Sets or resets configuration object to default configuration settings.
# File lib/cushion_defaults/configuration.rb, line 128 def defaults! self.update_readers = false self.update_writers = false self.auto_load_from_yaml = true self.yaml_source_folder = 'config/cushion_defaults/' self.yaml_source_full_path = nil self.bang_things_up = true self.record_in_log = true self.logger = Logger.new $stdout self.log_lvl = Logger::WARN self.whiny_yaml = false self.whiny_ignores = false self.ignore_attempts_to_set_nil = true self.blank_str_is_nil = true end
Update configuration options with those values contained within @loaded_config@.
@param loaded_config [Hash] hash of config options and settings
# File lib/cushion_defaults/configuration.rb, line 113 def from_hash(loaded_config) log_str = 'Loading configuration options from hash...' loaded_config.each do |key, val| log_str << "\n\t\t#{key}: #{val}" # We need to be able to use some of the checks and responses in the custom setter methods writer_sym = "#{key}=".to_sym send writer_sym, val if respond_to? writer_sym #instance_variable_set("@#{key.to_sym}", val) if instance_variable_defined? "@#{key.to_sym}" end CushionDefaults.log(log_str, :info) end
@!attribute [w] log_lvl Set the level of logging. Alias of logger.level=. 0: debug, 1: info, 2: warn, 3: error, 4: fatal, 5: unknown
# File lib/cushion_defaults/configuration.rb, line 89 def log_lvl=(lvl) if @logger @logger.level = lvl end end
@!attribute [w] logger Sets @logger to either a different Logger or another object that implements roughly the same interface. Note
that only minimal checking is done to ensure the interface is implemented, and passing in an object with an incomplete implementation of the interface will cause errors. If you want to disable logging, you should instead set {#record_in_log} to false.
@see record_in_log
# File lib/cushion_defaults/configuration.rb, line 73 def logger=(new_logger) if new_logger.nil? self.record_in_log = false @logger = nil else if new_logger.respond_to? :info @logger = new_logger assign_formatter_to_logger else CushionDefaults.log("config.logger not set to #{new_logger}, as it does not appear to implement standard logging methods.", :error) end end end
Opposite of {#we_have_a_pushy?} @return [boolean] true if no pushies have been set over the life of the program @api private
# File lib/cushion_defaults/configuration.rb, line 188 def no_pushies? # Note that if you add a pushy, and then remove it, this will still return false. Basically, the method returns # whether there was, at any point in time, a pushy default. # # The whole handling of pushies can and will be improved in the future. !@we_have_a_pushy end
Sets configuration object to most common testing (and development) options.
# File lib/cushion_defaults/configuration.rb, line 145 def test_settings! defaults! self.whiny_yaml = true self.whiny_ignores = true self.log_lvl = Logger::DEBUG end
CamelCase to underscores @param s [String] CamelCase @return [String] underscored version of s
# File lib/cushion_defaults/configuration.rb, line 157 def underscore(s) s.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end
Declares that a pushy default has been set. @api private
# File lib/cushion_defaults/configuration.rb, line 198 def we_have_a_pushy! @we_have_a_pushy = true end
@!attribute [r] we_have_a_pushy Denotes whether (over the life of the program) any defaults have been marked pushy. In the future, this may instead denote whether any defaults are currently marked as pushy. @return [boolean] true if a pushy was set at any time @api private
# File lib/cushion_defaults/configuration.rb, line 181 def we_have_a_pushy? @we_have_a_pushy end
Expected YAML location for a class. @param klass [Class] class in question. @return [String] expected path to the YAML file
# File lib/cushion_defaults/configuration.rb, line 168 def yaml_file_for(klass) "#{CushionDefaults::configuration.yaml_source_full_path}#{underscore(klass.to_s)}.yaml}" end
@!attribute [r] yaml_source_full_path
Returns or computes the folder where class-specific yaml files are expected to reside. @return [String] path to yaml config files.
# File lib/cushion_defaults/configuration.rb, line 98 def yaml_source_full_path @yaml_source_full_path || "#{CushionDefaults::CALLING_PATH}#{yaml_source_folder}" end
Protected Instance Methods
Specifies the formatter for logger. Will not be called if a logger is assigned with @should_assign_formatter@ set to false.
# File lib/cushion_defaults/configuration.rb, line 212 def assign_formatter_to_logger logger.formatter = proc do |severity, _time, progname, msg| "\nCUSHIONDEFAULTS: #{severity}\n\t\##{progname if progname}\n\t#{msg}\n" end end