From 71ee97c34c2737f80af5b67fc1c2d1a41c367260 Mon Sep 17 00:00:00 2001 From: Paul van Tilburg Date: Fri, 23 Dec 2011 21:17:02 +0100 Subject: [PATCH] Added the Stoptime::Config module and Config model to hold the configuration --- config.yaml | 10 +++++++ stoptime.rb | 78 ++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 config.yaml diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..3f3e4fe --- /dev/null +++ b/config.yaml @@ -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 diff --git a/stoptime.rb b/stoptime.rb index 3ed35b7..5377e72 100644 --- a/stoptime.rb +++ b/stoptime.rb @@ -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