Skip to content

Commit f14e2e3

Browse files
committed
Merge pull request #10 from Expez/config
Add support for configuration files
2 parents 840e3c0 + 143fba5 commit f14e2e3

File tree

7 files changed

+106
-1
lines changed

7 files changed

+106
-1
lines changed

.hatterrc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#
2+
# This file contains the default configuration for Hatter.
3+
4+
# Each setting is of the form: <key> = <value>
5+
6+
# Path to the root of the local maildir
7+
maildir = /path/to/maildir
8+
9+
# The format of the maildir, e.g maildir or mbox
10+
maildir_format = maildir

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ group :development, :test do
44
gem 'rspec'
55
gem 'pry'
66
gem 'rake'
7+
gem 'mail'
8+
gem 'rbcurse'
9+
gem 'configtoolkit'
710
end
811

912
# Specify your gem's dependencies in hatter.gemspec

Gemfile.lock

+32
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,27 @@ PATH
66
GEM
77
remote: http://rubygems.org/
88
specs:
9+
assertions (1.4.1)
910
coderay (1.0.8)
11+
configtoolkit (2.3.2)
12+
assertions (>= 1.0.0)
13+
relative (>= 1.0.0)
1014
diff-lcs (1.1.3)
15+
ffi (1.3.1)
16+
ffi (1.3.1-x86-mingw32)
17+
ffi-locale (1.0.1)
18+
ffi (>= 1.0.9)
19+
ffi-ncurses (0.4.0)
20+
ffi (>= 1.0.9)
21+
ffi-locale (>= 1.0.0)
22+
i18n (0.6.1)
23+
mail (2.5.3)
24+
i18n (>= 0.4.0)
25+
mime-types (~> 1.16)
26+
treetop (~> 1.4.8)
1127
method_source (0.8.1)
28+
mime-types (1.19)
29+
polyglot (0.3.3)
1230
pry (0.9.11.4)
1331
coderay (~> 1.0.5)
1432
method_source (~> 0.8)
@@ -19,6 +37,14 @@ GEM
1937
slop (~> 3.4)
2038
win32console (~> 1.3)
2139
rake (10.0.3)
40+
rbcurse (1.5.2)
41+
rbcurse-core (>= 0.0)
42+
rbcurse-extras (>= 0.0)
43+
rbcurse-core (0.0.3)
44+
ffi-ncurses (>= 0.4.0)
45+
rbcurse-extras (0.0.0)
46+
rbcurse-core (>= 0.0.3)
47+
relative (1.0.3)
2248
rspec (2.12.0)
2349
rspec-core (~> 2.12.0)
2450
rspec-expectations (~> 2.12.0)
@@ -28,6 +54,9 @@ GEM
2854
diff-lcs (~> 1.1.3)
2955
rspec-mocks (2.12.2)
3056
slop (3.4.3)
57+
treetop (1.4.12)
58+
polyglot
59+
polyglot (>= 0.3.1)
3160
win32console (1.3.2-x86-mingw32)
3261

3362
PLATFORMS
@@ -36,6 +65,9 @@ PLATFORMS
3665

3766
DEPENDENCIES
3867
Hatter!
68+
configtoolkit
69+
mail
3970
pry
4071
rake
72+
rbcurse
4173
rspec

lib/configuration.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'configtoolkit'
2+
require 'configtoolkit/keyvaluereader'
3+
4+
class Configuration < ConfigToolkit::BaseConfig
5+
6+
# Reads a configuration from file.
7+
#
8+
# @param [String] configPath the path to a configuration file.
9+
#
10+
# @return [Configuration] with values loaded from file.
11+
def self.from_file configPath
12+
reader = ConfigToolkit::KeyValueReader.new configPath
13+
load reader
14+
end
15+
16+
add_required_param(:maildir, String)
17+
add_required_param(:maildir_format, String)
18+
end

lib/hatter.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

spec/configuration_spec.rb

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require 'spec_helper'
2+
require 'relative'
3+
require 'configtoolkit/keyvaluereader'
4+
5+
describe 'Configuration' do
6+
7+
before :all do
8+
@maildir_path = "/path/to/maildir"
9+
CONFIG_FILE = File.expand_path_relative_to_caller("../.hatterrc")
10+
@maildir_format = "maildir"
11+
@config = Configuration.new() do |config|
12+
config.maildir = @maildir_path
13+
config.maildir_format = @maildir_format
14+
end
15+
end
16+
17+
it "contains all the required configuration settings" do
18+
@config.maildir?.should be_true
19+
@config.maildir_format?.should be_true
20+
end
21+
22+
it "the settings have the correct values" do
23+
@config.maildir.should eq @maildir_path
24+
@config.maildir_format.should eq @maildir_format
25+
end
26+
27+
it "reads the configuration values from file" do
28+
config = Configuration.from_file CONFIG_FILE
29+
config.maildir.should eq @maildir_path
30+
config.maildir_format.should eq @maildir_format
31+
end
32+
33+
it "barfs when required values are missing" do
34+
config = File.read CONFIG_FILE
35+
config.gsub!(/maildir/, 'asdf')
36+
File.open("invalid_config", "w") {|file| file.write config}
37+
expect {Configuration.from_file("invalid_config")}.to raise_error
38+
FileUtils::rm("invalid_config")
39+
end
40+
end

spec/spec_helper.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
require 'rspec'
22

3-
require './lib/hatter.rb'
3+
require './lib/hatter.rb'
4+
require './lib/configuration.rb'

0 commit comments

Comments
 (0)