Class: Waw::Config
Overview
Implements waw configuration utility
Defined Under Namespace
Classes: DSL
Instance Method Summary collapse
-
#config_error(name, message) ⇒ Object
Raises a configuration error.
-
#ensure(name, default) ⇒ Object
Returns the configuration property under name, or returns default if the config does not know that name.
-
#initialize(app, merge_default = true) ⇒ Config
constructor
Initialize a configuration instance.
-
#install_configuration_property(name, value) ⇒ Object
Installs a configuration parameter.
-
#knows?(name) ⇒ Boolean
Checks if a configuration property is known.
-
#merge(config_str) ⇒ Object
Merges the configuration with a given configuration string.
-
#merge_file(config_file) ⇒ Object
Merges the configuration with a given configuration file.
Constructor Details
#initialize(app, merge_default = true) ⇒ Config
Initialize a configuration instance. If merge_default is set to true, the default configuration file is automatically loaded.
30 31 32 33 34 |
# File 'lib/waw/config.rb', line 30 def initialize(app, merge_default = true) raise ArgumentError, "Waw application required as first argument" unless app.respond_to?(:root_folder) @wawapp = app self.merge_file(File.join(File.dirname(__FILE__), 'default_config.cfg')) if merge_default end |
Instance Method Details
#config_error(name, message) ⇒ Object
Raises a configuration error
37 38 39 |
# File 'lib/waw/config.rb', line 37 def config_error(name, ) raise ConfigurationError, end |
#ensure(name, default) ⇒ Object
Returns the configuration property under name, or returns default if the config does not know that name
48 49 50 |
# File 'lib/waw/config.rb', line 48 def ensure(name, default) knows?(name) ? self.send(name) : default end |
#install_configuration_property(name, value) ⇒ Object
Installs a configuration parameter
53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 |
# File 'lib/waw/config.rb', line 53 def install_configuration_property(name, value) case name when :load_path value = [value] unless Array===value value = value.collect{|val| File.(File.join(@wawapp.root_folder, val))} $LOAD_PATH.unshift(*value) when :requires value = [value] unless Array===value value.each{|f| require(f)} when :log_dir config_error(name, "log_dir is expected to be a string") unless String===value # config_error(name, "Unable to access log_dir #{value}") unless File.exists?(value) and # File.directory?(value) and # File.writable?(value) when :log_file config_error(name, "log_file is expected to be a string") unless String===value when :log_level config_error(name, "Bad log_level #{value}") unless value>=0 and value<=5 when :log_frequency config_error(name, "Bad log_frequency #{value}") unless %w{daily weekly monthly}.include?(value) when :web_domain, :web_base config_error(name, "web_domain is expected to be a string") unless String===value when :rack_session config_error(name, "rack_session is expected to be true or false") unless true==value or false==value when :rack_session_expire config_error(name, "rack_session_expire is expected to be an Integer") unless Integer===value end instance_eval "@#{name} = value" instance_eval <<-EOF def #{name}(force_array = false) value = @#{name} if Proc===value self.instance_eval &value else force_array ? (Array===value ? value : [value]) : value end end EOF end |
#knows?(name) ⇒ Boolean
Checks if a configuration property is known
42 43 44 |
# File 'lib/waw/config.rb', line 42 def knows?(name) self.respond_to?(name.to_s.to_sym) end |
#merge(config_str) ⇒ Object
Merges the configuration with a given configuration string
94 95 96 97 98 99 100 101 |
# File 'lib/waw/config.rb', line 94 def merge(config_str) begin DSL.new(self).instance_eval config_str rescue ConfigurationError => ex raise ex end self end |
#merge_file(config_file) ⇒ Object
Merges the configuration with a given configuration file
104 105 106 107 |
# File 'lib/waw/config.rb', line 104 def merge_file(config_file) raise ArgumentError, "Config file does not exists" unless File.exists?(config_file) merge File.read(config_file) end |