Class: Webgen::Configuration
- Inherits:
-
Object
- Object
- Webgen::Configuration
- Defined in:
- lib/webgen/configuration.rb
Overview
Stores the configuration for a webgen website.
Configuration options can be created by using the define_option method:
config.define_option "my.new.option", 'default value'
and later accessed or set using the accessor methods #[] and #[]=. A validation block can also be specified when defining an option. This validation block is called when a new value should be set and it should return the (possibly changed) value to be set:
config.define_option "my.new.option", 'default value' do |val|
raise "Option must be a string" unless val.kind_of?(String)
val.upcase
end
Note: When a Configuration object is dumped (via Marshal), the option validator procs are not dumped and can therefore not be restored!
Defined Under Namespace
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Contains all the defined configuration options.
Instance Method Summary collapse
-
#==(other) ⇒ Object
:nodoc:.
-
#[](name) ⇒ Object
Return the value for the configuration option
name
. -
#[]=(name, value) ⇒ Object
Use
value
as value for the configuration optionname
. -
#clone ⇒ Object
:nodoc:.
-
#define_option(name, default, &validator) ⇒ Object
Define a new option
name
with a default value ofdefault
. -
#freeze ⇒ Object
:nodoc:.
-
#initialize ⇒ Configuration
constructor
Create a new Configuration object.
-
#initialize_copy(orig) ⇒ Object
:nodoc:.
-
#load_from_file(filename) ⇒ Object
Load the configuration values.
-
#option?(name) ⇒ Boolean
Return
true
if the given option exists. -
#set_values(values) ⇒ Object
Set the configuration values from the Hash
values
.
Constructor Details
#initialize ⇒ Configuration
Create a new Configuration object.
55 56 57 58 |
# File 'lib/webgen/configuration.rb', line 55 def initialize @options = {} @values = {} end |
Instance Attribute Details
#options ⇒ Object (readonly)
Contains all the defined configuration options.
52 53 54 |
# File 'lib/webgen/configuration.rb', line 52 def @options end |
Instance Method Details
#==(other) ⇒ Object
:nodoc:
80 81 82 |
# File 'lib/webgen/configuration.rb', line 80 def ==(other) #:nodoc: @options == other. && @values == other.instance_variable_get(:@values) end |
#[](name) ⇒ Object
Return the value for the configuration option name
.
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/webgen/configuration.rb', line 105 def [](name) if @options.has_key?(name) if frozen? @values.has_key?(name) ? @values[name] : @options[name].dupped_default else @values[name] = @options[name].dupped_default unless @values.has_key?(name) @values[name] end else raise Error, "Configuration option '#{name}' does not exist" end end |
#[]=(name, value) ⇒ Object
Use value
as value for the configuration option name
.
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/webgen/configuration.rb', line 119 def []=(name, value) if @options.has_key?(name) begin @values[name] = (@options[name].validator ? @options[name].validator.call(value) : value) rescue raise Error, "Problem setting configuration option '#{name}': #{$!.}", $!.backtrace end else raise Error, "Configuration option '#{name}' does not exist" end end |
#clone ⇒ Object
:nodoc:
67 68 69 70 |
# File 'lib/webgen/configuration.rb', line 67 def clone #:nodoc: super freeze if frozen? end |
#define_option(name, default, &validator) ⇒ Object
Define a new option name
with a default value of default
.
If a validation block is provided, it is called with the new value when one is set and should return a (possibly altered) value to be set.
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/webgen/configuration.rb', line 88 def define_option(name, default, &validator) if @options.has_key?(name) raise ArgumentError, "Configuration option '#{name}' has already be defined" else @options[name] = Option.new @options[name].default = default.freeze @options[name].validator = validator.freeze @options[name].freeze end end |
#freeze ⇒ Object
:nodoc:
72 73 74 75 76 77 78 |
# File 'lib/webgen/configuration.rb', line 72 def freeze #:nodoc: super @options.freeze @values.each_value {|v| v.freeze} @values.freeze self end |
#initialize_copy(orig) ⇒ Object
:nodoc:
60 61 62 63 64 65 |
# File 'lib/webgen/configuration.rb', line 60 def initialize_copy(orig) #:nodoc: super @options = orig..dup @values = {} orig.instance_eval { @values }.each {|k,v| @values[k] = v.dup rescue v} end |
#load_from_file(filename) ⇒ Object
Load the configuration values.
If filename
is a String, it is treated as the name of the configuration file from which the values should be loaded. If filename
responds to #read, it is treated as an IO object from which the values should be loaded.
The configuration needs to be in YAML format. More specifically, it needs to contain a YAML hash which is further processed by #set_values.
Returns an array with all unknown configuration options.
171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/webgen/configuration.rb', line 171 def load_from_file(filename) data = if String === filename || filename.respond_to?(:read) begin YAML::load(String === filename ? File.read(filename) : filename.read) || {} rescue RuntimeError, ArgumentError, SyntaxError, YAML::SyntaxError => e raise Error, "Problem parsing configuration data (it needs to contain a YAML hash): #{e.}", e.backtrace end else raise ArgumentError, "Need a String or IO object, not a #{filename.class}" end raise Error, 'Structure of configuration file is invalid, it has to be a Hash' unless data.kind_of?(Hash) set_values(data) end |
#option?(name) ⇒ Boolean
Return true
if the given option exists.
100 101 102 |
# File 'lib/webgen/configuration.rb', line 100 def option?(name) @options.has_key?(name) end |
#set_values(values) ⇒ Object
Set the configuration values from the Hash values
.
The hash can either contain full configuration option names or namespaced option names, ie. in YAML format:
my.option: value
website:
lang: en
url: my_url
The above hash will set the option ‘my.option’ to value
, ‘website.lang’ to en
and ‘website.url’ to my_url
.
Returns an array with all unknown configuration options.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/webgen/configuration.rb', line 146 def set_values(values) = [] process = proc do |name, value| if @options.has_key?(name) self[name] = value elsif value.kind_of?(Hash) value.each {|k,v| process.call("#{name}.#{k}", v)} else << name end end values.each(&process) end |