bc-settingslogic 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: afa702215629a1527707195dd33ba627dab7bd68e6596e06984677db7d4d220e
4
+ data.tar.gz: 87e6d86ae1f7c7e6036f92f8a40c00939fee9448d87b64529299bc1bd6987e0c
5
+ SHA512:
6
+ metadata.gz: 1604ef84509592bc1fe03183147eec79c6f912f72a6ac7937ab735b1b5f5fb01d31d7fc6ece2b9a7b39e8b49f806ccc9b86214342358715da101540c62644413
7
+ data.tar.gz: 386e6843cbe343d5592fd2a83b8a88d14d6320eaed1f8995434b8efde4e8e965f1f9ce82fc41f3e1a53f95718a9559eeb0e005a579a4095f9b893d163ef883d7
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ .DS_Store
2
+ *.log
3
+ *.sqlite3
4
+ pkg/*
5
+ coverage/*
6
+ doc/*
7
+ benchmarks/*
8
+ .bundle
9
+ vendor/bundle
10
+ .rvmrc
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,29 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bc-settingslogic (2.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.3)
10
+ rake (10.0.3)
11
+ rspec (2.12.0)
12
+ rspec-core (~> 2.12.0)
13
+ rspec-expectations (~> 2.12.0)
14
+ rspec-mocks (~> 2.12.0)
15
+ rspec-core (2.12.2)
16
+ rspec-expectations (2.12.1)
17
+ diff-lcs (~> 1.1.3)
18
+ rspec-mocks (2.12.1)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ bc-settingslogic!
25
+ rake
26
+ rspec
27
+
28
+ BUNDLED WITH
29
+ 2.3.11
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Ben Johnson of Binary Logic (binarylogic.com)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,161 @@
1
+ = Settingslogic
2
+
3
+ Settingslogic is a simple configuration / settings solution that uses an ERB enabled YAML file. It has been great for
4
+ our apps, maybe you will enjoy it too. Settingslogic works with Rails, Sinatra, or any Ruby project.
5
+
6
+ {<img src="https://badge.fury.io/rb/settingslogic.svg" alt="Gem Version" />}[http://badge.fury.io/rb/settingslogic]
7
+ {<img src="https://travis-ci.org/settingslogic/settingslogic.svg" alt="Build Status" />}[https://travis-ci.org/settingslogic/settingslogic]
8
+ {<img src="http://inch-ci.org/github/settingslogic/settingslogic.png?branch=master" alt="Inline docs" />}[http://inch-ci.org/github/settingslogic/settingslogic]
9
+
10
+ == Helpful links
11
+
12
+ * <b>Documentation:</b> http://rdoc.info/github/settingslogic/settingslogic
13
+ * <b>Repository:</b> http://github.com/settingslogic/settingslogic/tree/master
14
+ * <b>Issues:</b> http://github.com/settingslogic/settingslogic/issues
15
+
16
+ == Installation
17
+
18
+ gem install settingslogic
19
+
20
+ == Usage
21
+
22
+ === 1. Define your class
23
+
24
+ Instead of defining a Settings constant for you, that task is left to you. Simply create a class in your application
25
+ that looks like:
26
+
27
+ class Settings < Settingslogic
28
+ source "#{Rails.root}/config/application.yml"
29
+ namespace Rails.env
30
+ end
31
+
32
+ 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
33
+ this file in a rails app is app/models/settings.rb
34
+
35
+ I felt adding a settings file in your app was more straightforward, less tricky, and more flexible.
36
+
37
+ === 2. Create your settings
38
+
39
+ Notice above we specified an absolute path to our settings file called "application.yml". This is just a typical YAML file.
40
+ Also notice above that we specified a namespace for our environment. A namespace is just an optional string that corresponds
41
+ to a key in the YAML file.
42
+
43
+ Using a namespace allows us to change our configuration depending on our environment:
44
+
45
+ # config/application.yml
46
+ defaults: &defaults
47
+ cool:
48
+ saweet: nested settings
49
+ neat_setting: 24
50
+ awesome_setting: <%= "Did you know 5 + 5 = #{5 + 5}?" %>
51
+
52
+ development:
53
+ <<: *defaults
54
+ neat_setting: 800
55
+
56
+ test:
57
+ <<: *defaults
58
+
59
+ production:
60
+ <<: *defaults
61
+
62
+ _Note_: Certain Ruby/Bundler versions include a version of the Psych YAML parser which incorrectly handles merges (the `<<` in the example above.)
63
+ 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:
64
+
65
+ require 'yaml'
66
+ YAML::ENGINE.yamler= 'syck'
67
+
68
+ === 3. Access your settings
69
+
70
+ >> Rails.env
71
+ => "development"
72
+
73
+ >> Settings.cool
74
+ => "#<Settingslogic::Settings ... >"
75
+
76
+ >> Settings.cool.saweet
77
+ => "nested settings"
78
+
79
+ >> Settings.neat_setting
80
+ => 800
81
+
82
+ >> Settings.awesome_setting
83
+ => "Did you know 5 + 5 = 10?"
84
+
85
+ You can use these settings anywhere, for example in a model:
86
+
87
+ class Post < ActiveRecord::Base
88
+ self.per_page = Settings.pagination.posts_per_page
89
+ end
90
+
91
+ === 4. Optional / dynamic settings
92
+
93
+ Often, you will want to handle defaults in your application logic itself, to reduce the number of settings
94
+ you need to put in your YAML file. You can access an optional setting by using Hash notation:
95
+
96
+ >> Settings.messaging.queue_name
97
+ => Exception: Missing setting 'queue_name' in 'message' section in 'application.yml'
98
+
99
+ >> Settings.messaging['queue_name']
100
+ => nil
101
+
102
+ >> Settings.messaging['queue_name'] ||= 'user_mail'
103
+ => "user_mail"
104
+
105
+ >> Settings.messaging.queue_name
106
+ => "user_mail"
107
+
108
+ Modifying our model example:
109
+
110
+ class Post < ActiveRecord::Base
111
+ self.per_page = Settings.posts['per_page'] || Settings.pagination.per_page
112
+ end
113
+
114
+ This would allow you to specify a custom value for per_page just for posts, or
115
+ to fall back to your default value if not specified.
116
+
117
+ === 5. Suppressing Exceptions Conditionally
118
+
119
+ Raising exceptions for missing settings helps highlight configuration problems. However, in a
120
+ Rails app it may make sense to suppress this in production and return nil for missing settings.
121
+ While it's useful to stop and highlight an error in development or test environments, this is
122
+ often not the right answer for production.
123
+
124
+ class Settings < Settingslogic
125
+ source "#{Rails.root}/config/application.yml"
126
+ namespace Rails.env
127
+ suppress_errors Rails.env.production?
128
+ end
129
+
130
+ >> Settings.non_existent_key
131
+ => nil
132
+
133
+ == Note on Sinatra / Capistrano / Vlad
134
+
135
+ Each of these frameworks uses a +set+ convention for settings, which actually defines methods
136
+ in the global Object namespace:
137
+
138
+ set :application, "myapp" # does "def application" globally
139
+
140
+ This can cause collisions with Settingslogic, since those methods are global. Luckily, the
141
+ solution is to just add a call to load! in your class:
142
+
143
+ class Settings < Settingslogic
144
+ source "#{Rails.root}/config/application.yml"
145
+ namespace Rails.env
146
+ load!
147
+ end
148
+
149
+ It's probably always safest to add load! to your class, since this guarantees settings will be
150
+ loaded at that time, rather than lazily later via method_missing.
151
+
152
+ Finally, you can reload all your settings later as well:
153
+
154
+ Settings.reload!
155
+
156
+ This is useful if you want to support changing your settings YAML without restarting your app.
157
+
158
+ == Author
159
+
160
+ Copyright (c) 2008-2010 {Ben Johnson}[http://github.com/binarylogic] of {Binary Logic}[http://www.binarylogic.com],
161
+ released under the MIT license. Support for optional settings and reloading by {Nate Wiger}[http://nate.wiger.org].
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ $:.push File.expand_path('lib', __dir__)
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'bc-settingslogic'
7
+ s.version = '2.1.0'
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Ben Johnson', 'Shaun McCormick']
10
+ s.email = %w[bjohnson@binarylogic.com shaun.mccormick@bigcommerce.com]
11
+ s.homepage = "https://github.com/bigcommerce/settingslogic"
12
+ s.summary = 'A simple and straightforward settings solution that uses an ERB enabled YAML file and a singleton design pattern.'
13
+ s.description = s.summary
14
+
15
+ s.add_development_dependency 'rake'
16
+ s.add_development_dependency 'rspec'
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = %w[lib]
22
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'settingslogic'
@@ -0,0 +1,196 @@
1
+ require "yaml"
2
+ require "erb"
3
+ require 'open-uri'
4
+
5
+ # A simple settings solution using a YAML file. See README for more information.
6
+ class Settingslogic < Hash
7
+ class MissingSetting < StandardError; end
8
+
9
+ class << self
10
+ def name # :nodoc:
11
+ self.superclass != Hash && instance.key?("name") ? instance.name : super
12
+ end
13
+
14
+ # Enables Settings.get('nested.key.name') for dynamic access
15
+ def get(key)
16
+ parts = key.split('.')
17
+ curs = self
18
+ while p = parts.shift
19
+ curs = curs.send(p)
20
+ end
21
+ curs
22
+ end
23
+
24
+ def source(value = nil)
25
+ @source ||= value
26
+ end
27
+
28
+ def namespace(value = nil)
29
+ @namespace ||= value
30
+ end
31
+
32
+ def suppress_errors(value = nil)
33
+ @suppress_errors ||= value
34
+ end
35
+
36
+ def [](key)
37
+ instance.fetch(key.to_s, nil)
38
+ end
39
+
40
+ def []=(key, val)
41
+ # Setting[:key][:key2] = 'value' for dynamic settings
42
+ val = new(val, source) if val.is_a? Hash
43
+ instance.store(key.to_s, val)
44
+ instance.create_accessor_for(key, val)
45
+ end
46
+
47
+ def load!
48
+ instance
49
+ true
50
+ end
51
+
52
+ def reload!
53
+ @instance = nil
54
+ load!
55
+ end
56
+
57
+ private
58
+ def instance
59
+ return @instance if @instance
60
+ @instance = new
61
+ create_accessors!
62
+ @instance
63
+ end
64
+
65
+ def method_missing(name, *args, &block)
66
+ instance.send(name, *args, &block)
67
+ end
68
+
69
+ # It would be great to DRY this up somehow, someday, but it's difficult because
70
+ # of the singleton pattern. Basically this proxies Setting.foo to Setting.instance.foo
71
+ def create_accessors!
72
+ instance.each do |key,val|
73
+ create_accessor_for(key)
74
+ end
75
+ end
76
+
77
+ def create_accessor_for(key)
78
+ return unless key.to_s =~ /^\w+$/ # could have "some-setting:" which blows up eval
79
+ instance_eval "def #{key}; instance.send(:#{key}); end"
80
+ end
81
+
82
+ end
83
+
84
+ # Initializes a new settings object. You can initialize an object in any of the following ways:
85
+ #
86
+ # Settings.new(:application) # will look for config/application.yml
87
+ # Settings.new("application.yaml") # will look for application.yaml
88
+ # Settings.new("/var/configs/application.yml") # will look for /var/configs/application.yml
89
+ # Settings.new(:config1 => 1, :config2 => 2)
90
+ #
91
+ # Basically if you pass a symbol it will look for that file in the configs directory of your rails app,
92
+ # if you are using this in rails. If you pass a string it should be an absolute path to your settings file.
93
+ # Then you can pass a hash, and it just allows you to access the hash via methods.
94
+ def initialize(hash_or_file = self.class.source, section = nil)
95
+ #puts "new! #{hash_or_file}"
96
+ case hash_or_file
97
+ when nil
98
+ raise Errno::ENOENT, "No file specified as Settingslogic source"
99
+ when Hash
100
+ self.replace hash_or_file
101
+ else
102
+ file_contents = open(hash_or_file).read
103
+ hash = if file_contents.empty?
104
+ {}
105
+ else
106
+ payload = ::ERB.new(file_contents).result
107
+ (::YAML.respond_to?(:unsafe_load) ? ::YAML.unsafe_load(payload) : ::YAML.load(payload)).to_hash
108
+ end
109
+ if self.class.namespace
110
+ hash = hash[self.class.namespace] or return missing_key("Missing setting '#{self.class.namespace}' in #{hash_or_file}")
111
+ end
112
+ self.replace hash
113
+ end
114
+ @section = section || self.class.source # so end of error says "in application.yml"
115
+ create_accessors!
116
+ end
117
+
118
+ # Called for dynamically-defined keys, and also the first key deferenced at the top-level, if load! is not used.
119
+ # Otherwise, create_accessors! (called by new) will have created actual methods for each key.
120
+ def method_missing(name, *args, &block)
121
+ key = name.to_s
122
+ return missing_key("Missing setting '#{key}' in #{@section}") unless has_key? key
123
+ value = fetch(key)
124
+ create_accessor_for(key)
125
+ value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value
126
+ end
127
+
128
+ def [](key)
129
+ fetch(key.to_s, nil)
130
+ end
131
+
132
+ def []=(key,val)
133
+ # Setting[:key][:key2] = 'value' for dynamic settings
134
+ val = self.class.new(val, @section) if val.is_a? Hash
135
+ store(key.to_s, val)
136
+ create_accessor_for(key, val)
137
+ end
138
+
139
+ # Returns an instance of a Hash object
140
+ def to_hash
141
+ Hash[self]
142
+ end
143
+
144
+ # This handles naming collisions with Sinatra/Vlad/Capistrano. Since these use a set()
145
+ # helper that defines methods in Object, ANY method_missing ANYWHERE picks up the Vlad/Sinatra
146
+ # settings! So settings.deploy_to title actually calls Object.deploy_to (from set :deploy_to, "host"),
147
+ # rather than the app_yml['deploy_to'] hash. Jeezus.
148
+ def create_accessors!
149
+ self.each do |key,val|
150
+ create_accessor_for(key)
151
+ end
152
+ end
153
+
154
+ # Use instance_eval/class_eval because they're actually more efficient than define_method{}
155
+ # http://stackoverflow.com/questions/185947/ruby-definemethod-vs-def
156
+ # http://bmorearty.wordpress.com/2009/01/09/fun-with-rubys-instance_eval-and-class_eval/
157
+ def create_accessor_for(key, val=nil)
158
+ return unless key.to_s =~ /^\w+$/ # could have "some-setting:" which blows up eval
159
+ instance_variable_set("@#{key}", val)
160
+ self.class.class_eval <<-EndEval
161
+ def #{key}
162
+ return @#{key} if @#{key}
163
+ return missing_key("Missing setting '#{key}' in #{@section}") unless has_key? '#{key}'
164
+ value = fetch('#{key}')
165
+ @#{key} = if value.is_a?(Hash)
166
+ self.class.new(value, "'#{key}' section in #{@section}")
167
+ elsif value.is_a?(Array) && value.all?{|v| v.is_a? Hash}
168
+ value.map{|v| self.class.new(v)}
169
+ else
170
+ value
171
+ end
172
+ end
173
+ EndEval
174
+ end
175
+
176
+ def symbolize_keys
177
+
178
+ inject({}) do |memo, tuple|
179
+
180
+ k = (tuple.first.to_sym rescue tuple.first) || tuple.first
181
+
182
+ v = k.is_a?(Symbol) ? send(k) : tuple.last # make sure the value is accessed the same way Settings.foo.bar works
183
+
184
+ memo[k] = v && v.respond_to?(:symbolize_keys) ? v.symbolize_keys : v #recurse for nested hashes
185
+
186
+ memo
187
+ end
188
+
189
+ end
190
+
191
+ def missing_key(msg)
192
+ return nil if self.class.suppress_errors
193
+
194
+ raise MissingSetting, msg
195
+ end
196
+ end
data/spec/settings.rb ADDED
@@ -0,0 +1,6 @@
1
+ class Settings < Settingslogic
2
+ source "#{File.dirname(__FILE__)}/settings.yml"
3
+ end
4
+
5
+ class SettingsInst < Settingslogic
6
+ end
data/spec/settings.yml ADDED
@@ -0,0 +1,28 @@
1
+ setting1:
2
+ setting1_child: saweet
3
+ deep:
4
+ another: my value
5
+ child:
6
+ value: 2
7
+
8
+ setting2: 5
9
+ setting3: <%= 5 * 5 %>
10
+ name: test
11
+
12
+ language:
13
+ haskell:
14
+ paradigm: functional
15
+ smalltalk:
16
+ paradigm: object oriented
17
+
18
+ collides:
19
+ does: not
20
+ nested:
21
+ collides:
22
+ does: not either
23
+
24
+ array:
25
+ -
26
+ name: first
27
+ -
28
+ name: second
data/spec/settings2.rb ADDED
@@ -0,0 +1,4 @@
1
+ class Settings2 < Settingslogic
2
+ source "#{File.dirname(__FILE__)}/settings.yml"
3
+ namespace "setting1"
4
+ end
data/spec/settings3.rb ADDED
@@ -0,0 +1,4 @@
1
+ class Settings3 < Settingslogic
2
+ source "#{File.dirname(__FILE__)}/settings.yml"
3
+ load! # test of load
4
+ end
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
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SettingsAliases < Settingslogic
4
+ source "#{__dir__}/settings_aliases.yml"
5
+ namespace 'test'
6
+ end
@@ -0,0 +1,11 @@
1
+ defaults: &defaults
2
+ a: 1
3
+ b: 2
4
+
5
+ development:
6
+ <<: *defaults
7
+ a: 2
8
+
9
+ test:
10
+ <<: *defaults
11
+ b: 3
@@ -0,0 +1,3 @@
1
+ class SettingsEmpty < Settingslogic
2
+ source "#{File.dirname(__FILE__)}/settings_empty.yml"
3
+ end
File without changes
@@ -0,0 +1,216 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
2
+
3
+ describe "Settingslogic" do
4
+ it "should access settings" do
5
+ Settings.setting2.should == 5
6
+ end
7
+
8
+ it "should access nested settings" do
9
+ Settings.setting1.setting1_child.should == "saweet"
10
+ end
11
+
12
+ it "should access settings in nested arrays" do
13
+ Settings.array.first.name.should == "first"
14
+ end
15
+
16
+ it "should access deep nested settings" do
17
+ Settings.setting1.deep.another.should == "my value"
18
+ end
19
+
20
+ it "should access extra deep nested settings" do
21
+ Settings.setting1.deep.child.value.should == 2
22
+ end
23
+
24
+ it "should enable erb" do
25
+ Settings.setting3.should == 25
26
+ end
27
+
28
+ it "should namespace settings" do
29
+ Settings2.setting1_child.should == "saweet"
30
+ Settings2.deep.another.should == "my value"
31
+ end
32
+
33
+ it "should return the namespace" do
34
+ Settings.namespace.should be_nil
35
+ Settings2.namespace.should == 'setting1'
36
+ end
37
+
38
+ it "should distinguish nested keys" do
39
+ Settings.language.haskell.paradigm.should == 'functional'
40
+ Settings.language.smalltalk.paradigm.should == 'object oriented'
41
+ end
42
+
43
+ it "should not collide with global methods" do
44
+ Settings3.nested.collides.does.should == 'not either'
45
+ Settings3[:nested] = 'fooey'
46
+ Settings3[:nested].should == 'fooey'
47
+ Settings3.nested.should == 'fooey'
48
+ Settings3.collides.does.should == 'not'
49
+ end
50
+
51
+ it "should raise a helpful error message" do
52
+ e = nil
53
+ begin
54
+ Settings.missing
55
+ rescue => e
56
+ e.should be_kind_of Settingslogic::MissingSetting
57
+ end
58
+ e.should_not be_nil
59
+ e.message.should =~ /Missing setting 'missing' in/
60
+
61
+ e = nil
62
+ begin
63
+ Settings.language.missing
64
+ rescue => e
65
+ e.should be_kind_of Settingslogic::MissingSetting
66
+ end
67
+ e.should_not be_nil
68
+ e.message.should =~ /Missing setting 'missing' in 'language' section/
69
+ end
70
+
71
+ it "should handle optional / dynamic settings" do
72
+ e = nil
73
+ begin
74
+ Settings.language.erlang
75
+ rescue => e
76
+ e.should be_kind_of Settingslogic::MissingSetting
77
+ end
78
+ e.should_not be_nil
79
+ e.message.should =~ /Missing setting 'erlang' in 'language' section/
80
+
81
+ Settings.language['erlang'].should be_nil
82
+ Settings.language['erlang'] = 5
83
+ Settings.language['erlang'].should == 5
84
+
85
+ Settings.language['erlang'] = {'paradigm' => 'functional'}
86
+ Settings.language.erlang.paradigm.should == 'functional'
87
+ Settings.respond_to?('erlang').should be_false
88
+
89
+ Settings.reload!
90
+ Settings.language['erlang'].should be_nil
91
+
92
+ Settings.language[:erlang] ||= 5
93
+ Settings.language[:erlang].should == 5
94
+
95
+ Settings.language[:erlang] = {}
96
+ Settings.language[:erlang][:paradigm] = 'functional'
97
+ Settings.language.erlang.paradigm.should == 'functional'
98
+
99
+ Settings[:toplevel] = '42'
100
+ Settings.toplevel.should == '42'
101
+ end
102
+
103
+ it "should raise an error on a nil source argument" do
104
+ class NoSource < Settingslogic; end
105
+ e = nil
106
+ begin
107
+ NoSource.foo.bar
108
+ rescue => e
109
+ e.should be_kind_of Errno::ENOENT
110
+ end
111
+ e.should_not be_nil
112
+ end
113
+
114
+ it "should allow suppressing errors" do
115
+ Settings4.non_existent_key.should be_nil
116
+ end
117
+
118
+ # This one edge case currently does not pass, because it requires very
119
+ # esoteric code in order to make it pass. It was judged not worth fixing,
120
+ # as it introduces significant complexity for minor gain.
121
+ # it "should handle reloading top-level settings"
122
+ # Settings[:inspect] = 'yeah baby'
123
+ # Settings.inspect.should == 'yeah baby'
124
+ # Settings.reload!
125
+ # Settings.inspect.should == 'Settings'
126
+ # end
127
+
128
+ it "should handle oddly-named settings" do
129
+ Settings.language['some-dash-setting#'] = 'dashtastic'
130
+ Settings.language['some-dash-setting#'].should == 'dashtastic'
131
+ end
132
+
133
+ it "should handle settings with nil value" do
134
+ Settings["flag"] = true
135
+ Settings["flag"] = nil
136
+ Settings.flag.should == nil
137
+ end
138
+
139
+ it "should handle settings with false value" do
140
+ Settings["flag"] = true
141
+ Settings["flag"] = false
142
+ Settings.flag.should == false
143
+ end
144
+
145
+ it "should support instance usage as well" do
146
+ settings = SettingsInst.new(Settings.source)
147
+ settings.setting1.setting1_child.should == "saweet"
148
+ end
149
+
150
+ it "should be able to get() a key with dot.notation" do
151
+ Settings.get('setting1.setting1_child').should == "saweet"
152
+ Settings.get('setting1.deep.another').should == "my value"
153
+ Settings.get('setting1.deep.child.value').should == 2
154
+ end
155
+
156
+ # If .name is not a property, delegate to superclass
157
+ it "should respond with Module.name" do
158
+ Settings2.name.should == "Settings2"
159
+ end
160
+
161
+ # If .name is called on Settingslogic itself, handle appropriately
162
+ # by delegating to Hash
163
+ it "should have the parent class always respond with Module.name" do
164
+ Settingslogic.name.should == 'Settingslogic'
165
+ end
166
+
167
+ # If .name is a property, respond with that instead of delegating to superclass
168
+ it "should allow a name setting to be overriden" do
169
+ Settings.name.should == 'test'
170
+ end
171
+
172
+ it "should allow symbolize_keys" do
173
+ Settings.reload!
174
+ result = Settings.language.haskell.symbolize_keys
175
+ result.class.should == Hash
176
+ result.should == {:paradigm => "functional"}
177
+ end
178
+
179
+ it "should allow symbolize_keys on nested hashes" do
180
+ Settings.reload!
181
+ result = Settings.language.symbolize_keys
182
+ result.class.should == Hash
183
+ result.should == {
184
+ :haskell => {:paradigm => "functional"},
185
+ :smalltalk => {:paradigm => "object oriented"}
186
+ }
187
+ end
188
+
189
+ it "should handle empty file" do
190
+ SettingsEmpty.keys.should eql([])
191
+ end
192
+
193
+ # Put this test last or else call to .instance will load @instance,
194
+ # masking bugs.
195
+ it "should be a hash" do
196
+ Settings.send(:instance).should be_is_a(Hash)
197
+ end
198
+
199
+ context 'with aliases' do
200
+ subject { SettingsAliases }
201
+
202
+ it 'respects aliased values' do
203
+ expect(subject.a).to eq 1
204
+ expect(subject.b).to eq 3
205
+ end
206
+ end
207
+
208
+ describe "#to_hash" do
209
+ it "should return a new instance of a Hash object" do
210
+ Settings.to_hash.should be_kind_of(Hash)
211
+ Settings.to_hash.class.name.should == "Hash"
212
+ Settings.to_hash.object_id.should_not == Settings.object_id
213
+ end
214
+ end
215
+
216
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require 'rspec'
6
+ require 'settingslogic'
7
+ require 'settings'
8
+ require 'settings2'
9
+ require 'settings3'
10
+ require 'settings4'
11
+ require 'settings_empty'
12
+ require 'settings_aliases'
13
+
14
+ # Needed to test Settings3
15
+ Object.send :define_method, 'collides' do
16
+ 'collision'
17
+ end
18
+
19
+ RSpec.configure do |config|
20
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bc-settingslogic
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben Johnson
8
+ - Shaun McCormick
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2022-05-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rspec
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ description: A simple and straightforward settings solution that uses an ERB enabled
43
+ YAML file and a singleton design pattern.
44
+ email:
45
+ - bjohnson@binarylogic.com
46
+ - shaun.mccormick@bigcommerce.com
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - ".gitignore"
52
+ - ".travis.yml"
53
+ - Gemfile
54
+ - Gemfile.lock
55
+ - LICENSE
56
+ - README.rdoc
57
+ - Rakefile
58
+ - bc-settingslogic.gemspec
59
+ - lib/bc-settingslogic.rb
60
+ - lib/settingslogic.rb
61
+ - spec/settings.rb
62
+ - spec/settings.yml
63
+ - spec/settings2.rb
64
+ - spec/settings3.rb
65
+ - spec/settings4.rb
66
+ - spec/settings_aliases.rb
67
+ - spec/settings_aliases.yml
68
+ - spec/settings_empty.rb
69
+ - spec/settings_empty.yml
70
+ - spec/settingslogic_spec.rb
71
+ - spec/spec_helper.rb
72
+ homepage: https://github.com/bigcommerce/settingslogic
73
+ licenses: []
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubygems_version: 3.3.7
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: A simple and straightforward settings solution that uses an ERB enabled YAML
94
+ file and a singleton design pattern.
95
+ test_files:
96
+ - spec/settings.rb
97
+ - spec/settings.yml
98
+ - spec/settings2.rb
99
+ - spec/settings3.rb
100
+ - spec/settings4.rb
101
+ - spec/settings_aliases.rb
102
+ - spec/settings_aliases.yml
103
+ - spec/settings_empty.rb
104
+ - spec/settings_empty.yml
105
+ - spec/settingslogic_spec.rb
106
+ - spec/spec_helper.rb