settings 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +133 -0
- data/lib/settings.rb +50 -9
- data/rakefile +7 -0
- data/settings.gemspec +23 -0
- data/test/fixtures/no_namespace.yml +1 -1
- data/test/fixtures/settings.yml +1 -1
- data/test/settings_test.rb +68 -69
- metadata +43 -43
- data/LICENSE +0 -19
- data/README.markdown +0 -83
- data/Rakefile +0 -35
- data/lib/settings_hash.rb +0 -60
- data/rails/init.rb +0 -1
- data/test/fixtures/empty.yml +0 -0
data/README.md
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
Settings
|
2
|
+
========
|
3
|
+
|
4
|
+
A special hash for application-wide settings.
|
5
|
+
|
6
|
+
Description
|
7
|
+
-----------
|
8
|
+
|
9
|
+
Small and simple specialized Hash which is helpful for storing immutable,
|
10
|
+
required key/value pairs to be loaded from a YAML file.
|
11
|
+
|
12
|
+
New instances of `Settings::Hash` are readonly -- any attempt to write to the
|
13
|
+
Hash after initialization will fail with a `TypeError`. This way you can avoid
|
14
|
+
inadvertently altering the expected value of a setting at runtime.
|
15
|
+
|
16
|
+
Any attempt to read a key which is not set will raise a
|
17
|
+
`Settings::Hash::SettingNotFound` error. This way expected values will raise when
|
18
|
+
expected, making them easier to track down than if they had silently returned
|
19
|
+
`nil`.
|
20
|
+
|
21
|
+
`Settings::Hash` supports an optional namespace which can be used to define
|
22
|
+
multiple groups of settings within a single .yml file. Commonly this can be
|
23
|
+
used to define per-environment settings, i.e. "test" and "development".
|
24
|
+
|
25
|
+
Finally, there is a small module which you can use in your Rack applications to
|
26
|
+
provide some sensible defaults for loading a settings file.
|
27
|
+
|
28
|
+
Usage
|
29
|
+
-----
|
30
|
+
|
31
|
+
`config/settings.yml`
|
32
|
+
|
33
|
+
test:
|
34
|
+
:foo: 123456
|
35
|
+
development:
|
36
|
+
:foo: "abcdef"
|
37
|
+
|
38
|
+
Settings::Hash:
|
39
|
+
|
40
|
+
settings = Settings::Hash.new("config/settings.yml")
|
41
|
+
settings["test"][:foo] #=> 123456
|
42
|
+
settings["development"][:foo] #=> "abcdef"
|
43
|
+
|
44
|
+
settings = Settings::Hash.new("config/settings.yml", "test")
|
45
|
+
settings[:foo] #=> 123456
|
46
|
+
|
47
|
+
ENV["RACK_ENV"] = "development"
|
48
|
+
settings = Settings::Hash.new("config/settings.yml", ENV["RACK_ENV"])
|
49
|
+
settings[:foo] #=> "abcdef"
|
50
|
+
|
51
|
+
Cuba:
|
52
|
+
|
53
|
+
Cuba.send :extend, Settings
|
54
|
+
|
55
|
+
Cuba.define do
|
56
|
+
on "/" do
|
57
|
+
res.write Cuba.setting(:foo) #=> "abcdef"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
Sinatra:
|
62
|
+
|
63
|
+
class MyApplication < Sinatra::Base
|
64
|
+
extend Settings
|
65
|
+
|
66
|
+
get "/" do
|
67
|
+
MyApplication.setting(:foo) #=> "abcdef"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
Rails:
|
72
|
+
|
73
|
+
gem "settings", "0.0.4" # (in `Gemfile`)
|
74
|
+
|
75
|
+
Rails.send :extend, Settings # (in `config/initializers/settings.rb`)
|
76
|
+
|
77
|
+
class WidgetController < ActionController::Base
|
78
|
+
def index
|
79
|
+
Rails.setting("foo") #=> "abcdef"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
Installation
|
84
|
+
------------
|
85
|
+
|
86
|
+
$ sudo gem install settings
|
87
|
+
|
88
|
+
Test
|
89
|
+
----
|
90
|
+
|
91
|
+
$ rake
|
92
|
+
|
93
|
+
Changelog
|
94
|
+
---------
|
95
|
+
|
96
|
+
### 0.0.4
|
97
|
+
|
98
|
+
Settings no longer symbolizes keys. If you want to use symbols for keys, define
|
99
|
+
your keys as symbols in your `.yml` file, i.e.:
|
100
|
+
|
101
|
+
:test:
|
102
|
+
:alphabet: "abcdef"
|
103
|
+
|
104
|
+
instead of:
|
105
|
+
|
106
|
+
test:
|
107
|
+
alphabet: "abcdef"
|
108
|
+
|
109
|
+
Settings no longer explicitly supports Rails -- instead it comes with some
|
110
|
+
defaults for Rack applications which could be used with Rails.
|
111
|
+
|
112
|
+
License
|
113
|
+
-------
|
114
|
+
|
115
|
+
Copyright (c) 2009-2011 Ben Alavi for Citrusbyte
|
116
|
+
|
117
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
118
|
+
of this software and associated documentation files (the "Software"), to deal
|
119
|
+
in the Software without restriction, including without limitation the rights
|
120
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
121
|
+
copies of the Software, and to permit persons to whom the Software is
|
122
|
+
furnished to do so, subject to the following conditions:
|
123
|
+
|
124
|
+
The above copyright notice and this permission notice shall be included in
|
125
|
+
all copies or substantial portions of the Software.
|
126
|
+
|
127
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
128
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
129
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
130
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
131
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
132
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
133
|
+
THE SOFTWARE.
|
data/lib/settings.rb
CHANGED
@@ -1,9 +1,50 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
1
|
+
require "yaml"
|
2
|
+
|
3
|
+
module Settings
|
4
|
+
VERSION = "0.0.4"
|
5
|
+
|
6
|
+
def self.extended(base)
|
7
|
+
base.instance_variable_set "@_settings", Hash.new(File.join(Dir.pwd, "config", "settings.yml"), ENV["RACK_ENV"])
|
8
|
+
end
|
9
|
+
|
10
|
+
def setting(key)
|
11
|
+
@_settings[key]
|
12
|
+
end
|
13
|
+
|
14
|
+
class Hash < ::Hash
|
15
|
+
# Raised when attempting to access a key which has not been set.
|
16
|
+
class SettingNotFound < StandardError;end;
|
17
|
+
|
18
|
+
# Creates a new Settings::Hash from a YAML file located at the given path.
|
19
|
+
#
|
20
|
+
# Optionally loads only the settings within the given namespace (if a
|
21
|
+
# namespace is given)
|
22
|
+
#
|
23
|
+
# Settings::Hash.new('/path/to/settings.yml') => { 'foo' => { :bar => 'baz' }, :bam => 'bang' }
|
24
|
+
# Settings::Hash.new('/path/to/settings.yml, 'foo') => { :bar => 'baz' }
|
25
|
+
#
|
26
|
+
def initialize(path, namespace=nil)
|
27
|
+
begin
|
28
|
+
settings = YAML.load_file(path)
|
29
|
+
rescue Errno::ENOENT => e
|
30
|
+
e.message << " (attempting to load settings file)"
|
31
|
+
raise e
|
32
|
+
end
|
33
|
+
|
34
|
+
if namespace
|
35
|
+
raise "No settings defined for #{namespace} in settings file: #{path}" unless settings[namespace]
|
36
|
+
settings = settings[namespace]
|
37
|
+
end
|
38
|
+
|
39
|
+
update settings
|
40
|
+
freeze
|
41
|
+
end
|
42
|
+
|
43
|
+
# Access the value at the given key, raises Settings::Hash::SettingNotFound
|
44
|
+
# if the key is not set.
|
45
|
+
def [](key)
|
46
|
+
raise SettingNotFound.new("No setting found for #{key.inspect}") unless has_key?(key)
|
47
|
+
super
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/rakefile
ADDED
data/settings.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.expand_path("lib/settings", File.dirname(__FILE__))
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "settings"
|
5
|
+
s.version = Settings::VERSION
|
6
|
+
s.summary = "A special hash for application-wide settings"
|
7
|
+
s.description = "Small and simple specialized Hash which is helpful for storing immutable, required key/value pairs to be loaded from a YAML file."
|
8
|
+
s.date = "2011-10-25"
|
9
|
+
s.authors = ["Ben Alavi"]
|
10
|
+
s.email = ["ben.alavi@citrusbyte.com"]
|
11
|
+
s.homepage = "http://github.com/citrusbyte/settings"
|
12
|
+
|
13
|
+
s.files = Dir[
|
14
|
+
"README.md",
|
15
|
+
"rakefile",
|
16
|
+
"settings.gemspec",
|
17
|
+
"lib/**/*.rb",
|
18
|
+
"test/**/*.rb",
|
19
|
+
"test/fixtures/*.yml"
|
20
|
+
]
|
21
|
+
|
22
|
+
s.add_development_dependency "contest"
|
23
|
+
end
|
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
:foo: "bar"
|
data/test/fixtures/settings.yml
CHANGED
data/test/settings_test.rb
CHANGED
@@ -1,80 +1,79 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
1
|
+
$: << File.expand_path("../lib", File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
require "test/unit"
|
5
|
+
require "contest"
|
6
|
+
require "settings"
|
5
7
|
|
6
8
|
def fixture_path
|
7
|
-
File.join(File.dirname(__FILE__),
|
9
|
+
File.join(File.dirname(__FILE__), "fixtures")
|
10
|
+
end
|
11
|
+
|
12
|
+
class Application
|
13
|
+
end
|
14
|
+
|
15
|
+
class Widget
|
8
16
|
end
|
9
17
|
|
10
18
|
class SettingsTest < Test::Unit::TestCase
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
should "set nested values" do
|
32
|
-
assert_equal 123, @settings[:abc][:def]
|
33
|
-
end
|
34
|
-
|
35
|
-
should "have bar for :foo" do
|
36
|
-
assert_equal 'bar', @settings[:foo]
|
37
|
-
end
|
38
|
-
|
39
|
-
should "freeze settings" do
|
40
|
-
assert_raise TypeError do
|
41
|
-
@settings[:foo] = 'baz'
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
should "return value for key if set" do
|
46
|
-
assert_equal 'bar', @settings[:foo]
|
47
|
-
end
|
48
|
-
|
49
|
-
should "raise if key is not set" do
|
50
|
-
assert_raise SettingsHash::SettingNotFound do
|
51
|
-
@settings[:bar]
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
context "and namespace doesnt exist" do
|
57
|
-
should "raise" do
|
58
|
-
assert_raise RuntimeError do
|
59
|
-
SettingsHash.new(File.join(fixture_path, 'settings.yml'), 'foo')
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
context "outside of test namespace" do
|
66
|
-
should "return hash of settings" do
|
67
|
-
assert_equal({ :baz => 'bang' }, SettingsHash.new(File.join(fixture_path, 'no_namespace.yml')))
|
68
|
-
end
|
69
|
-
end
|
19
|
+
should "return hash of all settings when no namespace is present" do
|
20
|
+
assert_equal({ :foo => "bar" }, Settings::Hash.new(File.join(fixture_path, "no_namespace.yml")))
|
21
|
+
end
|
22
|
+
|
23
|
+
should "load all settings" do
|
24
|
+
settings = Settings::Hash.new File.join(fixture_path, "settings.yml")
|
25
|
+
assert_equal({ "test" => { :foo => "bar", "abc" => { "def" => 123 } } }, settings)
|
26
|
+
end
|
27
|
+
|
28
|
+
should "only load settings in given namespace" do
|
29
|
+
settings = Settings::Hash.new(File.join(fixture_path, "settings.yml"), "test")
|
30
|
+
assert_equal({ :foo => "bar", "abc" => { "def" => 123 } }, settings)
|
31
|
+
end
|
32
|
+
|
33
|
+
should "raise when attempting to change a setting" do
|
34
|
+
settings = Settings::Hash.new File.join(fixture_path, "settings.yml")
|
35
|
+
assert_raise TypeError do
|
36
|
+
settings[:foo] = "baz"
|
70
37
|
end
|
71
38
|
end
|
72
39
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
end
|
40
|
+
should "raise when attempting to add a setting" do
|
41
|
+
settings = Settings::Hash.new File.join(fixture_path, "settings.yml")
|
42
|
+
assert_raise TypeError do
|
43
|
+
settings[:bar] = "bam"
|
78
44
|
end
|
79
45
|
end
|
46
|
+
|
47
|
+
should "raise if key is not set" do
|
48
|
+
settings = Settings::Hash.new File.join(fixture_path, "settings.yml")
|
49
|
+
assert_raise Settings::Hash::SettingNotFound do
|
50
|
+
settings[:bar]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
should "raise when settings file does not exist" do
|
55
|
+
assert_raise Errno::ENOENT do
|
56
|
+
Settings::Hash.new(File.join(fixture_path, "missing.yml"))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
should "raise when namespace doesn't exit" do
|
61
|
+
assert_raise RuntimeError do
|
62
|
+
Settings::Hash.new(File.join(fixture_path, 'settings.yml'), "foo")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
should "load settings from config/settings.yml when extended" do
|
67
|
+
ENV["RACK_ENV"] = "test"
|
68
|
+
|
69
|
+
FileUtils.rm "config/settings.yml"
|
70
|
+
FileUtils.rmdir "config"
|
71
|
+
FileUtils.mkdir "config"
|
72
|
+
FileUtils.cp "test/fixtures/settings.yml", "config/settings.yml"
|
73
|
+
|
74
|
+
assert !Application.respond_to?(:setting)
|
75
|
+
|
76
|
+
Application.send :extend, Settings
|
77
|
+
assert_equal "bar", Application.setting(:foo)
|
78
|
+
end
|
80
79
|
end
|
metadata
CHANGED
@@ -1,64 +1,64 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: settings
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
|
-
authors:
|
7
|
+
authors:
|
7
8
|
- Ben Alavi
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
12
|
+
date: 2011-10-25 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: contest
|
16
|
+
requirement: &70237296076480 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70237296076480
|
25
|
+
description: Small and simple specialized Hash which is helpful for storing immutable,
|
26
|
+
required key/value pairs to be loaded from a YAML file.
|
27
|
+
email:
|
28
|
+
- ben.alavi@citrusbyte.com
|
18
29
|
executables: []
|
19
|
-
|
20
30
|
extensions: []
|
21
|
-
|
22
31
|
extra_rdoc_files: []
|
23
|
-
|
24
|
-
|
32
|
+
files:
|
33
|
+
- README.md
|
34
|
+
- rakefile
|
35
|
+
- settings.gemspec
|
25
36
|
- lib/settings.rb
|
26
|
-
-
|
27
|
-
- README.markdown
|
28
|
-
- LICENSE
|
29
|
-
- Rakefile
|
30
|
-
- rails/init.rb
|
31
|
-
- test/fixtures/empty.yml
|
37
|
+
- test/settings_test.rb
|
32
38
|
- test/fixtures/no_namespace.yml
|
33
39
|
- test/fixtures/settings.yml
|
34
|
-
|
35
|
-
has_rdoc: true
|
36
|
-
homepage: http://labs.citrusbyte.com/projects/settings
|
40
|
+
homepage: http://github.com/citrusbyte/settings
|
37
41
|
licenses: []
|
38
|
-
|
39
42
|
post_install_message:
|
40
43
|
rdoc_options: []
|
41
|
-
|
42
|
-
require_paths:
|
44
|
+
require_paths:
|
43
45
|
- lib
|
44
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
56
58
|
requirements: []
|
57
|
-
|
58
|
-
|
59
|
-
rubygems_version: 1.3.4
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.10
|
60
61
|
signing_key:
|
61
62
|
specification_version: 3
|
62
63
|
summary: A special hash for application-wide settings
|
63
64
|
test_files: []
|
64
|
-
|
data/LICENSE
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
Copyright (c) 2009 Ben Alavi for Citrusbyte
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
-
of this software and associated documentation files (the "Software"), to deal
|
5
|
-
in the Software without restriction, including without limitation the rights
|
6
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
-
copies of the Software, and to permit persons to whom the Software is
|
8
|
-
furnished to do so, subject to the following conditions:
|
9
|
-
|
10
|
-
The above copyright notice and this permission notice shall be included in
|
11
|
-
all copies or substantial portions of the Software.
|
12
|
-
|
13
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
-
THE SOFTWARE.
|
data/README.markdown
DELETED
@@ -1,83 +0,0 @@
|
|
1
|
-
Settings
|
2
|
-
========
|
3
|
-
|
4
|
-
A special hash for application-wide settings.
|
5
|
-
|
6
|
-
Description
|
7
|
-
-----------
|
8
|
-
|
9
|
-
Small and simple specialized Hash which is helpful for storing immutable,
|
10
|
-
required key/value pairs to be loaded from a YAML file.
|
11
|
-
|
12
|
-
New instances of `SettingsHash` are readonly -- any attempt to write to the
|
13
|
-
Hash after initialization will fail with a `TypeError`.
|
14
|
-
|
15
|
-
Any attempt to read a key which is not set will raise a
|
16
|
-
`SettingsHash::SettingNotFound` error.
|
17
|
-
|
18
|
-
`SettingsHash` supports an optional namespace which can be used to define
|
19
|
-
multiple groups of settings within a single .yml file. For example, "test" and
|
20
|
-
"development" might be different namespaces for different running environments.
|
21
|
-
|
22
|
-
Usage
|
23
|
-
-----
|
24
|
-
|
25
|
-
### General
|
26
|
-
|
27
|
-
settings = SettingsHash.new('path/to/settings.yml')
|
28
|
-
settings = SettingsHash.new('path/to/settings.yml', 'foo') => loads settings under the 'foo' namespace
|
29
|
-
|
30
|
-
### Rails
|
31
|
-
|
32
|
-
Put your environment-specific `settings.yml` in `config/settings.yml`. It may
|
33
|
-
look something like this:
|
34
|
-
|
35
|
-
development:
|
36
|
-
memcached: true
|
37
|
-
test:
|
38
|
-
memcached: false
|
39
|
-
|
40
|
-
This file will automatically be loaded into a `Settings` global which can be
|
41
|
-
accessed like a normal Hash:
|
42
|
-
|
43
|
-
Settings[:memcached] => true (development environment)
|
44
|
-
Settings[:memcached] => false (test environment)
|
45
|
-
|
46
|
-
Installation
|
47
|
-
------------
|
48
|
-
|
49
|
-
$ gem sources -a http://gems.github.com (you only have to do this once)
|
50
|
-
$ sudo gem install citrusbyte-settings
|
51
|
-
|
52
|
-
Testing
|
53
|
-
-------
|
54
|
-
|
55
|
-
Tests require Contest gem:
|
56
|
-
|
57
|
-
http://github.com/citrusbyte/contest
|
58
|
-
|
59
|
-
License
|
60
|
-
-------
|
61
|
-
|
62
|
-
Copyright (c) 2009 Ben Alavi for Citrusbyte
|
63
|
-
|
64
|
-
Permission is hereby granted, free of charge, to any person
|
65
|
-
obtaining a copy of this software and associated documentation
|
66
|
-
files (the "Software"), to deal in the Software without
|
67
|
-
restriction, including without limitation the rights to use,
|
68
|
-
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
69
|
-
copies of the Software, and to permit persons to whom the
|
70
|
-
Software is furnished to do so, subject to the following
|
71
|
-
conditions:
|
72
|
-
|
73
|
-
The above copyright notice and this permission notice shall be
|
74
|
-
included in all copies or substantial portions of the Software.
|
75
|
-
|
76
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
77
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
78
|
-
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
79
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
80
|
-
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
81
|
-
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
82
|
-
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
83
|
-
OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
require 'rake'
|
2
|
-
require 'rake/gempackagetask'
|
3
|
-
require 'rake/testtask'
|
4
|
-
require 'rake/clean'
|
5
|
-
|
6
|
-
gem_spec_file = 'settings.gemspec'
|
7
|
-
|
8
|
-
gem_spec = eval(File.read(gem_spec_file)) rescue nil
|
9
|
-
|
10
|
-
task :default => :test
|
11
|
-
|
12
|
-
Rake::TestTask.new(:test) do |t|
|
13
|
-
t.pattern = 'test/**/*_test.rb'
|
14
|
-
t.verbose = false
|
15
|
-
end
|
16
|
-
|
17
|
-
Rake::GemPackageTask.new(gem_spec) do |pkg|
|
18
|
-
pkg.need_zip = false
|
19
|
-
pkg.need_tar = false
|
20
|
-
rm_f FileList['pkg/**/*.*']
|
21
|
-
end if gem_spec
|
22
|
-
|
23
|
-
desc "Generate the gemspec file."
|
24
|
-
task :gemspec do
|
25
|
-
require 'erb'
|
26
|
-
|
27
|
-
File.open(gem_spec_file, 'w') do |f|
|
28
|
-
f.write ERB.new(File.read("#{gem_spec_file}.erb")).result(binding)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
desc "Builds and installs the gem."
|
33
|
-
task :install => :repackage do
|
34
|
-
`sudo gem install pkg/#{gem_spec.name}-#{gem_spec.version}.gem`
|
35
|
-
end
|
data/lib/settings_hash.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
|
3
|
-
class Hash
|
4
|
-
# From ActiveSupport lib/active_support/core_ext/hash/keys.rb line 22-27
|
5
|
-
# Altered to recursively symbolize all keys in nested hashes
|
6
|
-
def symbolize_keys
|
7
|
-
inject({}) do |options, (key, value)|
|
8
|
-
options[(key.to_sym rescue key) || key] = (value.is_a?(Hash) ? value.symbolize_keys : value)
|
9
|
-
options
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
# A very simple "readonly" hash implementation. Freezes itselfs on
|
15
|
-
# initialization so that any attempts to change will result in a TypeError
|
16
|
-
class ReadonlyHash < Hash
|
17
|
-
class << self
|
18
|
-
def [](*args)
|
19
|
-
new(super(*args))
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def initialize(hash)
|
24
|
-
update hash
|
25
|
-
freeze
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
class SettingsHash < ReadonlyHash
|
30
|
-
# Raised when attempting to access a key which has not been set.
|
31
|
-
class SettingNotFound < StandardError;end;
|
32
|
-
|
33
|
-
# Creates a new SettingsHash from a YAML file located at the given path.
|
34
|
-
#
|
35
|
-
# Optionally loads only the settings within the given namespace (if a
|
36
|
-
# namespace is given)
|
37
|
-
#
|
38
|
-
# SettingsHash.new('/path/to/settings.yml') => { :foo => { :bar => 'baz' }, :bam => 'bang' }
|
39
|
-
# SettingsHash.new('/path/to/settings.yml, 'foo') => { :bar => 'baz' }
|
40
|
-
#
|
41
|
-
# Note that hash keys are symbolized (as seen in example above)
|
42
|
-
def initialize(path, namespace=nil)
|
43
|
-
raise "No settings file found: #{path}" unless File.exists?(path)
|
44
|
-
settings = YAML.load_file(path)
|
45
|
-
|
46
|
-
if namespace
|
47
|
-
raise "No settings defined for #{namespace} in settings file: #{path}" unless settings[namespace]
|
48
|
-
settings = settings[namespace]
|
49
|
-
end
|
50
|
-
|
51
|
-
super(settings.symbolize_keys)
|
52
|
-
end
|
53
|
-
|
54
|
-
# Access the value at the given key, raises SettingsHash::SettingNotFound if
|
55
|
-
# the key is not set.
|
56
|
-
def [](key)
|
57
|
-
raise SettingNotFound.new("No setting found for #{key}") unless has_key?(key)
|
58
|
-
super
|
59
|
-
end
|
60
|
-
end
|
data/rails/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'settings'
|
data/test/fixtures/empty.yml
DELETED
File without changes
|