svutil 0.0.13 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/svutil.rb +1 -1
- data/lib/svutil/config.rb +75 -60
- data/test/test_config.rb +78 -0
- data/test/test_defaults.rb +13 -0
- data/test/test_helper.rb +1 -0
- metadata +8 -6
data/lib/svutil.rb
CHANGED
data/lib/svutil/config.rb
CHANGED
@@ -7,10 +7,32 @@ module SVUtil
|
|
7
7
|
def self.config
|
8
8
|
Config.config_class
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
|
+
class Defaults
|
12
|
+
def initialize
|
13
|
+
@attrs = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def default_for(name)
|
17
|
+
@attrs[name]
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
def method_missing(method_id, *args)
|
22
|
+
@attrs ||= {}
|
23
|
+
if method_id.to_s =~ /=$/
|
24
|
+
@attrs[method_id.to_s[0...-1]] = args.first
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
11
29
|
class Config
|
12
30
|
class << self
|
13
31
|
attr_writer :config_file
|
32
|
+
attr_reader :attrs
|
33
|
+
attr_reader :defaults
|
34
|
+
# Used mainly for testing
|
35
|
+
attr_accessor :option_source
|
14
36
|
|
15
37
|
def config_class
|
16
38
|
subclasses_of(self).first || self
|
@@ -20,47 +42,17 @@ module SVUtil
|
|
20
42
|
@config_file || 'settings'
|
21
43
|
end
|
22
44
|
|
23
|
-
def
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
apply_all
|
28
|
-
validate
|
29
|
-
end
|
30
|
-
|
31
|
-
def set_defaults
|
32
|
-
# Overide this method in subclasses
|
33
|
-
end
|
34
|
-
|
35
|
-
def default(name, value)
|
36
|
-
@temp_hash ||= {}
|
37
|
-
if !@temp_hash.has_key?(name) || @temp_hash[name].nil?
|
38
|
-
set(name, value)
|
45
|
+
def set(options = {})
|
46
|
+
@attrs ||= {}
|
47
|
+
if block_given?
|
48
|
+
yield self
|
39
49
|
end
|
50
|
+
self.validate
|
40
51
|
end
|
41
52
|
|
42
|
-
def
|
43
|
-
|
44
|
-
|
45
|
-
end
|
46
|
-
|
47
|
-
def apply(name, value)
|
48
|
-
@hash ||= {}
|
49
|
-
@hash[name.to_s] = value
|
50
|
-
end
|
51
|
-
|
52
|
-
def apply_all
|
53
|
-
@hash ||= {}
|
54
|
-
@hash.merge!(@temp_hash) if @temp_hash
|
55
|
-
end
|
56
|
-
|
57
|
-
def method_missing(method_id, *args)
|
58
|
-
return nil unless @hash
|
59
|
-
if method_id.to_s =~ /=$/
|
60
|
-
@hash[method_id.to_s[0...-1]] = args.first
|
61
|
-
else
|
62
|
-
@hash[method_id.to_s]
|
63
|
-
end
|
53
|
+
def defaults(&block)
|
54
|
+
@defaults ||= Defaults.new
|
55
|
+
yield @defaults
|
64
56
|
end
|
65
57
|
|
66
58
|
def validate
|
@@ -72,29 +64,52 @@ module SVUtil
|
|
72
64
|
true
|
73
65
|
end
|
74
66
|
|
75
|
-
def
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
OptionParser.new do |opts|
|
81
|
-
opts.on("-f", "--config [filename]", "Config file to use (default 'settings')") do |filename|
|
82
|
-
self.config_file = filename
|
83
|
-
end
|
84
|
-
opts.on("-d", "--daemon", "Run in the background as a daemon") do
|
85
|
-
self.set(:daemon, true)
|
86
|
-
end
|
87
|
-
opts.on("-l", "--debug-log [log-file]", "Debug Log File") do |log|
|
88
|
-
self.set(log_file, log)
|
89
|
-
end
|
90
|
-
opts.on("-T", "--trace", "Display backtrace on errors") do
|
91
|
-
self.set(:trace, true)
|
92
|
-
end
|
93
|
-
yield opts if block_given?
|
94
|
-
end.parse!
|
67
|
+
def init
|
68
|
+
self.set do |c|
|
69
|
+
process_options
|
70
|
+
load_config_file
|
71
|
+
end
|
95
72
|
end
|
96
73
|
|
74
|
+
protected
|
75
|
+
# Overide in subclasses for additional options
|
76
|
+
def process_options
|
77
|
+
parse_options
|
78
|
+
end
|
79
|
+
|
80
|
+
def parse_options(&block)
|
81
|
+
OptionParser.new do |opts|
|
82
|
+
opts.on("-f", "--config [filename]", "Config file to use (default 'settings')") do |filename|
|
83
|
+
self.config_file = filename.strip
|
84
|
+
end
|
85
|
+
opts.on("-d", "--daemon", "Run in the background as a daemon") do
|
86
|
+
self.daemon = true
|
87
|
+
end
|
88
|
+
opts.on("-l", "--debug-log [log-file]", "Debug Log File") do |log|
|
89
|
+
self.log_file = log
|
90
|
+
end
|
91
|
+
opts.on("-T", "--trace", "Display backtrace on errors") do
|
92
|
+
self.trace = true
|
93
|
+
end
|
94
|
+
yield opts if block_given?
|
95
|
+
end.parse!(self.option_source || ARGV)
|
96
|
+
end
|
97
|
+
|
97
98
|
private
|
99
|
+
def method_missing(method_id, *args)
|
100
|
+
@attrs ||= {}
|
101
|
+
if method_id.to_s =~ /=$/
|
102
|
+
@attrs[method_id.to_s[0...-1]] = args.first
|
103
|
+
else
|
104
|
+
value = @attrs[method_id.to_s]
|
105
|
+
if !value && @defaults
|
106
|
+
@defaults.default_for(method_id.to_s)
|
107
|
+
else
|
108
|
+
value
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
98
113
|
def load_config_file
|
99
114
|
contents = ""
|
100
115
|
File.open(config_file, "r") { |file| contents << file.read }
|
@@ -104,7 +119,7 @@ module SVUtil
|
|
104
119
|
STDERR.puts "Invalid config file '#{config_file}'. Syntax error at line #{index + 1}"
|
105
120
|
exit 1
|
106
121
|
end
|
107
|
-
|
122
|
+
self.send("#{pair[0].strip}=", pair[1].strip)
|
108
123
|
end
|
109
124
|
end
|
110
125
|
|
data/test/test_config.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class CustomConfig < SVUtil::Config
|
4
|
+
defaults do |c|
|
5
|
+
c.foo = 'bar'
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class TestConfig < Test::Unit::TestCase
|
10
|
+
include SVUtil
|
11
|
+
|
12
|
+
def setup
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_set
|
16
|
+
assert !Config.my_symbol
|
17
|
+
Config.my_symbol = 'abc'
|
18
|
+
assert_equal 'abc', Config.my_symbol
|
19
|
+
Config.my_var = 10
|
20
|
+
assert_equal 10, Config.my_var
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_set_with_block
|
24
|
+
Config.expects(:validate).times(1)
|
25
|
+
Config.set do |c|
|
26
|
+
c.another = 'abc'
|
27
|
+
c.some_var = 123
|
28
|
+
c.foobar = true
|
29
|
+
end
|
30
|
+
assert_equal 'abc', Config.another
|
31
|
+
assert_equal 123, Config.some_var
|
32
|
+
assert Config.foobar
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_standard_cli_options
|
36
|
+
Config.expects(:validate).times(1)
|
37
|
+
Config.option_source = [ "-f", "test/settings", "-d", "-l", "debug", "-T" ]
|
38
|
+
Config.init
|
39
|
+
assert_equal "test/settings", Config.config_file
|
40
|
+
assert_equal "debug", Config.log_file
|
41
|
+
assert Config.daemon
|
42
|
+
assert Config.trace
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_config_file
|
46
|
+
Config.expects(:validate).times(1)
|
47
|
+
Config.config_file = "test/settings"
|
48
|
+
Config.init
|
49
|
+
assert_equal 'bar', Config.foo
|
50
|
+
end
|
51
|
+
|
52
|
+
# Command Line loses!
|
53
|
+
def test_file_overide
|
54
|
+
Config.expects(:validate).times(1)
|
55
|
+
Config.config_file = "test/settings"
|
56
|
+
Config.option_source = [ "-l", "logfile_set_on_cli" ]
|
57
|
+
Config.init
|
58
|
+
assert_equal "logfile_set_in_config_file", Config.log_file
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_defaults
|
63
|
+
Config.defaults do |c|
|
64
|
+
c.manager = 'daniel'
|
65
|
+
end
|
66
|
+
assert_equal 'daniel', Config.manager
|
67
|
+
Config.manager = 'james'
|
68
|
+
assert_equal 'james', Config.manager
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_validations
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_custom_defaults
|
76
|
+
assert_equal 'bar', CustomConfig.foo
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class TestDefaults < Test::Unit::TestCase
|
4
|
+
include SVUtil
|
5
|
+
|
6
|
+
def test_set
|
7
|
+
defaults = Defaults.new
|
8
|
+
assert !defaults.default_for('person')
|
9
|
+
defaults.person = 'daniel'
|
10
|
+
assert_equal 'daniel', defaults.default_for('person')
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 1
|
8
|
+
- 2
|
9
|
+
version: 0.1.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Daniel Draper
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-02-
|
17
|
+
date: 2011-02-08 00:00:00 +10:30
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -40,9 +40,9 @@ dependencies:
|
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
segments:
|
42
42
|
- 2
|
43
|
-
-
|
43
|
+
- 4
|
44
44
|
- 0
|
45
|
-
version: 2.
|
45
|
+
version: 2.4.0
|
46
46
|
type: :development
|
47
47
|
version_requirements: *id002
|
48
48
|
description: A simple managed process builder for Ruby projects
|
@@ -100,5 +100,7 @@ signing_key:
|
|
100
100
|
specification_version: 3
|
101
101
|
summary: Simple Process Builder for Ruby
|
102
102
|
test_files:
|
103
|
+
- test/test_config.rb
|
104
|
+
- test/test_defaults.rb
|
103
105
|
- test/test_helper.rb
|
104
106
|
- test/test_svutil.rb
|