app-ctx 0.1.4 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/{CHANGELOG → History.txt} +32 -0
- data/Manifest.txt +17 -0
- data/README.txt +151 -0
- data/Rakefile +15 -0
- data/lib/app-ctx.rb +41 -6
- data/test/config-1.yml +5 -0
- data/test/config-2.yml +3 -0
- data/test/plain-ruby-as-config-dsl.rb +14 -0
- data/test/{t_app-ctx.rb → test_app-ctx.rb} +80 -7
- metadata +34 -22
- data/README +0 -118
data/{CHANGELOG → History.txt}
RENAMED
@@ -1,3 +1,34 @@
|
|
1
|
+
== 0.1.6 / 2007-02-17
|
2
|
+
|
3
|
+
* repacked with hoe
|
4
|
+
|
5
|
+
Sat Feb 17 10:56:36 CET 2007 dirk@sebrink.de
|
6
|
+
* class#method entrypoints added and hash pimping tested and fixed
|
7
|
+
|
8
|
+
Sun Feb 11 15:59:24 CET 2007 dirk@sebrink.de
|
9
|
+
* proof-of-concept: loading plain ruby values from file
|
10
|
+
|
11
|
+
Sun Feb 11 15:58:48 CET 2007 dirk@sebrink.de
|
12
|
+
* customized :entrypoints
|
13
|
+
|
14
|
+
Sun Feb 11 15:57:36 CET 2007 dirk@sebrink.de
|
15
|
+
* minor
|
16
|
+
|
17
|
+
Sat Dec 2 22:03:46 CET 2006 dirk@sebrink.de
|
18
|
+
* new: forward
|
19
|
+
|
20
|
+
Thu Nov 2 16:42:56 CET 2006 dirk@sebrink.de
|
21
|
+
* bugfix: rdoc include missing for ruby 1.8.5
|
22
|
+
|
23
|
+
Thu Nov 2 16:40:37 CET 2006 dirk@sebrink.de
|
24
|
+
* bug fix: rdoc include needed for ruby 1.8.5 version
|
25
|
+
|
26
|
+
Sun Oct 29 10:10:32 CET 2006 dirk@sebrink.de
|
27
|
+
* made gemspec file autobuilding
|
28
|
+
|
29
|
+
Sat Oct 21 12:23:02 CEST 2006 dirk@sebrink.de
|
30
|
+
* n
|
31
|
+
|
1
32
|
Sat Oct 21 11:45:50 CEST 2006 dirk@sebrink.de
|
2
33
|
* added examples to gemspec
|
3
34
|
|
@@ -66,3 +97,4 @@ Thu Aug 24 18:05:58 CEST 2006 dirk@sebrink.de
|
|
66
97
|
|
67
98
|
Wed Aug 23 12:12:00 CEST 2006 dirk@sebrink.de
|
68
99
|
* new repository
|
100
|
+
|
data/Manifest.txt
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
History.txt
|
2
|
+
Manifest.txt
|
3
|
+
README.txt
|
4
|
+
Rakefile
|
5
|
+
lib/app-ctx.rb
|
6
|
+
lib/app-ctx
|
7
|
+
lib/app-ctx/dlog.rb
|
8
|
+
test/config-1.yml
|
9
|
+
test/config-2.yml
|
10
|
+
test/plain-ruby-as-config-dsl.rb
|
11
|
+
test/test_app-ctx.rb
|
12
|
+
examples/boolean.rb
|
13
|
+
examples/conversions.rb
|
14
|
+
examples/ruby_conv.rb
|
15
|
+
examples/run_with_block.rb
|
16
|
+
examples/run_with_class.rb
|
17
|
+
examples/set_default_values.rb
|
data/README.txt
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
app-ctx
|
2
|
+
by dirk luesebrink
|
3
|
+
http://www.sofasportler.de/dirk.blog
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Every application needs configuration and app-ctx provides a concise way of
|
8
|
+
doing it.
|
9
|
+
|
10
|
+
For all applications (you are not a mouseclicker, are u?), once in a while
|
11
|
+
you need to supply some configuration values to overrule the built-in
|
12
|
+
defaults. The app-ctx gem does unify and organize built-in constants,
|
13
|
+
config files and commandline option with a clearly defined priority, from
|
14
|
+
low to high:
|
15
|
+
|
16
|
+
- procedural: set from your implementation App::Config#set_default_values
|
17
|
+
- YAML default values file loaded from next to the $0 script
|
18
|
+
- user supplied configuration file, eg.: --config=/tmp/foo.yml
|
19
|
+
- command line options and flags: --foo --bar=foo
|
20
|
+
|
21
|
+
But for your application it is of no interesst from where the values are
|
22
|
+
coming: command line option: "--port=1234", a user configuration file or
|
23
|
+
from the applications built-in default values. Therefor +app-ctx+ combines
|
24
|
+
value settings from various sources into a single configuration hash.
|
25
|
+
|
26
|
+
== SYNOPSIS:
|
27
|
+
|
28
|
+
(hint: see also the examples dir)
|
29
|
+
basically you have two ways to use it:
|
30
|
+
|
31
|
+
require 'app-ctx' # of course,and than...
|
32
|
+
|
33
|
+
1. closures (see examples/run_with_block.rb)
|
34
|
+
|
35
|
+
App::ctx.run do |context| ... end
|
36
|
+
|
37
|
+
where context object provides:
|
38
|
+
|
39
|
+
values: the combined key and value settings
|
40
|
+
argv: remaining argument(not the options) of the command line
|
41
|
+
defaults_path: full path to the defaults file
|
42
|
+
|
43
|
+
|
44
|
+
2. with a mainclass(see examples/run_with_class.rb)
|
45
|
+
|
46
|
+
App::run :class => Simple
|
47
|
+
|
48
|
+
for the second case(with a mainclass) an application instance of this class
|
49
|
+
is created. The first argument is than taken as method name and executed,
|
50
|
+
again with a context oject provided.
|
51
|
+
|
52
|
+
$ ruby example/run_with_class show
|
53
|
+
|
54
|
+
will result in the :show method beeing called:
|
55
|
+
|
56
|
+
context = Config.new...
|
57
|
+
...
|
58
|
+
app = Simple.new...
|
59
|
+
app.show(context)
|
60
|
+
|
61
|
+
|
62
|
+
= String/Type mappings
|
63
|
+
|
64
|
+
Commandline options are strings only, but sometimes you need strongly typed
|
65
|
+
primitive values. +app-ctx+ does automatically convert integer and float
|
66
|
+
values, see "examples/conversions.rb" for:
|
67
|
+
|
68
|
+
$ ./examples/conversions.rb
|
69
|
+
{:i=>23, :f=>3.14}
|
70
|
+
typeof 'i': Fixnum
|
71
|
+
typeof 'f': Float
|
72
|
+
|
73
|
+
$ ./examples/conversions.rb -i=17 -f=2.12
|
74
|
+
{:i=>17, :f=>2.12}
|
75
|
+
typeof 'i': Fixnum
|
76
|
+
typeof 'f': Float
|
77
|
+
|
78
|
+
$ ./examples/conversions.rb -i=i -f=f
|
79
|
+
{:i=>"i", :f=>"f"}
|
80
|
+
typeof 'i': String
|
81
|
+
typeof 'f': String
|
82
|
+
|
83
|
+
|
84
|
+
== Flags/Boolean values
|
85
|
+
|
86
|
+
Flags(options without values) are converted to boolean values, see
|
87
|
+
examples/boolean.rb:
|
88
|
+
|
89
|
+
$ ruby ./examples/boolean.rb
|
90
|
+
{:bool=>false}
|
91
|
+
typeof 'bool': FalseClass
|
92
|
+
|
93
|
+
$ ruby ./examples/boolean.rb --bool
|
94
|
+
{:bool=>true}
|
95
|
+
typeof 'bool': TrueClass
|
96
|
+
|
97
|
+
|
98
|
+
== Ruby Conversions
|
99
|
+
|
100
|
+
When Fixnum, Float and boolean conversion are not enough, as a last resort,
|
101
|
+
you can use ruby code directly for evaluation of option values. Replacing
|
102
|
+
'=' with ':' results in assigning the ruby evaluation value to the key, see
|
103
|
+
examples/ruby_conv.rb:
|
104
|
+
|
105
|
+
$ ruby ./examples/ruby_conv.rb
|
106
|
+
{:r=>1..10, :a=>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}
|
107
|
+
typeof 'r': Range
|
108
|
+
typeof 'a': Array
|
109
|
+
|
110
|
+
$ ruby ./examples/ruby_conv.rb -r:2..3 -a:'(-1..1).to_a'
|
111
|
+
{:r=>2..3, :a=>[-1, 0, 1]}
|
112
|
+
typeof 'r': Range
|
113
|
+
typeof 'a': Array
|
114
|
+
|
115
|
+
$ ruby ./examples/ruby_conv.rb -r:2..3 -a:\(-1..1\).to_a
|
116
|
+
{:r=>2..3, :a=>[-1, 0, 1]}
|
117
|
+
typeof 'r': Range
|
118
|
+
typeof 'a': Array
|
119
|
+
|
120
|
+
== REQUIREMENTS:
|
121
|
+
|
122
|
+
* the hoe gem
|
123
|
+
|
124
|
+
== INSTALL:
|
125
|
+
|
126
|
+
* sudo gem install app-ctx --include-dependencies
|
127
|
+
|
128
|
+
== LICENSE:
|
129
|
+
|
130
|
+
(The MIT License)
|
131
|
+
|
132
|
+
Copyright (c) 2007 FIX
|
133
|
+
|
134
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
135
|
+
a copy of this software and associated documentation files (the
|
136
|
+
'Software'), to deal in the Software without restriction, including
|
137
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
138
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
139
|
+
permit persons to whom the Software is furnished to do so, subject to
|
140
|
+
the following conditions:
|
141
|
+
|
142
|
+
The above copyright notice and this permission notice shall be
|
143
|
+
included in all copies or substantial portions of the Software.
|
144
|
+
|
145
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
146
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
147
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
148
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
149
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
150
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
151
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
require './lib/app-ctx.rb'
|
6
|
+
|
7
|
+
Hoe.new('app-ctx', App::VERSION) do |p|
|
8
|
+
p.rubyforge_name = 'app-ctx'
|
9
|
+
p.summary = "Every application needs configuration and app-ctx provides a concise way of doing it."
|
10
|
+
p.description = p.paragraphs_of('README.txt', 2..4).join("\n\n")
|
11
|
+
p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
|
12
|
+
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
13
|
+
end
|
14
|
+
|
15
|
+
# vim: syntax=Ruby
|
data/lib/app-ctx.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
|
2
1
|
require 'yaml'
|
3
2
|
require 'rdoc/rdoc'
|
4
3
|
require 'rdoc/usage'
|
5
4
|
|
5
|
+
$:.unshift File.dirname(__FILE__)
|
6
6
|
require 'app-ctx/dlog'
|
7
7
|
|
8
8
|
module App
|
9
9
|
|
10
|
+
VERSION = '0.1.6'
|
11
|
+
|
10
12
|
# the priority of configuation settings is:
|
11
13
|
# 1. command line options
|
12
14
|
# 2. configuration from custom file, eg.: --config=/tmp/foo.yml
|
@@ -39,6 +41,14 @@ module App
|
|
39
41
|
@values = defaults.merge(@values)
|
40
42
|
end
|
41
43
|
|
44
|
+
# continue processing of command line with next argument from cmdline
|
45
|
+
def forward(target)
|
46
|
+
begin
|
47
|
+
(op = argv.shift) && target.send(op, self)
|
48
|
+
rescue ArgumentError => e
|
49
|
+
target.send(op)
|
50
|
+
end
|
51
|
+
end
|
42
52
|
|
43
53
|
def to_s
|
44
54
|
<<-EOT
|
@@ -139,7 +149,7 @@ module App
|
|
139
149
|
|
140
150
|
thank you, good bye and have a nice day
|
141
151
|
|
142
|
-
|
152
|
+
EOM
|
143
153
|
}
|
144
154
|
|
145
155
|
def inform message_id, binding
|
@@ -151,7 +161,13 @@ module App
|
|
151
161
|
@context = context
|
152
162
|
end
|
153
163
|
|
154
|
-
def execute
|
164
|
+
def execute opts = {}
|
165
|
+
|
166
|
+
tmp = opts[:entrypoint] || :application_main
|
167
|
+
entrypoint, clazz = tmp.to_s.split("#").reverse
|
168
|
+
clazz = eval "#{clazz ||= opts[:class]}"
|
169
|
+
|
170
|
+
pimp_my_hash(@context.values)
|
155
171
|
|
156
172
|
if block_given?
|
157
173
|
warn "attached block takes precedence over class!" if clazz
|
@@ -165,11 +181,12 @@ module App
|
|
165
181
|
setter_injection app, @context
|
166
182
|
|
167
183
|
begin
|
184
|
+
pimp_my_hash(@context.values) # XXX refresh lost pimping
|
168
185
|
# first try the default entry point
|
169
|
-
app.
|
186
|
+
app.send(entrypoint, @context)
|
170
187
|
|
171
188
|
rescue NoMethodError => e
|
172
|
-
if app.respond_to?
|
189
|
+
if app.respond_to?(entrypoint)
|
173
190
|
DL.logger.error "failed invokation", e
|
174
191
|
else
|
175
192
|
if 0 == @context.argv.length
|
@@ -182,6 +199,8 @@ module App
|
|
182
199
|
begin
|
183
200
|
op = @context.argv.shift
|
184
201
|
app.send(op, @context)
|
202
|
+
rescue ArgumentError => e
|
203
|
+
app.send(op)
|
185
204
|
rescue => e
|
186
205
|
DL.logger.error "oops: #{e}", e
|
187
206
|
end
|
@@ -192,6 +211,22 @@ module App
|
|
192
211
|
app # the created application instance
|
193
212
|
end
|
194
213
|
|
214
|
+
def pimp_my_hash(h) # as seen on why/redhanded the first time
|
215
|
+
class << h
|
216
|
+
def method_missing(m,*a)
|
217
|
+
if m.to_s =~ /=$/
|
218
|
+
self[$`.to_sym] = a[0]
|
219
|
+
elsif a.empty?
|
220
|
+
self[m]
|
221
|
+
else
|
222
|
+
raise NoMethodError, "#{m}"
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
h
|
227
|
+
end
|
228
|
+
private :pimp_my_hash
|
229
|
+
|
195
230
|
# IoC setter dependency injection:
|
196
231
|
# try all context keys as setter class
|
197
232
|
def setter_injection obj, context
|
@@ -216,7 +251,7 @@ module App
|
|
216
251
|
if block_given?
|
217
252
|
container.execute {|context| block.call(context) }
|
218
253
|
else
|
219
|
-
container.execute
|
254
|
+
container.execute(params)
|
220
255
|
end
|
221
256
|
end
|
222
257
|
|
data/test/config-1.yml
ADDED
data/test/config-2.yml
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
|
2
|
+
title = "plain ruby as config file DSL"
|
3
|
+
|
4
|
+
comment = %Q{
|
5
|
+
this file gets loaded on startup and local varibles in-here thereafter
|
6
|
+
become available as part of the global configuration};
|
7
|
+
|
8
|
+
require 'date'
|
9
|
+
birthday = Date.parse "2007-11-16"
|
10
|
+
|
11
|
+
float_value = 1.234
|
12
|
+
int_value = 1234
|
13
|
+
|
14
|
+
some_string = %q{the quick brown...}
|
@@ -8,6 +8,36 @@ require 'app-ctx'
|
|
8
8
|
|
9
9
|
class AppModuleTest < Test::Unit::TestCase
|
10
10
|
|
11
|
+
def load_values_from_file(filepath)
|
12
|
+
b = binding
|
13
|
+
v1 = eval "local_variables", b
|
14
|
+
eval IO.read(filepath), b
|
15
|
+
v2 = eval "local_variables", b
|
16
|
+
(v2 - v1).inject({}) do |c, key|
|
17
|
+
c[key.to_sym] = eval "#{key}", b
|
18
|
+
c
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_load_values_from_file
|
23
|
+
puts "\n --> #{self.name}"
|
24
|
+
filename = File.join(File.dirname(__FILE__), "plain-ruby-as-config-dsl.rb")
|
25
|
+
c = load_values_from_file(filename)
|
26
|
+
assert_kind_of Hash, c
|
27
|
+
#assert_equal 6, c.size
|
28
|
+
expect = {
|
29
|
+
:float_value => 1.234,
|
30
|
+
:int_value => 1234,
|
31
|
+
:some_string => "the quick brown...",
|
32
|
+
:title => "plain ruby as config file DSL",
|
33
|
+
:comment => %Q{
|
34
|
+
this file gets loaded on startup and local varibles in-here thereafter
|
35
|
+
become available as part of the global configuration},
|
36
|
+
:birthday => Date.parse("2007-11-16"),
|
37
|
+
}
|
38
|
+
assert_equal expect, c
|
39
|
+
end
|
40
|
+
|
11
41
|
class With_application_main
|
12
42
|
attr_reader :context
|
13
43
|
attr_accessor :baroque
|
@@ -36,6 +66,32 @@ class AppModuleTest < Test::Unit::TestCase
|
|
36
66
|
@context = context
|
37
67
|
end
|
38
68
|
end
|
69
|
+
|
70
|
+
def test_customized_entrypoint
|
71
|
+
puts "\n --> #{self.name}"
|
72
|
+
argv = ["-f", "--foo=bar", "arg1", "arg2"]
|
73
|
+
app = App::run :entrypoint => :entrypoint, :class => ArgumentEntryPoint, :argv => argv
|
74
|
+
assert_not_nil app
|
75
|
+
assert_equal ArgumentEntryPoint, app.class
|
76
|
+
assert_not_nil app.context
|
77
|
+
assert_equal({:f=>true, :foo=>"bar"}, app.context.values)
|
78
|
+
assert_equal ["arg1", "arg2"], app.context.argv
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_class_method_entrypoint
|
82
|
+
puts "\n --> #{self.name}"
|
83
|
+
argv = ["-f", "--foo=bar", "arg1", "arg2"]
|
84
|
+
app = App::run :entrypoint => "AppModuleTest::ArgumentEntryPoint#entrypoint", :argv => argv
|
85
|
+
assert_not_nil app
|
86
|
+
assert_equal ArgumentEntryPoint, app.class
|
87
|
+
assert_not_nil app.context
|
88
|
+
assert_equal({:f=>true, :foo=>"bar"}, app.context.values)
|
89
|
+
assert_equal ["arg1", "arg2"], app.context.argv
|
90
|
+
|
91
|
+
assert_equal true, app.context.values.f
|
92
|
+
assert_equal "bar", app.context.values.foo
|
93
|
+
end
|
94
|
+
|
39
95
|
def test_run_without_point
|
40
96
|
puts "\n --> #{self.name}"
|
41
97
|
argv = ["-f", "--foo=bar", "entrypoint", "first_method_arg"]
|
@@ -45,6 +101,9 @@ class AppModuleTest < Test::Unit::TestCase
|
|
45
101
|
assert_not_nil app.context
|
46
102
|
assert_equal({:f=>true, :foo=>"bar"}, app.context.values)
|
47
103
|
assert_equal ["first_method_arg"], app.context.argv
|
104
|
+
|
105
|
+
assert_equal true, app.context.values.f
|
106
|
+
assert_equal "bar", app.context.values.foo
|
48
107
|
end
|
49
108
|
|
50
109
|
end
|
@@ -53,19 +112,33 @@ class AppConfigTest < Test::Unit::TestCase
|
|
53
112
|
|
54
113
|
include App
|
55
114
|
|
115
|
+
def test_forward
|
116
|
+
puts "\n --> #{self.name}"
|
117
|
+
class << (recorder = Object.new)
|
118
|
+
def c; "c"; end
|
119
|
+
def method_missing(m, *args, &blk)
|
120
|
+
args.first.forward(self)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
c = Config.new :argv => ["a", "b", "c"]
|
124
|
+
assert_equal "c", c.forward(recorder)
|
125
|
+
end
|
126
|
+
|
56
127
|
def test_config_block
|
57
128
|
puts "\n --> #{self.name}"
|
58
|
-
App::
|
129
|
+
App::run do |context|
|
59
130
|
puts "--------------->context: #{context.inspect}"
|
60
131
|
assert_not_nil context
|
61
132
|
assert_equal({}, context.values)
|
62
133
|
end
|
63
134
|
|
64
|
-
App::
|
135
|
+
App::run(:argv=>["-f", "--foo=bar", "arg1", "arg2"]) do |context|
|
65
136
|
puts "--------------->context: #{context.inspect}"
|
66
137
|
assert_not_nil context
|
67
138
|
assert_equal({:f=>true, :foo=>"bar"}, context.values)
|
68
139
|
assert_equal ["arg1", "arg2"], context.argv
|
140
|
+
assert_equal true, context.values.f
|
141
|
+
assert_equal "bar", context.values.foo
|
69
142
|
end
|
70
143
|
end
|
71
144
|
|
@@ -73,17 +146,17 @@ class AppConfigTest < Test::Unit::TestCase
|
|
73
146
|
puts "\n --> #{self.name}"
|
74
147
|
c = App::Config.new
|
75
148
|
# config file does not exist:w
|
76
|
-
c.load_config_file File.join(File.dirname(
|
149
|
+
c.load_config_file File.join(File.dirname(__FILE__), "no-such-file")
|
77
150
|
assert_equal({}, c.values)
|
78
151
|
|
79
152
|
# load configuration from file
|
80
|
-
c.load_config_file File.join(File.dirname(
|
153
|
+
c.load_config_file File.join(File.dirname(__FILE__), "config-1.yml")
|
81
154
|
assert_equal({
|
82
155
|
:f=>3.21, :i=>17, :empty_hash=>{}, :assoc=>{:key=>"foo"}
|
83
156
|
}, c.values)
|
84
157
|
|
85
158
|
# config overloading
|
86
|
-
c.load_config_file File.join(File.dirname(
|
159
|
+
c.load_config_file File.join(File.dirname(__FILE__), "config-2.yml")
|
87
160
|
assert_equal({
|
88
161
|
:f=>3.21, :i=>17, :empty_hash=>{},
|
89
162
|
:assoc=>{:key=>"bar"}, :f2=>-3.21
|
@@ -97,8 +170,8 @@ class AppConfigTest < Test::Unit::TestCase
|
|
97
170
|
assert_equal({}, c.values)
|
98
171
|
|
99
172
|
# find the default values config file next to the $0 script
|
100
|
-
puts "script: #{$0}"
|
101
|
-
assert_equal File.expand_path($0).sub(
|
173
|
+
puts "script: #{$0}"
|
174
|
+
assert_equal File.expand_path($0).sub(/(.rb)?$/, ".yml"), c.defaults_path
|
102
175
|
end
|
103
176
|
|
104
177
|
def test_config_path
|
metadata
CHANGED
@@ -3,15 +3,15 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: app-ctx
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date:
|
8
|
-
summary:
|
6
|
+
version: 0.1.6
|
7
|
+
date: 2007-02-17 00:00:00 +01:00
|
8
|
+
summary: Every application needs configuration and app-ctx provides a concise way of doing it.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
|
-
email:
|
12
|
-
homepage:
|
13
|
-
rubyforge_project:
|
14
|
-
description:
|
11
|
+
email: ryand-ruby@zenspider.com
|
12
|
+
homepage: " by dirk luesebrink"
|
13
|
+
rubyforge_project: app-ctx
|
14
|
+
description: "For all applications (you are not a mouseclicker, are u?), once in a while you need to supply some configuration values to overrule the built-in defaults. The app-ctx gem does unify and organize built-in constants, config files and commandline option with a clearly defined priority, from low to high: - procedural: set from your implementation App::Config#set_default_values - YAML default values file loaded from next to the $0 script - user supplied configuration file, eg.: --config=/tmp/foo.yml - command line options and flags: --foo --bar=foo But for your application it is of no interesst from where the values are coming: command line option: \"--port=1234\", a user configuration file or from the applications built-in default values. Therefor +app-ctx+ combines value settings from various sources into a single configuration hash."
|
15
15
|
autorequire:
|
16
16
|
default_executable:
|
17
17
|
bindir: bin
|
@@ -26,32 +26,44 @@ platform: ruby
|
|
26
26
|
signing_key:
|
27
27
|
cert_chain:
|
28
28
|
authors:
|
29
|
-
-
|
29
|
+
- Ryan Davis
|
30
30
|
files:
|
31
|
+
- History.txt
|
32
|
+
- Manifest.txt
|
33
|
+
- README.txt
|
34
|
+
- Rakefile
|
35
|
+
- lib/app-ctx.rb
|
36
|
+
- lib/app-ctx
|
37
|
+
- lib/app-ctx/dlog.rb
|
38
|
+
- test/config-1.yml
|
39
|
+
- test/config-2.yml
|
40
|
+
- test/plain-ruby-as-config-dsl.rb
|
41
|
+
- test/test_app-ctx.rb
|
31
42
|
- examples/boolean.rb
|
32
43
|
- examples/conversions.rb
|
33
44
|
- examples/ruby_conv.rb
|
34
45
|
- examples/run_with_block.rb
|
35
46
|
- examples/run_with_class.rb
|
36
47
|
- examples/set_default_values.rb
|
37
|
-
- lib/app-ctx.rb
|
38
|
-
- lib/app-ctx/dlog.rb
|
39
|
-
- test/t_app-ctx.rb
|
40
|
-
- CHANGELOG
|
41
|
-
- README
|
42
48
|
test_files:
|
43
|
-
- test/
|
44
|
-
rdoc_options:
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
- CHANGELOG
|
49
|
-
- README
|
49
|
+
- test/test_app-ctx.rb
|
50
|
+
rdoc_options: []
|
51
|
+
|
52
|
+
extra_rdoc_files: []
|
53
|
+
|
50
54
|
executables: []
|
51
55
|
|
52
56
|
extensions: []
|
53
57
|
|
54
58
|
requirements: []
|
55
59
|
|
56
|
-
dependencies:
|
57
|
-
|
60
|
+
dependencies:
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: hoe
|
63
|
+
version_requirement:
|
64
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.2.0
|
69
|
+
version:
|
data/README
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# = Application Context
|
3
|
-
#
|
4
|
-
# Every application needs configuration and app-ctx provides a concise way of
|
5
|
-
# doing it.
|
6
|
-
#
|
7
|
-
# For all applications (you are not a mouseclicker, are u?), once in a while
|
8
|
-
# you need to supply some configuration values to overrule the built-in
|
9
|
-
# defaults. The app-ctx gem does unify and organize built-in constants,
|
10
|
-
# config files and commandline option with a clearly defined priority, from
|
11
|
-
# low to high:
|
12
|
-
#
|
13
|
-
# - procedural: App::Config#set_default_values
|
14
|
-
# - default values YAML file from next to the $0 script
|
15
|
-
# - user supplied configuration file, eg.: --config=/tmp/foo.yml
|
16
|
-
# - command line options and flags: --foo --bar=foo
|
17
|
-
#
|
18
|
-
# But for your application it is of no interesst from where the values are
|
19
|
-
# coming: command line option: "--port=1234", a user configuration file or
|
20
|
-
# from the applications built-in default values. Therefor +app-ctx+ combines
|
21
|
-
# value settings from various sources into a single configuration hash.
|
22
|
-
#
|
23
|
-
# basically you have two ways to use it:
|
24
|
-
#
|
25
|
-
# require 'app-ctx' # of course,and than...
|
26
|
-
#
|
27
|
-
# 1. closures (see examples/run_with_block.rb)
|
28
|
-
#
|
29
|
-
# App::ctx.run do |context| ... end
|
30
|
-
#
|
31
|
-
# where context object provides:
|
32
|
-
#
|
33
|
-
# values: the combined key and value settings
|
34
|
-
# argv: remaining argument(not the options) of the command line
|
35
|
-
# defaults_path: full path to the defaults file
|
36
|
-
#
|
37
|
-
#
|
38
|
-
# 2. with a mainclass(see examples/run_with_class.rb)
|
39
|
-
#
|
40
|
-
# App::ctx.run YourClassHere
|
41
|
-
#
|
42
|
-
# for the second case(with a mainclass) an application instance of this class
|
43
|
-
# is created. The first argument is than taken as method name and executed,
|
44
|
-
# again with a context oject provided.
|
45
|
-
#
|
46
|
-
# $ ruby example/run_with_class show
|
47
|
-
#
|
48
|
-
# will result in the :show method beeing called:
|
49
|
-
#
|
50
|
-
# context = Config.new...
|
51
|
-
# ...
|
52
|
-
# app = Simple.new...
|
53
|
-
# app.show(context)
|
54
|
-
#
|
55
|
-
#
|
56
|
-
# == Conversions
|
57
|
-
#
|
58
|
-
# Commandline options are strings only, but sometimes you need strongly typed
|
59
|
-
# primitive values. +app-ctx+ does automatically convert integer and float
|
60
|
-
# values, see "examples/conversions.rb" for:
|
61
|
-
#
|
62
|
-
# $ ./examples/conversions.rb
|
63
|
-
# {:i=>23, :f=>3.14}
|
64
|
-
# typeof 'i': Fixnum
|
65
|
-
# typeof 'f': Float
|
66
|
-
#
|
67
|
-
# $ ./examples/conversions.rb -i=17 -f=2.12
|
68
|
-
# {:i=>17, :f=>2.12}
|
69
|
-
# typeof 'i': Fixnum
|
70
|
-
# typeof 'f': Float
|
71
|
-
#
|
72
|
-
# $ ./examples/conversions.rb -i=i -f=f
|
73
|
-
# {:i=>"i", :f=>"f"}
|
74
|
-
# typeof 'i': String
|
75
|
-
# typeof 'f': String
|
76
|
-
#
|
77
|
-
#
|
78
|
-
# == Flags/Boolean values
|
79
|
-
#
|
80
|
-
# Flags(options without values) are converted to boolean values, see
|
81
|
-
# examples/boolean.rb:
|
82
|
-
#
|
83
|
-
# $ ruby ./examples/boolean.rb
|
84
|
-
# {:bool=>false}
|
85
|
-
# typeof 'bool': FalseClass
|
86
|
-
#
|
87
|
-
# $ ruby ./examples/boolean.rb --bool
|
88
|
-
# {:bool=>true}
|
89
|
-
# typeof 'bool': TrueClass
|
90
|
-
#
|
91
|
-
#
|
92
|
-
# == Ruby Conversions
|
93
|
-
#
|
94
|
-
# When Fixnum, Float and boolean conversion are not enough, as a last resort,
|
95
|
-
# you can use ruby code directly for evaluation of option values. Replacing
|
96
|
-
# '=' with ':' results in assigning the ruby evaluation value to the key, see
|
97
|
-
# examples/ruby_conv.rb:
|
98
|
-
#
|
99
|
-
# $ ruby ./examples/ruby_conv.rb
|
100
|
-
# {:r=>1..10, :a=>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}
|
101
|
-
# typeof 'r': Range
|
102
|
-
# typeof 'a': Array
|
103
|
-
#
|
104
|
-
# $ ruby ./examples/ruby_conv.rb -r:2..3 -a:'(-1..1).to_a'
|
105
|
-
# {:r=>2..3, :a=>[-1, 0, 1]}
|
106
|
-
# typeof 'r': Range
|
107
|
-
# typeof 'a': Array
|
108
|
-
#
|
109
|
-
# $ ruby ./examples/ruby_conv.rb -r:2..3 -a:\(-1..1\).to_a
|
110
|
-
# {:r=>2..3, :a=>[-1, 0, 1]}
|
111
|
-
# typeof 'r': Range
|
112
|
-
# typeof 'a': Array
|
113
|
-
#
|
114
|
-
#
|
115
|
-
# have fun
|
116
|
-
# dirk
|
117
|
-
#
|
118
|
-
# vim: set tw=80 syntax=txt nosmartindent:
|