cubus-settingslogic 2.1.1 → 2.2.0

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/.gitignore CHANGED
@@ -5,4 +5,5 @@ pkg/*
5
5
  coverage/*
6
6
  doc/*
7
7
  benchmarks/*
8
-
8
+ .rvmrc
9
+ *.gem
@@ -1,3 +1,10 @@
1
+ == 2.2.0 released 2010-09-21
2
+
3
+ * better stacktrace info for injected methods
4
+ * support for numeric keys (by Ignacio Carrera)
5
+ * tests pass on active_support 3
6
+ * tests pass on ruby 1.9.2
7
+
1
8
  == 2.1.1 released 2010-05-03
2
9
 
3
10
  * removed name method due to conflict with ZenTest
@@ -36,9 +36,9 @@ that looks like:
36
36
  end
37
37
 
38
38
  Name it Settings, name it Config, name it whatever you want. Add as many or as few as you like. A good place to put
39
- this file in a rails app is app/models/settings.rb
39
+ this file in a rails app is app/models/settings.rb. Declare your namespace to Rails.env if you use per-environment
40
+ settings. These will be merged with the settings in the default namespace.
40
41
 
41
- I felt adding a settings file in your app was more straightforward, less tricky, and more flexible.
42
42
 
43
43
  === 2. Create your settings
44
44
 
@@ -82,6 +82,9 @@ Using a namespace allows us to change our configuration depending on our environ
82
82
  >> Settings.awesome_setting
83
83
  => "Did you know 5 + 5 = 10?"
84
84
 
85
+ >> Settings.key_by_path "cool.saweet"
86
+ => "nested settings"
87
+
85
88
  You can use these settings anywhere, for example in a model:
86
89
 
87
90
  class Post < ActiveRecord::Base
@@ -114,32 +117,7 @@ Modifying our model example:
114
117
  This would allow you to specify a custom value for per_page just for posts, or
115
118
  to fall back to your default value if not specified.
116
119
 
117
- == Note on Sinatra / Capistrano / Vlad
118
-
119
- Each of these frameworks uses a +set+ convention for settings, which actually defines methods
120
- in the global Object namespace:
121
-
122
- set :application, "myapp" # does "def application" globally
123
-
124
- This can cause collisions with Settingslogic, since those methods are global. Luckily, the
125
- solution is to just add a call to load! in your class:
126
-
127
- class Settings < Settingslogic
128
- source "#{Rails.root}/config/application.yml"
129
- namespace Rails.env
130
- load!
131
- end
132
-
133
- It's probably always safest to add load! to your class, since this guarantees settings will be
134
- loaded at that time, rather than lazily later via method_missing.
135
-
136
- Finally, you can reload all your settings later as well:
137
-
138
- Settings.reload!
139
-
140
- This is useful if you want to support changing your settings YAML without restarting your app.
141
-
142
120
  == Author
143
121
 
144
122
  Copyright (c) 2008-2010 {Ben Johnson}[http://github.com/binarylogic] of {Binary Logic}[http://www.binarylogic.com],
145
- released under the MIT license. Support for optional settings and reloading by {Nate Wiger}[http://nate.wiger.org].
123
+ released under the MIT license. Support for optional settings and reloading by {Nate Wiger}[http://nate.wiger.org].
data/Rakefile CHANGED
@@ -1,3 +1,4 @@
1
+ # vim:fileencoding=utf-8
1
2
  require 'rubygems'
2
3
  require 'rake'
3
4
 
@@ -6,9 +7,9 @@ begin
6
7
  Jeweler::Tasks.new do |gem|
7
8
  gem.name = "cubus-settingslogic"
8
9
  gem.summary = "A simple and straightforward settings solution that uses an ERB enabled YAML file and a singleton design pattern."
9
- gem.email = "bjohnson@binarylogic.com"
10
- gem.homepage = "http://github.com/binarylogic/settingslogic"
11
- gem.authors = ["Ben Johnson of Binary Logic", "Mihai Târnovan of Cubus Arts", "Gabriel Târnovan of Cubus Arts"]
10
+ gem.email = "mihai.tarnovan@cubus.ro"
11
+ gem.homepage = "http://github.com/mtarnovan/settingslogic"
12
+ gem.authors = ["Ben Johnson of Binary Logic", "Mihai Târnovan of Cubus Arts", "Gabriel Târnovan of Cubus Arts", "Ignacio Carrera"]
12
13
  end
13
14
  Jeweler::GemcutterTasks.new
14
15
  rescue LoadError
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 2
3
- :minor: 1
4
- :build:
3
+ :minor: 2
5
4
  :patch: 0
5
+ :build:
@@ -98,6 +98,11 @@ class Settingslogic < Hash
98
98
  create_accessors!
99
99
  end
100
100
 
101
+ def [](key)
102
+ # @settings.key.value or @settings[:key][:value] or @settings['key']['value']
103
+ super(key.to_s)
104
+ end
105
+
101
106
  # Called for dynamically-defined keys, and also the first key deferenced at the top-level, if load! is not used.
102
107
  # Otherwise, create_accessors! (called by new) will have created actual methods for each key.
103
108
  def method_missing(key, *args, &block)
@@ -110,22 +115,19 @@ class Settingslogic < Hash
110
115
  end
111
116
 
112
117
  private
113
- # This handles naming collisions with Sinatra/Vlad/Capistrano. Since these use a set()
114
- # helper that defines methods in Object, ANY method_missing ANYWHERE picks up the Vlad/Sinatra
115
- # settings! So settings.deploy_to title actually calls Object.deploy_to (from set :deploy_to, "host"),
116
- # rather than the app_yml['deploy_to'] hash. Jeezus.
117
118
  def create_accessors!
118
119
  self.each do |key,val|
119
- # Use instance_eval/class_eval because they're actually more efficient than define_method{}
120
- # http://stackoverflow.com/questions/185947/ruby-definemethod-vs-def
121
- # http://bmorearty.wordpress.com/2009/01/09/fun-with-rubys-instance_eval-and-class_eval/
122
- self.class.class_eval <<-EndEval
123
- def #{key}
124
- return @#{key} if @#{key} # cache (performance)
125
- value = fetch('#{key}')
126
- @#{key} = value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value
127
- end
128
- EndEval
120
+ begin
121
+ Kernel.Float(key.to_s) # allow numeric keys
122
+ rescue ArgumentError, TypeError
123
+ self.class.class_eval(<<-EndEval, __FILE__, __LINE__ + 1)
124
+ def #{key}
125
+ return @#{key} if @#{key} # cache (performance)
126
+ value = fetch('#{key}')
127
+ @#{key} = value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value
128
+ end
129
+ EndEval
130
+ end
129
131
  end
130
132
  end
131
133
 
@@ -21,4 +21,8 @@ collides:
21
21
  default_settings4:
22
22
  haskell:
23
23
  paradigm: voodoo
24
- foo: bar
24
+ foo: bar
25
+
26
+ with_numbers:
27
+ 12: 'hello'
28
+ '21': 'bye'
@@ -54,7 +54,16 @@ describe "Settingslogic" do
54
54
  Settings4.haskell.foo.should == 'bar'
55
55
  Settings4.smalltalk.paradigm.should == 'object oriented'
56
56
  end
57
-
57
+
58
+ it "should allow numeric keys" do
59
+ Settings.with_numbers.should == { 12 => 'hello', '21' => 'bye' }
60
+ end
61
+
62
+ it "should allow accessing numeric keys as string and viceversa" do
63
+ Settings.with_numbers[12].should == Settings.with_numbers['12']
64
+ Settings.with_numbers[21].should == Settings.with_numbers['21']
65
+ end
66
+
58
67
  it "should raise a helpful error message" do
59
68
  e = nil
60
69
  begin
@@ -1,7 +1,8 @@
1
1
  require 'spec'
2
2
  require 'rubygems'
3
- require 'ruby-debug' if RUBY_VERSION < '1.9' # ruby-debug does not work on 1.9.1 yet
3
+ require 'ruby-debug'
4
4
  require 'active_support'
5
+ require 'active_support/core_ext/hash'
5
6
 
6
7
  $LOAD_PATH.unshift(File.dirname(__FILE__))
7
8
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
metadata CHANGED
@@ -4,24 +4,25 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 2
7
- - 1
8
- - 1
9
- version: 2.1.1
7
+ - 2
8
+ - 0
9
+ version: 2.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ben Johnson of Binary Logic
13
13
  - "Mihai T\xC3\xA2rnovan of Cubus Arts"
14
14
  - "Gabriel T\xC3\xA2rnovan of Cubus Arts"
15
+ - Ignacio Carrera
15
16
  autorequire:
16
17
  bindir: bin
17
18
  cert_chain: []
18
19
 
19
- date: 2010-03-15 00:00:00 +02:00
20
+ date: 2010-09-21 00:00:00 +03:00
20
21
  default_executable:
21
22
  dependencies: []
22
23
 
23
24
  description:
24
- email: bjohnson@binarylogic.com, mihai.tarnovan@cubus.ro, gabriel.tarnovan@cubus.ro
25
+ email: mihai.tarnovan@cubus.ro
25
26
  executables: []
26
27
 
27
28
  extensions: []
@@ -44,10 +45,11 @@ files:
44
45
  - spec/settings.yml
45
46
  - spec/settings2.rb
46
47
  - spec/settings3.rb
48
+ - spec/settings4.rb
47
49
  - spec/settingslogic_spec.rb
48
50
  - spec/spec_helper.rb
49
51
  has_rdoc: true
50
- homepage: http://github.com/binarylogic/settingslogic
52
+ homepage: http://github.com/mtarnovan/settingslogic
51
53
  licenses: []
52
54
 
53
55
  post_install_message:
@@ -56,6 +58,7 @@ rdoc_options:
56
58
  require_paths:
57
59
  - lib
58
60
  required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
59
62
  requirements:
60
63
  - - ">="
61
64
  - !ruby/object:Gem::Version
@@ -63,6 +66,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
63
66
  - 0
64
67
  version: "0"
65
68
  required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
66
70
  requirements:
67
71
  - - ">="
68
72
  - !ruby/object:Gem::Version
@@ -72,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
76
  requirements: []
73
77
 
74
78
  rubyforge_project:
75
- rubygems_version: 1.3.6
79
+ rubygems_version: 1.3.7
76
80
  signing_key:
77
81
  specification_version: 3
78
82
  summary: A simple and straightforward settings solution that uses an ERB enabled YAML file and a singleton design pattern.