Added the Stoptime::Config module and Config model to hold the configuration

This commit is contained in:
Paul van Tilburg 2011-12-23 21:17:02 +01:00
parent c0c3d9773a
commit 71ee97c34c
2 changed files with 81 additions and 7 deletions

10
config.yaml Normal file
View file

@ -0,0 +1,10 @@
# Default configuration for Stop… Camping Time!
# The default hourly rate
#hourly_rate: 20.0
# The VAT rate
#vat_rate: 19.0
# The invoice ID format (see strftime(3) and %N for the sequence number)
#invoice_id: %Y%N

View file

@ -41,15 +41,28 @@ unless defined? PUBLIC_DIR
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
:default => "%Y-%m-%d",
:month_and_year => "%B %Y")
# The default hourly rate.
# FIXME: this should be configurable.
HourlyRate = 20.0
# The default VAT rate.
VATRate = 19
end
# = Mix-in module
#
# This module enables configuration support available for specific
# controllers or the entire application.
module StopTime::Config
# The parsed configuration (Hash).
attr_reader :config
# Override controller call handler so that the configuration is available
# for all controllers and views.
# See also: http://code.whytheluckystiff.net/camping/wiki/BeforeAndAfterOverrides
def service(*a)
# FIXME: config path should be configurable!
@config = StopTime::Models::Config.instance
super(*a)
end
end #module Photos::Config
# = The main application module
module StopTime
@ -61,11 +74,62 @@ module StopTime
StopTime::Models.create_schema
end
# Automatically mix-in the configuration support in the application.
include StopTime::Config
Signal.trap("HUP") do
$stderr.puts "I: caught signal HUP, reloading config"
Models::Config.instance.reload
end
end
# = The Stop… Camping Time! models
module StopTime::Models
# The configuration model class
#
# This class contains the configuration overlaying overridden options for
# subdirectories such that for each directory the specific configuration
# can be found.
class Config
include Singleton
# The default configuation file. (FIXME: shouldn't be hardcoded!)
ConfigFile = "./config.yaml"
# The default configuration. Note that the configuration of the root
# will be merged with this configuration.
DefaultConfig = { "invoice_id" => "%Y%N",
"hourly_rate" => 20.0,
"vat_rate" => 19.0 }
# Creates a new configuration object and loads the configuation.
def initialize
load
end
# Loads the configuration by reaiding the file file, parsing it, and
# performing a merge with descendants.
def load
cfg = nil
# Read and parse the configuration.
begin
File.open(ConfigFile, "r") { |file| cfg = YAML.load(file) }
rescue => e
$stderr.puts "E: couldn't read configuration file: #{e}"
end
# Merge the loaded config with the default config.
@config = DefaultConfig.dup.merge cfg unless cfg.nil?
end
# Reloads the configuration file.
def reload
load
end
end # class StopTime::Models::Config
# == The customer class
#
# This class represents a customer that has projects/tasks