citrusbyte-settings 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,19 @@
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 ADDED
@@ -0,0 +1,83 @@
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::SettingMissing` 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 ADDED
@@ -0,0 +1,35 @@
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/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'rails'
data/lib/rails.rb ADDED
@@ -0,0 +1,6 @@
1
+ # Rails-specific initialization, only here because if you try to set a global
2
+ # constant in init.rb it gets removed, but if you require a file that sets a
3
+ # global constant it seems to work -- not sure why?
4
+
5
+ require 'settings_hash'
6
+ Settings = SettingsHash.new(File.join(Rails.root, 'config', 'settings.yml'), Rails.env)
@@ -0,0 +1,12 @@
1
+ class ReadonlyHash < Hash
2
+ class << self
3
+ def [](*args)
4
+ new(super(*args))
5
+ end
6
+ end
7
+
8
+ def initialize(hash)
9
+ update hash
10
+ freeze
11
+ end
12
+ end
@@ -0,0 +1,34 @@
1
+ require 'readonly_hash'
2
+ require 'yaml'
3
+
4
+ class SettingsHash < ReadonlyHash
5
+ class SettingMissing < StandardError;end;
6
+
7
+ def initialize(path, namespace=nil)
8
+ raise "No settings file found: #{path}" unless File.exists?(path)
9
+ settings = YAML.load_file(path)
10
+
11
+ if namespace
12
+ raise "No settings defined for #{namespace} in settings file: #{path}" unless settings[namespace]
13
+ settings = settings[namespace]
14
+ end
15
+
16
+ super(settings.symbolize_keys)
17
+ end
18
+
19
+ def [](key)
20
+ raise SettingMissing.new("No setting found for #{key}") unless has_key?(key)
21
+ super
22
+ end
23
+ end
24
+
25
+ class Hash
26
+ # from ActiveSupport
27
+ # lib/active_support/core_ext/hash/keys.rb line 22-27
28
+ def symbolize_keys
29
+ inject({}) do |options, (key, value)|
30
+ options[(key.to_sym rescue key) || key] = value
31
+ options
32
+ end
33
+ end
34
+ end
File without changes
@@ -0,0 +1 @@
1
+ baz: bang
@@ -0,0 +1,4 @@
1
+ test:
2
+ foo: bar
3
+ abc:
4
+ def: 123
@@ -0,0 +1,64 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'contest'
4
+ require File.dirname(__FILE__) + "/../lib/settings_hash"
5
+
6
+ def fixture_path
7
+ File.join(File.dirname(__FILE__), 'fixtures')
8
+ end
9
+
10
+ class SettingsTest < Test::Unit::TestCase
11
+ context "settings file exists" do
12
+ context "and has values" do
13
+ context "in test namespace" do
14
+ context "and namespace exists" do
15
+ setup do
16
+ @settings = SettingsHash.new(File.join(fixture_path, 'settings.yml'), 'test')
17
+ end
18
+
19
+ test "should return hash of settings" do
20
+ assert_equal({ :foo => 'bar', :abc => { 'def' => 123 } }, @settings)
21
+ end
22
+
23
+ test "should freeze settings" do
24
+ assert_raise TypeError do
25
+ @settings[:foo] = 'baz'
26
+ end
27
+ end
28
+
29
+ test "should return value for key if set" do
30
+ assert_equal 'bar', @settings[:foo]
31
+ end
32
+
33
+ test "should raise if key is not set" do
34
+ assert_raise SettingsHash::SettingMissing do
35
+ @settings[:bar]
36
+ end
37
+ end
38
+ end
39
+
40
+ context "and namespace doesnt exist" do
41
+ test "should raise" do
42
+ assert_raise RuntimeError do
43
+ SettingsHash.new(File.join(fixture_path, 'settings.yml'), 'foo')
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ context "outside of test namespace" do
50
+ test "should return hash of settings" do
51
+ assert_equal({ :baz => 'bang' }, SettingsHash.new(File.join(fixture_path, 'no_namespace.yml')))
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ context "settings file doesnt exist" do
58
+ test "should raise" do
59
+ assert_raise RuntimeError do
60
+ SettingsHash.new(File.join(fixture_path, 'missing.yml'))
61
+ end
62
+ end
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: citrusbyte-settings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Alavi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-17 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: ben.alavi@citrusbyte.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - init.rb
26
+ - README.markdown
27
+ - LICENSE
28
+ - Rakefile
29
+ - lib/rails.rb
30
+ - lib/settings_hash.rb
31
+ - lib/readonly_hash.rb
32
+ - test/settings_test.rb
33
+ - test/fixtures/empty.yml
34
+ - test/fixtures/no_namespace.yml
35
+ - test/fixtures/settings.yml
36
+ has_rdoc: false
37
+ homepage: http://github.com/citrusbyte/settings
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: A special hash for application-wide settings
62
+ test_files: []
63
+