cjbottaro-app_config 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +8 -0
- data/VERSION.yml +2 -2
- data/app_config.gemspec +56 -0
- data/init.rb +4 -0
- data/install.rb +1 -0
- data/lib/app_config.rb +1 -17
- data/lib/closed_struct.rb +26 -5
- data/tasks/app_config_tasks.rake +4 -0
- data/test/closed_struct_test.rb +33 -0
- data/uninstall.rb +1 -0
- metadata +12 -5
data/CHANGELOG
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
10/03/2008
|
2
|
+
* recursively merge the configuration hashes
|
3
|
+
|
4
|
+
07/01/2009
|
5
|
+
* Packaged as a gem (but still works as a Rails plugin).
|
6
|
+
* The app config object is now an instance of ApplicationConfiguration.
|
7
|
+
* NoMethodError raised if you try to access a config element that doesn't exist.
|
8
|
+
* ApplicationConfiguration#reload! to reread the config files and rebuild the app config object.
|
data/VERSION.yml
CHANGED
data/app_config.gemspec
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{app_config}
|
8
|
+
s.version = "1.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Christopher J Bottaro"]
|
12
|
+
s.date = %q{2009-08-27}
|
13
|
+
s.description = %q{Application level configuration that supports YAML config file, inheritance, ERB, and object member notation.}
|
14
|
+
s.email = %q{cjbottaro@alumni.cs.utexas.edu}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
"CHANGELOG",
|
20
|
+
"README.rdoc",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION.yml",
|
23
|
+
"app_config.gemspec",
|
24
|
+
"init.rb",
|
25
|
+
"install.rb",
|
26
|
+
"lib/app_config.rb",
|
27
|
+
"lib/closed_struct.rb",
|
28
|
+
"tasks/app_config_tasks.rake",
|
29
|
+
"test/app_config.yml",
|
30
|
+
"test/app_config_test.rb",
|
31
|
+
"test/closed_struct_test.rb",
|
32
|
+
"test/development.yml",
|
33
|
+
"test/empty1.yml",
|
34
|
+
"test/empty2.yml",
|
35
|
+
"uninstall.rb"
|
36
|
+
]
|
37
|
+
s.homepage = %q{http://github.com/cjbottaro/app_config}
|
38
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
39
|
+
s.require_paths = ["lib"]
|
40
|
+
s.rubygems_version = %q{1.3.3}
|
41
|
+
s.summary = %q{Application level configuration.}
|
42
|
+
s.test_files = [
|
43
|
+
"test/app_config_test.rb",
|
44
|
+
"test/closed_struct_test.rb"
|
45
|
+
]
|
46
|
+
|
47
|
+
if s.respond_to? :specification_version then
|
48
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
49
|
+
s.specification_version = 3
|
50
|
+
|
51
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
52
|
+
else
|
53
|
+
end
|
54
|
+
else
|
55
|
+
end
|
56
|
+
end
|
data/init.rb
ADDED
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
data/lib/app_config.rb
CHANGED
@@ -22,7 +22,7 @@ class ApplicationConfiguration
|
|
22
22
|
conf1 = load_conf_file(@conf_path_1)
|
23
23
|
conf2 = load_conf_file(@conf_path_2)
|
24
24
|
conf = recursive_merge(conf1, conf2)
|
25
|
-
@config =
|
25
|
+
@config = ClosedStruct.r_new(conf)
|
26
26
|
end
|
27
27
|
|
28
28
|
private
|
@@ -42,22 +42,6 @@ private
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
# Recursively converts Hashes to ClosedStructs (including Hashes inside Arrays)
|
46
|
-
def convert(h) #:nodoc:
|
47
|
-
s = ClosedStruct.new(h.keys)
|
48
|
-
h.each do |k, v|
|
49
|
-
if v.instance_of?(Hash)
|
50
|
-
s.send( (k+'=').to_sym, convert(v))
|
51
|
-
elsif v.instance_of?(Array)
|
52
|
-
converted_array = v.collect { |e| e.instance_of?(Hash) ? convert(e) : e }
|
53
|
-
s.send( (k+'=').to_sym, converted_array)
|
54
|
-
else
|
55
|
-
s.send( (k+'=').to_sym, v)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
s
|
59
|
-
end
|
60
|
-
|
61
45
|
# Recursively merges hashes. h2 will overwrite h1.
|
62
46
|
def recursive_merge(h1, h2) #:nodoc:
|
63
47
|
h1.merge(h2){ |k, v1, v2| v2.kind_of?(Hash) ? recursive_merge(v1, v2) : v2 }
|
data/lib/closed_struct.rb
CHANGED
@@ -1,13 +1,22 @@
|
|
1
1
|
require 'ostruct'
|
2
2
|
|
3
|
-
|
3
|
+
# Like OpenStruct, but raises an exception if you try to access a member that wasn't specified in the initializer.
|
4
|
+
class ClosedStruct < OpenStruct
|
5
|
+
|
6
|
+
def self.r_new(hash)
|
7
|
+
closed_struct = ClosedStruct.new(hash)
|
8
|
+
closed_struct.send(:recursive_initialize)
|
9
|
+
closed_struct
|
10
|
+
end
|
4
11
|
|
5
12
|
def initialize(*args)
|
6
|
-
if args.length == 1
|
13
|
+
if args.length == 1 and args.first.kind_of?(Hash)
|
7
14
|
super(args.first)
|
15
|
+
elsif args.length > 1 and args.all?{ |arg| [Symbol, String].include?(arg.class) }
|
16
|
+
args = args.inject({}){ |memo, arg| memo[arg.to_sym] = nil; memo }
|
17
|
+
super(args)
|
8
18
|
else
|
9
|
-
|
10
|
-
super(h)
|
19
|
+
raise ArgumentError, "invalid arguments: #{args.inspect}"
|
11
20
|
end
|
12
21
|
@closed = true
|
13
22
|
end
|
@@ -36,4 +45,16 @@ class ClosedStruct < OpenStruct # :nodoc:
|
|
36
45
|
@table.dup
|
37
46
|
end
|
38
47
|
|
39
|
-
|
48
|
+
private
|
49
|
+
|
50
|
+
def recursive_initialize
|
51
|
+
@table.each do |k, v|
|
52
|
+
if v.kind_of?(Hash)
|
53
|
+
@table[k] = ClosedStruct.r_new(v)
|
54
|
+
elsif v.kind_of?(Array)
|
55
|
+
@table[k] = v.collect{ |e| e.kind_of?(Hash) ? ClosedStruct.r_new(e) : e }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'app_config'
|
3
|
+
|
4
|
+
class ClosedStructTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_from_hash
|
7
|
+
s = ClosedStruct.new :a => "a", "b" => "b", :c => 123
|
8
|
+
assert_equal "a", s.a
|
9
|
+
assert_equal "b", s.b
|
10
|
+
assert_equal 123, s.c
|
11
|
+
assert_raise(NoMethodError){ s.d }
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_from_array
|
15
|
+
s = ClosedStruct.new :a, :b, :c
|
16
|
+
s.b = "b"
|
17
|
+
assert_nil s.a
|
18
|
+
assert_equal "b", s.b
|
19
|
+
assert_nil s.c
|
20
|
+
assert_raise(NoMethodError){ s.d }
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_nested_hash
|
24
|
+
s = ClosedStruct.r_new :a => :a, :b => { :c => :c }, :d => :d
|
25
|
+
assert_equal :c, s.b.c
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_nested_hashes_in_array
|
29
|
+
s = ClosedStruct.r_new :a => :a, :b => [ {:c => :c }, { :d => :d } ], :e => :e
|
30
|
+
assert_equal :c, s.b[0].c
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cjbottaro-app_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christopher J Bottaro
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-08-27 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -22,19 +22,25 @@ extensions: []
|
|
22
22
|
extra_rdoc_files:
|
23
23
|
- README.rdoc
|
24
24
|
files:
|
25
|
+
- CHANGELOG
|
25
26
|
- README.rdoc
|
26
27
|
- Rakefile
|
27
28
|
- VERSION.yml
|
29
|
+
- app_config.gemspec
|
30
|
+
- init.rb
|
31
|
+
- install.rb
|
28
32
|
- lib/app_config.rb
|
29
33
|
- lib/closed_struct.rb
|
34
|
+
- tasks/app_config_tasks.rake
|
30
35
|
- test/app_config.yml
|
31
36
|
- test/app_config_test.rb
|
37
|
+
- test/closed_struct_test.rb
|
32
38
|
- test/development.yml
|
33
39
|
- test/empty1.yml
|
34
40
|
- test/empty2.yml
|
35
|
-
|
41
|
+
- uninstall.rb
|
42
|
+
has_rdoc: false
|
36
43
|
homepage: http://github.com/cjbottaro/app_config
|
37
|
-
licenses:
|
38
44
|
post_install_message:
|
39
45
|
rdoc_options:
|
40
46
|
- --charset=UTF-8
|
@@ -55,9 +61,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
61
|
requirements: []
|
56
62
|
|
57
63
|
rubyforge_project:
|
58
|
-
rubygems_version: 1.
|
64
|
+
rubygems_version: 1.2.0
|
59
65
|
signing_key:
|
60
66
|
specification_version: 3
|
61
67
|
summary: Application level configuration.
|
62
68
|
test_files:
|
63
69
|
- test/app_config_test.rb
|
70
|
+
- test/closed_struct_test.rb
|