Class: Waw::Config

Inherits:
Object show all
Defined in:
lib/waw/config.rb

Overview

Implements waw configuration utility

Defined Under Namespace

Classes: DSL

Instance Method Summary collapse

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.

Raises:

  • (ArgumentError)


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

Raises:



37
38
39
# File 'lib/waw/config.rb', line 37

def config_error(name, message)
  raise ConfigurationError, message
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.expand_path(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

Returns:



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

Raises:

  • (ArgumentError)


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