settingslogic 2.0.6 → 2.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'rake'
4
+ gem 'jeweler'
5
+ gem 'rspec', :require => 'spec'
6
+ gem 'ruby-debug19'
data/Gemfile.lock ADDED
@@ -0,0 +1,41 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ archive-tar-minitar (0.5.2)
5
+ columnize (0.3.6)
6
+ diff-lcs (1.1.3)
7
+ git (1.2.5)
8
+ jeweler (1.6.4)
9
+ bundler (~> 1.0)
10
+ git (>= 1.2.5)
11
+ rake
12
+ linecache19 (0.5.12)
13
+ ruby_core_source (>= 0.1.4)
14
+ rake (0.9.2.2)
15
+ rspec (2.8.0)
16
+ rspec-core (~> 2.8.0)
17
+ rspec-expectations (~> 2.8.0)
18
+ rspec-mocks (~> 2.8.0)
19
+ rspec-core (2.8.0)
20
+ rspec-expectations (2.8.0)
21
+ diff-lcs (~> 1.1.2)
22
+ rspec-mocks (2.8.0)
23
+ ruby-debug-base19 (0.11.25)
24
+ columnize (>= 0.3.1)
25
+ linecache19 (>= 0.5.11)
26
+ ruby_core_source (>= 0.1.4)
27
+ ruby-debug19 (0.11.6)
28
+ columnize (>= 0.3.1)
29
+ linecache19 (>= 0.5.11)
30
+ ruby-debug-base19 (>= 0.11.19)
31
+ ruby_core_source (0.1.5)
32
+ archive-tar-minitar (>= 0.5.2)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ jeweler
39
+ rake
40
+ rspec
41
+ ruby-debug19
data/README.rdoc CHANGED
@@ -48,7 +48,7 @@ to a key in the YAML file.
48
48
 
49
49
  Using a namespace allows us to change our configuration depending on our environment:
50
50
 
51
- # app/config/application.yml
51
+ # config/application.yml
52
52
  defaults: &defaults
53
53
  cool:
54
54
  saweet: nested settings
@@ -65,6 +65,12 @@ Using a namespace allows us to change our configuration depending on our environ
65
65
  production:
66
66
  <<: *defaults
67
67
 
68
+ _Note_: Certain Ruby/Bundler versions include a version of the Psych YAML parser which incorrectly handles merges (the `<<` in the example above.)
69
+ If your default settings seem to be overwriting your environment-specific settings, including the following lines in your config/boot.rb file may solve the problem:
70
+
71
+ require 'yaml'
72
+ YAML::ENGINE.yamler= 'syck'
73
+
68
74
  === 3. Access your settings
69
75
 
70
76
  >> Rails.env
@@ -114,6 +120,22 @@ Modifying our model example:
114
120
  This would allow you to specify a custom value for per_page just for posts, or
115
121
  to fall back to your default value if not specified.
116
122
 
123
+ === 5. Suppressing Exceptions Conditionally
124
+
125
+ Raising exceptions for missing settings helps highlight configuration problems. However, in a
126
+ Rails app it may make sense to suppress this in production and return nil for missing settings.
127
+ While it's useful to stop and highlight an error in development or test environments, this is
128
+ often not the right answer for production.
129
+
130
+ class Settings < Settingslogic
131
+ source "#{Rails.root}/config/application.yml"
132
+ namespace Rails.env
133
+ suppress_errors Rails.env.production?
134
+ end
135
+
136
+ >> Settings.non_existent_key
137
+ => nil
138
+
117
139
  == Note on Sinatra / Capistrano / Vlad
118
140
 
119
141
  Each of these frameworks uses a +set+ convention for settings, which actually defines methods
data/Rakefile CHANGED
@@ -27,6 +27,6 @@ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
27
  spec.rcov = true
28
28
  end
29
29
 
30
- task :spec => :check_dependencies
30
+ #task :spec => :check_dependencies
31
31
 
32
32
  task :default => :spec
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
- ---
1
+ ---
2
2
  :major: 2
3
3
  :minor: 0
4
+ :patch: 7
4
5
  :build:
5
- :patch: 6
data/lib/settingslogic.rb CHANGED
@@ -7,7 +7,7 @@ class Settingslogic < Hash
7
7
 
8
8
  class << self
9
9
  def name # :nodoc:
10
- instance.key?("name") ? instance.name : super
10
+ self.superclass != Hash && instance.key?("name") ? instance.name : super
11
11
  end
12
12
 
13
13
  # Enables Settings.get('nested.key.name') for dynamic access
@@ -35,7 +35,15 @@ class Settingslogic < Hash
35
35
  @namespace = value
36
36
  end
37
37
  end
38
-
38
+
39
+ def suppress_errors(value = nil)
40
+ if value.nil?
41
+ @suppress_errors
42
+ else
43
+ @suppress_errors = value
44
+ end
45
+ end
46
+
39
47
  def [](key)
40
48
  instance.fetch(key.to_s, nil)
41
49
  end
@@ -103,7 +111,9 @@ class Settingslogic < Hash
103
111
  self.replace hash_or_file
104
112
  else
105
113
  hash = YAML.load(ERB.new(File.read(hash_or_file)).result).to_hash
106
- hash = hash[self.class.namespace] if self.class.namespace
114
+ if self.class.namespace
115
+ hash = hash[self.class.namespace] or return missing_key("Missing setting '#{self.class.namespace}' in #{hash_or_file}")
116
+ end
107
117
  self.replace hash
108
118
  end
109
119
  @section = section || self.class.source # so end of error says "in application.yml"
@@ -114,7 +124,7 @@ class Settingslogic < Hash
114
124
  # Otherwise, create_accessors! (called by new) will have created actual methods for each key.
115
125
  def method_missing(name, *args, &block)
116
126
  key = name.to_s
117
- raise MissingSetting, "Missing setting '#{key}' in #{@section}" unless has_key? key
127
+ return missing_key("Missing setting '#{key}' in #{@section}") unless has_key? key
118
128
  value = fetch(key)
119
129
  create_accessor_for(key)
120
130
  value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value
@@ -150,10 +160,16 @@ class Settingslogic < Hash
150
160
  self.class.class_eval <<-EndEval
151
161
  def #{key}
152
162
  return @#{key} if @#{key}
153
- raise MissingSetting, "Missing setting '#{key}' in #{@section}" unless has_key? '#{key}'
163
+ return missing_key("Missing setting '#{key}' in #{@section}") unless has_key? '#{key}'
154
164
  value = fetch('#{key}')
155
165
  @#{key} = value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value
156
166
  end
157
167
  EndEval
158
168
  end
169
+
170
+ def missing_key(msg)
171
+ return nil if self.class.suppress_errors
172
+
173
+ raise MissingSetting, msg
174
+ end
159
175
  end
@@ -1,59 +1,64 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{settingslogic}
8
- s.version = "2.0.6"
7
+ s.name = "settingslogic"
8
+ s.version = "2.0.7"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ben Johnson of Binary Logic"]
12
- s.date = %q{2010-02-12}
13
- s.email = %q{bjohnson@binarylogic.com}
12
+ s.date = "2012-01-06"
13
+ s.email = "bjohnson@binarylogic.com"
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE",
16
- "README.rdoc"
16
+ "README.rdoc"
17
17
  ]
18
18
  s.files = [
19
- ".gitignore",
20
- "CHANGELOG.rdoc",
21
- "LICENSE",
22
- "README.rdoc",
23
- "Rakefile",
24
- "VERSION.yml",
25
- "init.rb",
26
- "lib/settingslogic.rb",
27
- "rails/init.rb",
28
- "settingslogic.gemspec",
29
- "spec/settings.rb",
30
- "spec/settings.yml",
31
- "spec/settings2.rb",
32
- "spec/settings3.rb",
33
- "spec/settingslogic_spec.rb",
34
- "spec/spec_helper.rb"
35
- ]
36
- s.homepage = %q{http://github.com/binarylogic/settingslogic}
37
- s.rdoc_options = ["--charset=UTF-8"]
38
- s.require_paths = ["lib"]
39
- s.rubygems_version = %q{1.3.5}
40
- s.summary = %q{A simple and straightforward settings solution that uses an ERB enabled YAML file and a singleton design pattern.}
41
- s.test_files = [
19
+ "CHANGELOG.rdoc",
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION.yml",
26
+ "init.rb",
27
+ "lib/settingslogic.rb",
28
+ "rails/init.rb",
29
+ "settingslogic.gemspec",
42
30
  "spec/settings.rb",
43
- "spec/settings2.rb",
44
- "spec/settings3.rb",
45
- "spec/settingslogic_spec.rb",
46
- "spec/spec_helper.rb"
31
+ "spec/settings.yml",
32
+ "spec/settings2.rb",
33
+ "spec/settings3.rb",
34
+ "spec/settings4.rb",
35
+ "spec/settingslogic_spec.rb",
36
+ "spec/spec_helper.rb"
47
37
  ]
38
+ s.homepage = "http://github.com/binarylogic/settingslogic"
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = "1.8.10"
41
+ s.summary = "A simple and straightforward settings solution that uses an ERB enabled YAML file and a singleton design pattern."
48
42
 
49
43
  if s.respond_to? :specification_version then
50
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
44
  s.specification_version = 3
52
45
 
53
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_runtime_dependency(%q<rake>, [">= 0"])
48
+ s.add_runtime_dependency(%q<jeweler>, [">= 0"])
49
+ s.add_runtime_dependency(%q<rspec>, [">= 0"])
50
+ s.add_runtime_dependency(%q<ruby-debug19>, [">= 0"])
54
51
  else
52
+ s.add_dependency(%q<rake>, [">= 0"])
53
+ s.add_dependency(%q<jeweler>, [">= 0"])
54
+ s.add_dependency(%q<rspec>, [">= 0"])
55
+ s.add_dependency(%q<ruby-debug19>, [">= 0"])
55
56
  end
56
57
  else
58
+ s.add_dependency(%q<rake>, [">= 0"])
59
+ s.add_dependency(%q<jeweler>, [">= 0"])
60
+ s.add_dependency(%q<rspec>, [">= 0"])
61
+ s.add_dependency(%q<ruby-debug19>, [">= 0"])
57
62
  end
58
63
  end
59
64
 
data/spec/settings4.rb ADDED
@@ -0,0 +1,4 @@
1
+ class Settings4 < Settingslogic
2
+ source "#{File.dirname(__FILE__)}/settings.yml"
3
+ suppress_errors true
4
+ end
@@ -107,6 +107,10 @@ describe "Settingslogic" do
107
107
  e.should_not be_nil
108
108
  end
109
109
 
110
+ it "should allow suppressing errors" do
111
+ Settings4.non_existent_key.should be_nil
112
+ end
113
+
110
114
  # This one edge case currently does not pass, because it requires very
111
115
  # esoteric code in order to make it pass. It was judged not worth fixing,
112
116
  # as it introduces significant complexity for minor gain.
@@ -133,9 +137,26 @@ describe "Settingslogic" do
133
137
  Settings.get('setting1.deep.child.value').should == 2
134
138
  end
135
139
 
140
+ # If .name is not a property, delegate to superclass
141
+ it "should respond with Module.name" do
142
+ Settings2.name.should == "Settings2"
143
+ end
144
+
145
+ # If .name is called on Settingslogic itself, handle appropriately
146
+ # by delegating to Hash
147
+ it "should have the parent class always respond with Module.name" do
148
+ Settingslogic.name.should == 'Settingslogic'
149
+ end
150
+
151
+ # If .name is a property, respond with that instead of delegating to superclass
152
+ it "should allow a name setting to be overriden" do
153
+ Settings.name.should == 'test'
154
+ end
155
+
136
156
  # Put this test last or else call to .instance will load @instance,
137
157
  # masking bugs.
138
158
  it "should be a hash" do
139
159
  Settings.send(:instance).should be_is_a(Hash)
140
160
  end
161
+
141
162
  end
data/spec/spec_helper.rb CHANGED
@@ -1,18 +1,16 @@
1
- require 'spec'
2
- require 'rubygems'
3
- require 'ruby-debug' if RUBY_VERSION < '1.9' # ruby-debug does not work on 1.9.1 yet
4
-
5
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
6
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rspec'
7
4
  require 'settingslogic'
8
5
  require 'settings'
9
6
  require 'settings2'
10
7
  require 'settings3'
8
+ require 'settings4'
11
9
 
12
10
  # Needed to test Settings3
13
11
  Object.send :define_method, 'collides' do
14
12
  'collision'
15
13
  end
16
14
 
17
- Spec::Runner.configure do |config|
15
+ RSpec.configure do |config|
18
16
  end
metadata CHANGED
@@ -1,30 +1,71 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: settingslogic
3
- version: !ruby/object:Gem::Version
4
- version: 2.0.6
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.7
5
+ prerelease:
5
6
  platform: ruby
6
- authors:
7
+ authors:
7
8
  - Ben Johnson of Binary Logic
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
-
12
- date: 2010-02-12 00:00:00 -05:00
13
- default_executable:
14
- dependencies: []
15
-
12
+ date: 2012-01-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70310322933480 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70310322933480
25
+ - !ruby/object:Gem::Dependency
26
+ name: jeweler
27
+ requirement: &70310322933000 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70310322933000
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70310322932520 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70310322932520
47
+ - !ruby/object:Gem::Dependency
48
+ name: ruby-debug19
49
+ requirement: &70310322932040 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70310322932040
16
58
  description:
17
59
  email: bjohnson@binarylogic.com
18
60
  executables: []
19
-
20
61
  extensions: []
21
-
22
- extra_rdoc_files:
62
+ extra_rdoc_files:
23
63
  - LICENSE
24
64
  - README.rdoc
25
- files:
26
- - .gitignore
65
+ files:
27
66
  - CHANGELOG.rdoc
67
+ - Gemfile
68
+ - Gemfile.lock
28
69
  - LICENSE
29
70
  - README.rdoc
30
71
  - Rakefile
@@ -37,39 +78,32 @@ files:
37
78
  - spec/settings.yml
38
79
  - spec/settings2.rb
39
80
  - spec/settings3.rb
81
+ - spec/settings4.rb
40
82
  - spec/settingslogic_spec.rb
41
83
  - spec/spec_helper.rb
42
- has_rdoc: true
43
84
  homepage: http://github.com/binarylogic/settingslogic
44
85
  licenses: []
45
-
46
86
  post_install_message:
47
- rdoc_options:
48
- - --charset=UTF-8
49
- require_paths:
87
+ rdoc_options: []
88
+ require_paths:
50
89
  - lib
51
- required_ruby_version: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- version: "0"
56
- version:
57
- required_rubygems_version: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: "0"
62
- version:
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
63
102
  requirements: []
64
-
65
103
  rubyforge_project:
66
- rubygems_version: 1.3.5
104
+ rubygems_version: 1.8.10
67
105
  signing_key:
68
106
  specification_version: 3
69
- summary: A simple and straightforward settings solution that uses an ERB enabled YAML file and a singleton design pattern.
70
- test_files:
71
- - spec/settings.rb
72
- - spec/settings2.rb
73
- - spec/settings3.rb
74
- - spec/settingslogic_spec.rb
75
- - spec/spec_helper.rb
107
+ summary: A simple and straightforward settings solution that uses an ERB enabled YAML
108
+ file and a singleton design pattern.
109
+ test_files: []
data/.gitignore DELETED
@@ -1,8 +0,0 @@
1
- .DS_Store
2
- *.log
3
- *.sqlite3
4
- pkg/*
5
- coverage/*
6
- doc/*
7
- benchmarks/*
8
-