class Webgen::Configuration

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!

Attributes

options[R]

Contains all the defined configuration options.

Public Class Methods

new() click to toggle source

Create a new Configuration object.

   # File lib/webgen/configuration.rb
54 def initialize
55   @options = {}
56   @values = {}
57 end

Public Instance Methods

[](name) click to toggle source

Return the value for the configuration option name.

    # File lib/webgen/configuration.rb
104 def [](name)
105   if @options.has_key?(name)
106     if frozen?
107       @values.has_key?(name) ? @values[name] : @options[name].dupped_default
108     else
109       @values[name] = @options[name].dupped_default unless @values.has_key?(name)
110       @values[name]
111     end
112   else
113     raise Error, "Configuration option '#{name}' does not exist"
114   end
115 end
[]=(name, value) click to toggle source

Use value as value for the configuration option name.

    # File lib/webgen/configuration.rb
118 def []=(name, value)
119   if @options.has_key?(name)
120     begin
121       @values[name] = (@options[name].validator ? @options[name].validator.call(value) : value)
122     rescue
123       raise Error, "Problem setting configuration option '#{name}': #{$!.message}", $!.backtrace
124     end
125   else
126     raise Error, "Configuration option '#{name}' does not exist"
127   end
128 end
define_option(name, default, &validator) click to toggle source

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.

   # File lib/webgen/configuration.rb
87 def define_option(name, default, &validator)
88   if @options.has_key?(name)
89     raise ArgumentError, "Configuration option '#{name}' has already be defined"
90   else
91     @options[name] = Option.new
92     @options[name].default = default.freeze
93     @options[name].validator = validator.freeze
94     @options[name].freeze
95   end
96 end
load_from_file(filename) click to toggle source

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.

    # File lib/webgen/configuration.rb
170 def load_from_file(filename)
171   data = if String === filename || filename.respond_to?(:read)
172            begin
173              YAML::load(String === filename ? File.read(filename) : filename.read) || {}
174            rescue RuntimeError, ArgumentError, SyntaxError, YAML::SyntaxError => e
175              raise Error, "Problem parsing configuration data (it needs to contain a YAML hash): #{e.message}", e.backtrace
176            end
177          else
178            raise ArgumentError, "Need a String or IO object, not a #{filename.class}"
179          end
180   raise Error, 'Structure of configuration file is invalid, it has to be a Hash' unless data.kind_of?(Hash)
181   set_values(data)
182 end
option?(name) click to toggle source

Return true if the given option exists.

    # File lib/webgen/configuration.rb
 99 def option?(name)
100   @options.has_key?(name)
101 end
set_values(values) click to toggle source

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.

    # File lib/webgen/configuration.rb
145 def set_values(values)
146   unknown_options = []
147   process = proc do |name, value|
148     if @options.has_key?(name)
149       self[name] = value
150     elsif value.kind_of?(Hash)
151       value.each {|k,v| process.call("#{name}.#{k}", v)}
152     else
153       unknown_options << name
154     end
155   end
156   values.each(&process)
157   unknown_options
158 end