Skip to content

Commit

Permalink
Refactor config out of Application
Browse files Browse the repository at this point in the history
- Use the Configuration singleton to store config values
- First config option is a application-wide logger
  • Loading branch information
titanous committed Jun 16, 2010
1 parent abe3966 commit d7be079
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 54 deletions.
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## Features
- Logging everywhere
- Polling for POP3
- Accept messages via named pipe
- Accept messages via STDIN
- Gemspec
- Reply with ActionMailer
- MTA integration
Expand All @@ -21,6 +23,4 @@
- Mailing list system (w/simple Sinatra web interface)

## Website
- Ask about using the vagrantup.com design (clean, easy, and built on GitHub
Pages)
- Overview and quick tutorial docs
2 changes: 1 addition & 1 deletion lib/mailman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

module Mailman

[:Application, :Router, :Route, :Receiver, :MessageProcessor].each do |constant|
[:Application, :Router, :Configuration, :Route, :Receiver, :MessageProcessor].each do |constant|
autoload constant, "mailman/#{constant.to_s.underscore}"
end

Expand Down
21 changes: 0 additions & 21 deletions lib/mailman/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ class Application
# @return [Router] the app's router
attr_reader :router

# @return [HashWithIndifferentAccess] a hash of config options
attr_accessor :config

# Creates a new router, and sets up any routes passed in the block.
# @param [Proc] block a block with routes
def initialize(&block)
@config = HashWithIndifferentAccess.new
@router = Mailman::Router.new
instance_eval(&block)
end
Expand All @@ -21,22 +17,5 @@ def default(&block)
@router.default_block = block
end

# Sets a config option.
# @param [Symbol] key the config key
# @param value the config value
def set(key, value)
@config[key] = value
end

# Same as calling +set :option, true+ for each of the given options.
def enable(*opts)
opts.each { |key| set(key, true) }
end

# Same as calling +set :option, false+ for each of the given options.
def disable(*opts)
opts.each { |key| set(key, false) }
end

end
end
21 changes: 21 additions & 0 deletions lib/mailman/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Mailman
class Configuration

# Takes a block that is evaluated in the singleton to set options.
def initialize(&block)
class_eval(&block)
end

class << self

# @return [Logger] the application's logger
attr_accessor :logger

def logger
@logger ||= Logger.new(STDOUT)
end

end

end
end
30 changes: 0 additions & 30 deletions spec/mailman/application_spec.rb

This file was deleted.

19 changes: 19 additions & 0 deletions spec/mailman/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', '/spec_helper'))

describe Mailman::Configuration do

it 'should have a default logger' do
Mailman::Configuration.logger.instance_variable_get('@logdev').dev.should == STDOUT
end

it 'should store a custom logger using the class method' do
Mailman::Configuration.logger = Logger.new(STDERR)
Mailman::Configuration.logger.instance_variable_get('@logdev').dev.should == STDERR
end

it 'should store the custom logger using a block' do
Mailman::Configuration.new { logger = Logger.new(STDERR) }
Mailman::Configuration.logger.instance_variable_get('@logdev').dev.should == STDERR
end

end

0 comments on commit d7be079

Please sign in to comment.