From 5c02cf6dd0feb4fa86a497b1523371c53c5552ae Mon Sep 17 00:00:00 2001 From: Loren Segal Date: Wed, 21 Aug 2013 22:45:49 -0700 Subject: [PATCH] Revamp configuration --- app.rb | 16 +++------------- init.rb | 7 +++++++ lib/configuration.rb | 24 ++++++++++++++++++++++++ templates/default/layout/html/footer.erb | 10 +++++----- templates/default/method/html/disqus.erb | 4 ++-- templates/default/method/setup.rb | 2 +- 6 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 lib/configuration.rb diff --git a/app.rb b/app.rb index 4f9ca81..402b1a3 100644 --- a/app.rb +++ b/app.rb @@ -3,7 +3,6 @@ require 'yard' require 'sinatra' require 'json' -require 'yaml' require 'fileutils' require 'airbrake' @@ -40,15 +39,9 @@ def self.load_configuration set :whitelisted_gems, [] set :caching, false - return unless File.file?(CONFIG_FILE) - puts ">> Loading #{CONFIG_FILE}" - YAML.load_file(CONFIG_FILE).each do |key, value| + $CONFIG.each do |key, value| set key, value - the_options[key.to_sym] = value - $DISQUS = value if key == 'disqus' # HACK for DISQUS setting - $CLICKY = value if key == 'clicky' # Hack for Clicky setting - $GOOGLE_ANALYTICS = value if key == 'google_analytics' # Hack for GA settings end end @@ -100,10 +93,10 @@ def self.find_featured_yardoc(name, libdir) end def self.load_featured_adapter - raise(Errno::ENOENT) unless the_options.has_key?(:featured) + raise(Errno::ENOENT) unless $CONFIG.has_key?(:featured) opts = adapter_options opts[:options][:router] = FeaturedRouter - the_options[:featured].each do |key, value| + $CONFIG[:featured].each do |key, value| opts[:libraries][key] = case value when String if value == "gem" @@ -153,9 +146,6 @@ def self.post_all(*args, &block) args.each {|arg| post(arg, &block) } end - class << self; attr_accessor :the_options end - @the_options = {} - use Rack::Deflater use Rack::ConditionalGet use Rack::Head diff --git a/init.rb b/init.rb index d3137c5..d2dca09 100644 --- a/init.rb +++ b/init.rb @@ -5,6 +5,7 @@ require 'rubygems' require 'bundler/setup' +require 'yaml' require 'yard' require 'yard-sd' require 'yard-rails' @@ -33,3 +34,9 @@ def __p(*extra) CONFIG_FILE = __p('config', 'config.yaml', :file) REMOTE_GEMS_FILE = __p('data', 'remote_gems', :file) RECENT_SQL_FILE = __p('data', 'recent.sqlite', :file) + +require_relative 'lib/helpers' +require_relative 'lib/cache' +require_relative 'lib/configuration' + +$CONFIG = Configuration.load diff --git a/lib/configuration.rb b/lib/configuration.rb new file mode 100644 index 0000000..a5d7d60 --- /dev/null +++ b/lib/configuration.rb @@ -0,0 +1,24 @@ +class Configuration < Hash + def self.load + config = Configuration.new + + if File.file?(CONFIG_FILE) + YAML.load_file(CONFIG_FILE).each do |key, value| + config[key] = value + define_method(key) { self[key] } + end + end + + config + end + + def caching_type + if caching; :disk + elsif varnish_host; :varnish + end + end + + def method_missing(name, *args, &block) + self[name] + end +end diff --git a/templates/default/layout/html/footer.erb b/templates/default/layout/html/footer.erb index 8a29c20..b003fbe 100644 --- a/templates/default/layout/html/footer.erb +++ b/templates/default/layout/html/footer.erb @@ -4,20 +4,20 @@ <%= YARD::VERSION %> (ruby-<%= RUBY_VERSION %>). -<% if $CLICKY %> +<% if $CONFIG.clicky %> - - + + <% end %> -<% if $GOOGLE_ANALYTICS %> +<% if $CONFIG.google_analytics %> diff --git a/templates/default/method/html/disqus.erb b/templates/default/method/html/disqus.erb index 7c10cdc..551ed59 100644 --- a/templates/default/method/html/disqus.erb +++ b/templates/default/method/html/disqus.erb @@ -4,6 +4,6 @@

Comments

- - + + \ No newline at end of file diff --git a/templates/default/method/setup.rb b/templates/default/method/setup.rb index 1fa337b..955faf2 100644 --- a/templates/default/method/setup.rb +++ b/templates/default/method/setup.rb @@ -1,5 +1,5 @@ def init super - return unless defined? $DISQUS + return unless defined? $CONFIG.disqus sections.push :disqus end \ No newline at end of file