app_conf 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/LICENSE +23 -0
- data/README.md +48 -0
- data/Rakefile +33 -0
- data/app_conf.gemspec +16 -0
- data/lib/app_conf.rb +42 -0
- data/spec/app_conf_spec.rb +48 -0
- data/spec/config.yml +5 -0
- data/spec/other.yml +4 -0
- metadata +76 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright (c) 2011 Phil Thompson
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
AppConf
|
2
|
+
=======
|
3
|
+
Simplest YAML Backed Application Configuration
|
4
|
+
|
5
|
+
Installation
|
6
|
+
----------------------------------
|
7
|
+
gem install app_conf
|
8
|
+
|
9
|
+
Usage
|
10
|
+
----------------------------------
|
11
|
+
config.yml
|
12
|
+
---
|
13
|
+
fullname: Joe Bloggs
|
14
|
+
user:
|
15
|
+
name: Joe
|
16
|
+
|
17
|
+
other.yml
|
18
|
+
---
|
19
|
+
user:
|
20
|
+
address:
|
21
|
+
street: 1 Some Road
|
22
|
+
|
23
|
+
Code:
|
24
|
+
AppConf.load('config.yml', 'other.yml')
|
25
|
+
AppConf.fullname -> 'Joe Blogs'
|
26
|
+
AppConf.user.name -> 'Joe'
|
27
|
+
AppConf.user.address.street -> '1 Some Road'
|
28
|
+
|
29
|
+
Syntax
|
30
|
+
----------------------------------
|
31
|
+
AppConf.load(*filenames)
|
32
|
+
AppConf.multiple.nested.keys
|
33
|
+
AppConf.loaded.from.yaml = 'can override'
|
34
|
+
AppConf.non.existing.value = 'can set'
|
35
|
+
|
36
|
+
Other stuff
|
37
|
+
----------------------------------
|
38
|
+
* Works with Ruby 1.9.2
|
39
|
+
* No gem dependencies
|
40
|
+
* Tested with MiniTest::Spec
|
41
|
+
* Not dependent on Rails but easy to use with it. For example:
|
42
|
+
`AppConf.load('config.yml', "#{Rails.env}.yml")`
|
43
|
+
|
44
|
+
Why
|
45
|
+
----------------------------------
|
46
|
+
* Because I wanted to write the simplest app config possible
|
47
|
+
* Because I can
|
48
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
|
3
|
+
desc 'Test, build and install the gem'
|
4
|
+
task :default => [:spec, :install]
|
5
|
+
|
6
|
+
Rake::TestTask.new(:spec) do |t|
|
7
|
+
t.pattern = 'spec/*_spec.rb'
|
8
|
+
end
|
9
|
+
|
10
|
+
desc 'Build and install the gem'
|
11
|
+
task :install do
|
12
|
+
gemspec_path = Dir['*.gemspec'].first
|
13
|
+
spec = eval(File.read(gemspec_path))
|
14
|
+
|
15
|
+
result = `gem build #{gemspec_path} 2>&1`
|
16
|
+
if result =~ /Successfully built/
|
17
|
+
system "gem uninstall -x #{spec.name} 2>&1"
|
18
|
+
system "gem install #{spec.file_name} --no-rdoc --no-ri 2>&1"
|
19
|
+
else
|
20
|
+
raise result
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
desc 'Take the version in the gemspec, create a git tag and send the gem to rubygems'
|
25
|
+
task :release do
|
26
|
+
gemspec_path = Dir['*.gemspec'].first
|
27
|
+
spec = eval(File.read(gemspec_path))
|
28
|
+
|
29
|
+
system "git tag -f -a v#{spec.version} -m 'Version #{spec.version}'"
|
30
|
+
system "git push --tags"
|
31
|
+
system "gem push #{spec.file_name}"
|
32
|
+
end
|
33
|
+
|
data/app_conf.gemspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'base64'
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'app_conf'
|
5
|
+
s.version = '0.1.0'
|
6
|
+
s.authors = 'Phil Thompson'
|
7
|
+
s.email = Base64.decode64("cGhpbEBlbGVjdHJpY3Zpc2lvbnMuY29t\n")
|
8
|
+
s.summary = 'Simplest YAML Backed Application Configuration'
|
9
|
+
s.required_rubygems_version = '>= 1.3.6'
|
10
|
+
|
11
|
+
s.files = `git ls-files`.split("\n")
|
12
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
13
|
+
|
14
|
+
s.require_path = 'lib'
|
15
|
+
end
|
16
|
+
|
data/lib/app_conf.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
class AppConf
|
4
|
+
attr_reader :config
|
5
|
+
|
6
|
+
private_class_method :new
|
7
|
+
def initialize
|
8
|
+
@config = {}
|
9
|
+
end
|
10
|
+
@@config = new
|
11
|
+
|
12
|
+
def self.load(*filenames)
|
13
|
+
filenames.each do |filename|
|
14
|
+
recurse(YAML.load(File.open(filename)), @@config)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def method_missing(method, *args, &block)
|
19
|
+
method = method.to_s
|
20
|
+
if method[-1] == '='
|
21
|
+
method = method[0..-2]
|
22
|
+
raise "Not allowed to overwrite nested entities" if @config[method].is_a?(AppConf)
|
23
|
+
@config[method] = args.first
|
24
|
+
else
|
25
|
+
@config[method]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.method_missing(method, *args, &block)
|
30
|
+
@@config.send(method, *args)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
def self.recurse(yaml, app_config)
|
35
|
+
yaml.each do |k, v|
|
36
|
+
v = recurse(v, app_config.send(k) || new) if v.is_a?(Hash)
|
37
|
+
app_config.config[k] = v
|
38
|
+
end
|
39
|
+
app_config
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
$LOAD_PATH << 'lib'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'app_conf'
|
4
|
+
|
5
|
+
describe AppConf do
|
6
|
+
before(:each) do
|
7
|
+
@dir = File.dirname(__FILE__)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'cannot be instantiated directly' do
|
11
|
+
AppConf.new.must_be_nil
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'works with dot notation' do
|
15
|
+
AppConf.load("#{@dir}/config.yml")
|
16
|
+
AppConf.fullname.must_equal 'Joe Bloggs'
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'works with nested dot notation' do
|
20
|
+
AppConf.load("#{@dir}/config.yml")
|
21
|
+
AppConf.user.name.first.must_equal 'Joe'
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'works with multiple files' do
|
25
|
+
AppConf.load("#{@dir}/config.yml", "#{@dir}/other.yml")
|
26
|
+
AppConf.user.address.street.must_equal '1 Some Road'
|
27
|
+
AppConf.user.name.first.must_equal 'Joe'
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'allows additional keys to be set' do
|
31
|
+
AppConf.load("#{@dir}/config.yml")
|
32
|
+
AppConf.user.name.last = 'Bloggs'
|
33
|
+
AppConf.user.name.last.must_equal 'Bloggs'
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'allows existing keys to be overridden' do
|
37
|
+
AppConf.load("#{@dir}/config.yml")
|
38
|
+
AppConf.user.name.first = 'Jody'
|
39
|
+
AppConf.user.name.first.must_equal 'Jody'
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'does not allow nested items to be overwritten' do
|
43
|
+
AppConf.load("#{@dir}/config.yml")
|
44
|
+
lambda { AppConf.user.name = 'something' }.must_raise RuntimeError
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
data/spec/config.yml
ADDED
data/spec/other.yml
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: app_conf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Phil Thompson
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-02-27 00:00:00 +00:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description:
|
22
|
+
email: phil@electricvisions.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- .gitignore
|
31
|
+
- LICENSE
|
32
|
+
- README.md
|
33
|
+
- Rakefile
|
34
|
+
- app_conf.gemspec
|
35
|
+
- lib/app_conf.rb
|
36
|
+
- spec/app_conf_spec.rb
|
37
|
+
- spec/config.yml
|
38
|
+
- spec/other.yml
|
39
|
+
has_rdoc: true
|
40
|
+
homepage:
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
segments:
|
62
|
+
- 1
|
63
|
+
- 3
|
64
|
+
- 6
|
65
|
+
version: 1.3.6
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.7
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Simplest YAML Backed Application Configuration
|
73
|
+
test_files:
|
74
|
+
- spec/app_conf_spec.rb
|
75
|
+
- spec/config.yml
|
76
|
+
- spec/other.yml
|