module Thoth::Config

Public Class Methods

<<(config) click to toggle source

Adds the specified config Hash to Thoth's config lookup chain. Any configuration values in config will be used as defaults unless they're specified earlier in the lookup chain (i.e. in Thoth's config file).

# File lib/thoth/config.rb, line 36
def <<(config)
  raise ArgumentError, "config must be a Hash" unless config.is_a?(Hash)

  (@lookup ||= []) << config
  cache_config

  @lookup
end
load(file) click to toggle source

Loads the specified configuration file.

# File lib/thoth/config.rb, line 46
def load(file)
  raise Thoth::Error, "Config file not found: #{file}" unless File.file?(file)

  @live = {
    'db' => "sqlite:///#{HOME_DIR}/db/live.db",

    'site' => {
      'name' => "New Rethoth Blog",
      'desc' => "Rethoth is awesome.",
      'url'  => "http://localhost:7000/",

      'core_js'  => [
        'http://yui.yahooapis.com/2.8.0/build/yahoo-dom-event/yahoo-dom-event.js',
        '/js/thoth.js'
      ],

      'css' => [],
      'js'  => [],

      'enable_comments' => true,
      'enable_sitemap'  => true,

      'gravatar' => {
        'enabled' => true,
        'default' => "identicon",
        'rating'  => "g",
        'size'    => 32
      }
    },

    'admin' => {
      'name'  => "John Doe",
      'email' => "",
      'user'  => "thoth",
      'pass'  => "thoth",
      'seed'  => "6d552ac197a862b82b85868d6c245feb"
    },

    'plugins' => [],

    'media' => File.join(HOME_DIR, 'media'),

    'server' => {
      'adapter'       => 'webrick',
      'address'       => '0.0.0.0',
      'port'          => 7000,
      'enable_cache'  => true,
      'enable_minify' => true,
      'error_log'     => File.join(HOME_DIR, 'log', 'error.log'),

      'memcache' => {
        'enabled' => false,
        'servers' => ['localhost:11211:1']
      }
    },

    'timestamp' => {
      'long'  => "%A %B %d, %Y @ %I:%M %p (%Z)",
      'short' => "%Y-%m-%d %I:%M"
    }
  }

  @dev = {
    'db' => "sqlite:///#{HOME_DIR}/db/dev.db",

    'server' => {
      'enable_cache'  => false,
      'enable_minify' => false
    }
  }

  begin
    config = YAML.load(Erubis::Eruby.new(File.read(file)).result(binding)) || {}
  rescue => e
    raise Thoth::ConfigError, "Config error in #{file}: #{e}"
  end

  @lookup ||= if Thoth.trait[:mode] == :production
      [config['live'] || {}, @live]
  else
    [config['dev'] || {}, config['live'] || {}, @dev, @live]
  end

  cache_config
end
method_missing(name) click to toggle source
# File lib/thoth/config.rb, line 132
def method_missing(name)
  (@cached || {})[name.to_s] || {}
end

Private Class Methods

cache_config() click to toggle source

Merges configs such that those earlier in the lookup chain override those later in the chain.

# File lib/thoth/config.rb, line 140
def cache_config
  @cached = {}

  @lookup.reverse.each do |c|
    c.each {|k, v| @cached[k] = config_merge(@cached[k] || {}, v) }
  end
end
config_merge(master, value) click to toggle source
# File lib/thoth/config.rb, line 148
def config_merge(master, value)
  if value.is_a?(Hash)
    value.each {|k, v| master[k] = config_merge(master[k] || {}, v) }
    return master
  end

  value
end