citrusbyte-settings 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,9 @@
1
1
  # Rails-specific initialization, only here because if you try to set a global
2
2
  # constant in init.rb it gets removed, but if you require a file that sets a
3
3
  # global constant it seems to work -- not sure why?
4
-
4
+ #
5
+ # Also, Rails gem-dependencies expect a file with the name of the gem in lib
6
+ # that defines a constant with the same name -- hence we have to file called
7
+ # settings.rb which defines Settings for Rails to be happy =)
5
8
  require 'settings_hash'
6
9
  Settings = SettingsHash.new(File.join(Rails.root, 'config', 'settings.yml'), Rails.env)
@@ -1,9 +1,44 @@
1
- require 'readonly_hash'
2
1
  require 'yaml'
3
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
+
4
29
  class SettingsHash < ReadonlyHash
30
+ # Raised when attempting to access a key which has not been set.
5
31
  class SettingNotFound < StandardError;end;
6
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)
7
42
  def initialize(path, namespace=nil)
8
43
  raise "No settings file found: #{path}" unless File.exists?(path)
9
44
  settings = YAML.load_file(path)
@@ -16,19 +51,10 @@ class SettingsHash < ReadonlyHash
16
51
  super(settings.symbolize_keys)
17
52
  end
18
53
 
54
+ # Access the value at the given key, raises SettingsHash::SettingNotFound if
55
+ # the key is not set.
19
56
  def [](key)
20
57
  raise SettingNotFound.new("No setting found for #{key}") unless has_key?(key)
21
58
  super
22
59
  end
23
60
  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
@@ -0,0 +1 @@
1
+ require 'settings'
@@ -16,21 +16,37 @@ class SettingsTest < Test::Unit::TestCase
16
16
  @settings = SettingsHash.new(File.join(fixture_path, 'settings.yml'), 'test')
17
17
  end
18
18
 
19
- test "should return hash of settings" do
20
- assert_equal({ :foo => 'bar', :abc => { 'def' => 123 } }, @settings)
19
+ should "return hash of settings" do
20
+ assert @settings.is_a?(SettingsHash)
21
21
  end
22
22
 
23
- test "should freeze settings" do
23
+ should "symbolize keys" do
24
+ assert @settings.has_key?(:foo)
25
+ end
26
+
27
+ should "symbolize nested keys" do
28
+ assert @settings[:abc].has_key?(:def)
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
24
40
  assert_raise TypeError do
25
41
  @settings[:foo] = 'baz'
26
42
  end
27
43
  end
28
44
 
29
- test "should return value for key if set" do
45
+ should "return value for key if set" do
30
46
  assert_equal 'bar', @settings[:foo]
31
47
  end
32
48
 
33
- test "should raise if key is not set" do
49
+ should "raise if key is not set" do
34
50
  assert_raise SettingsHash::SettingNotFound do
35
51
  @settings[:bar]
36
52
  end
@@ -38,16 +54,16 @@ class SettingsTest < Test::Unit::TestCase
38
54
  end
39
55
 
40
56
  context "and namespace doesnt exist" do
41
- test "should raise" do
57
+ should "raise" do
42
58
  assert_raise RuntimeError do
43
59
  SettingsHash.new(File.join(fixture_path, 'settings.yml'), 'foo')
44
60
  end
45
61
  end
46
62
  end
47
63
  end
48
-
64
+
49
65
  context "outside of test namespace" do
50
- test "should return hash of settings" do
66
+ should "return hash of settings" do
51
67
  assert_equal({ :baz => 'bang' }, SettingsHash.new(File.join(fixture_path, 'no_namespace.yml')))
52
68
  end
53
69
  end
@@ -55,7 +71,7 @@ class SettingsTest < Test::Unit::TestCase
55
71
  end
56
72
 
57
73
  context "settings file doesnt exist" do
58
- test "should raise" do
74
+ should "raise" do
59
75
  assert_raise RuntimeError do
60
76
  SettingsHash.new(File.join(fixture_path, 'missing.yml'))
61
77
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: citrusbyte-settings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Alavi
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-17 00:00:00 -07:00
12
+ date: 2009-04-25 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -22,19 +22,18 @@ extensions: []
22
22
  extra_rdoc_files: []
23
23
 
24
24
  files:
25
- - init.rb
25
+ - lib/settings.rb
26
+ - lib/settings_hash.rb
26
27
  - README.markdown
27
28
  - LICENSE
28
29
  - Rakefile
29
- - lib/rails.rb
30
- - lib/settings_hash.rb
31
- - lib/readonly_hash.rb
32
- - test/settings_test.rb
30
+ - rails/init.rb
33
31
  - test/fixtures/empty.yml
34
32
  - test/fixtures/no_namespace.yml
35
33
  - test/fixtures/settings.yml
34
+ - test/settings_test.rb
36
35
  has_rdoc: false
37
- homepage: http://github.com/citrusbyte/settings
36
+ homepage: http://labs.citrusbyte.com/projects/settings
38
37
  post_install_message:
39
38
  rdoc_options: []
40
39
 
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require 'rails'
@@ -1,12 +0,0 @@
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