-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the new configuration class for handling configuration values. …
…Includes passing specs. Dependent classes need to be updated, including a new defined bootup process.
- Loading branch information
Showing
7 changed files
with
726 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
module Halcyon | ||
|
||
# Application configuration map. | ||
# | ||
class Config | ||
|
||
attr_accessor :config | ||
|
||
autoload :Helpers, 'halcyon/config/helpers' | ||
autoload :Paths, 'halcyon/config/paths' | ||
autoload :File, 'halcyon/config/file' | ||
|
||
# Creates an empty configuration hash (Mash) and sets up the configuration | ||
# to whatever the settings are provided, merging over the defaults. | ||
# | ||
# Examples: | ||
# | ||
# Halcyon::Config.new(:environment => :development) | ||
# | ||
# OR | ||
# | ||
# Halcyon::Config.new(:allow_from => :all) | ||
# | ||
# OR | ||
# | ||
# Halcyon::Config.new | ||
# | ||
# OR | ||
# | ||
# Halcyon::Config.new do |c| | ||
# c[:foo] = true | ||
# end | ||
# | ||
def initialize(config={}, &block) | ||
env = config.delete(:environment) | ||
self.config = Mash.new | ||
self.setup(self.defaults(env).merge(config)) | ||
self.use(&block) if block_given? | ||
end | ||
|
||
# Sets the configuration up with the values given. | ||
# | ||
def configure(config={}) | ||
config.each do |(key, val)| | ||
self.config[key] = val | ||
end | ||
end | ||
|
||
# Sets up the configuration by storing the settings provided (via param or | ||
# via block). | ||
# | ||
# Usage: | ||
# | ||
# Halcyon.config.setup do |c| | ||
# c[:foo] = true | ||
# end | ||
# | ||
# or | ||
# | ||
# Halcyon.config.setup(:foo => true) | ||
# | ||
def setup(config={}) | ||
if block_given? | ||
yield(self.config.dup) | ||
end | ||
# merge new settings | ||
self.configure(config) | ||
end | ||
|
||
# Yields and returns the configuration. | ||
# | ||
# Examples: | ||
# | ||
# Halcyon.config.use do |c| | ||
# c[:foo] = true | ||
# end | ||
# | ||
def use | ||
if block_given? | ||
yield self.config | ||
end | ||
self.config | ||
end | ||
|
||
# Allows retrieval of single key config values and setting single config | ||
# values. | ||
# | ||
# Examples: | ||
# | ||
# Halcyon.config.app #=> 'AppName' | ||
# Halcyon.config[:app] #=> 'AppName' | ||
# | ||
def method_missing(method, *args) | ||
if method.to_s[-1,1] == '=' | ||
self.put(method.to_s.tr('=',''), *args) | ||
else | ||
self.get(method) | ||
end | ||
end | ||
|
||
# Get the configuration value associated with the key. | ||
# | ||
# Examples: | ||
# | ||
# Halcyon.config.get(:app) #=> 'AppName' | ||
# | ||
def get(key) | ||
self.config[key] | ||
end | ||
|
||
# Put the configuration value associated with the key or setup with a hash. | ||
# | ||
# Examples: | ||
# | ||
# Halcyon.config.put(:app, 'AppName') | ||
# | ||
# OR | ||
# | ||
# Halcyon.config.put(:app => 'AppName') | ||
# | ||
def put(key_or_config_hash, value = nil) | ||
if value.nil? and key_or_config_hash.is_a?(Hash) | ||
self.configure(key_or_config_hash) | ||
else | ||
self.config[key_or_config_hash] = value | ||
end | ||
end | ||
|
||
# Removes the configuration value from the hash. | ||
# | ||
# Examples: | ||
# | ||
# Halcyon.config.delete(:app) #=> 'AppName' | ||
# | ||
def delete(key) | ||
self.config.delete(key) | ||
end | ||
|
||
# Alias for the <tt>get</tt> method. | ||
# | ||
# Examples: | ||
# | ||
# Halcyon.config[:foo] #=> true | ||
# | ||
def [](key) | ||
self.get(key) | ||
end | ||
|
||
# Alias for the <tt>put</tt> method. (Restricted to the key/value pair.) | ||
# | ||
# Examples: | ||
# | ||
# Halcyon.config[:foo] = true | ||
# | ||
def []=(key, value) | ||
self.put(key, value) | ||
end | ||
|
||
# Returns the configuration rendered as YAML. | ||
# | ||
def to_yaml | ||
require 'yaml' | ||
self.config.to_hash.to_yaml | ||
end | ||
|
||
# Returns the configuration as a hash. | ||
# | ||
def to_hash | ||
self.config.to_hash | ||
end | ||
|
||
# Default configuration values. | ||
# | ||
# Defaults to the configuration for <tt>:development</tt>. | ||
# | ||
def defaults(env = nil) | ||
base = { | ||
:allow_from => 'all', | ||
:logging => { | ||
:type => 'Logger', | ||
:level => 'debug' | ||
}, | ||
:root => nil, | ||
:app => nil, | ||
:environment => nil, | ||
:paths => Paths.new | ||
} | ||
case (env || :development) | ||
when :development | ||
base.merge({ | ||
:environment => :development | ||
}) | ||
when :test | ||
base.merge({ | ||
:environment => :test, | ||
:logging => { | ||
:type => 'Logger', | ||
:level => 'warn', | ||
:file => 'log/test.log' | ||
} | ||
}) | ||
when :console | ||
base.merge({ | ||
:environment => :console | ||
}) | ||
when :production | ||
base.merge({ | ||
:environment => :production, | ||
:logging => { | ||
:type => 'Logger', | ||
:level => 'warn', | ||
:file => 'log/production.log' | ||
} | ||
}) | ||
end | ||
end | ||
|
||
def inspect | ||
attrs = "" | ||
self.config.keys.each {|key| attrs << " #{key}=#{self.config[key].inspect}"} | ||
"#<Halcyon::Config#{attrs}>" | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
module Halcyon | ||
class Config | ||
|
||
# Class to assist with loading configuration from a file. | ||
# | ||
# Examples: | ||
# | ||
# Halcyon::Config::File.new(file_name_or_path).to_hash #=> {...} | ||
# | ||
class File | ||
|
||
attr_accessor :path | ||
attr_accessor :content | ||
|
||
# Creates a profile with the default paths. | ||
# | ||
def initialize(file) | ||
if ::File.exist?(file) | ||
self.path = file | ||
elsif ::File.exist?(Halcyon.paths.for(:config)/file) | ||
self.path = Halcyon.paths.for(:config)/file | ||
else | ||
raise ArgumentError.new("Could not find #{self.path} (it does not exist).") | ||
end | ||
self.content = ::File.read(self.path) | ||
end | ||
|
||
# Returns the loaded configuration file's contents parsed by the | ||
# marshal format loader (defaulting to YAML, also providing JSON). | ||
# | ||
# Examples: | ||
# | ||
# p = Halcyon.paths.for(:config)/'config.yml' | ||
# c = Halcyon::Config::File.new(p) | ||
# c.to_hash #=> the contents of the config file parsed as YAML | ||
# c.to_hash(:from_json) #=> same as above only parsed as JSON | ||
# # parsing errors will happen if you try to use the wrong marshal | ||
# # load method | ||
# | ||
def to_hash(from = :from_yaml) | ||
case from | ||
when :from_yaml | ||
require 'yaml' | ||
YAML.load(self.content) | ||
when :from_json | ||
JSON.parse(self.content) | ||
end | ||
end | ||
|
||
def inspect | ||
"#<Halcyon::Config::File #{self.path}>" | ||
end | ||
|
||
class << self | ||
|
||
# Provides a convenient way to load the configuration and return the | ||
# appropriate hash contents. | ||
# | ||
def load(path) | ||
file = File.new(path) | ||
case path | ||
when /\.(yaml|yml)/ | ||
file.to_hash | ||
when /\.(json)/ | ||
file.to_hash(:from_json) | ||
end | ||
end | ||
|
||
# Loads the configuration file and parses it's contents as YAML. | ||
# This is a shortcut method. | ||
# | ||
def load_from_yaml(path) | ||
self.new(path).to_hash | ||
end | ||
|
||
# Loads the configuration file and parses it's contents as JSON. | ||
# This is a shortcut method. | ||
# | ||
def load_from_json(path) | ||
self.new(path).to_hash(:from_json) | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end |
Oops, something went wrong.