santuxus-settingslogic 2.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +27 -0
- data/LICENSE +20 -0
- data/README.rdoc +145 -0
- data/Rakefile +32 -0
- data/VERSION.yml +5 -0
- data/init.rb +1 -0
- data/lib/settingslogic.rb +164 -0
- data/rails/init.rb +1 -0
- data/settingslogic.gemspec +59 -0
- data/spec/settings.rb +6 -0
- data/spec/settings.yml +22 -0
- data/spec/settings2.rb +4 -0
- data/spec/settings2.yml +2 -0
- data/spec/settings3.rb +4 -0
- data/spec/settings3.yml +1 -0
- data/spec/settings4.rb +3 -0
- data/spec/settingslogic_spec.rb +148 -0
- data/spec/spec_helper.rb +19 -0
- metadata +90 -0
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
== 2.0.2 released 2009-08-22
|
2
|
+
|
3
|
+
* Define methods during method_missing instead of during initialization. Allows for modification on the fly.
|
4
|
+
|
5
|
+
== 2.0.0 released 2009-08-22
|
6
|
+
|
7
|
+
* Less magic, instead of automatically defining a Settings constant, you should define your own constant. See the readme for an example.
|
8
|
+
|
9
|
+
== 1.0.3 released 2009-04-23
|
10
|
+
|
11
|
+
* Fix Settings initialized with a Hash to work with both symbol and string hash keys.
|
12
|
+
|
13
|
+
== 1.0.2 released 2009-04-09
|
14
|
+
|
15
|
+
* Call key? off of the self in the class level name method.
|
16
|
+
|
17
|
+
== 1.0.1 released 2009-04-02
|
18
|
+
|
19
|
+
* Inherit from hash.
|
20
|
+
|
21
|
+
== 1.0.0 released 2008-12-05
|
22
|
+
|
23
|
+
* Only define methods if we have settings.
|
24
|
+
|
25
|
+
== 0.9.0 released 2008-10-30
|
26
|
+
|
27
|
+
* Initial release
|
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,145 @@
|
|
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
|
+
So here is my question to you.....is Settingslogic a great settings solution or the greatest?
|
7
|
+
|
8
|
+
== Helpful links
|
9
|
+
|
10
|
+
* <b>Documentation:</b> http://rdoc.info/projects/binarylogic/settingslogic
|
11
|
+
* <b>Repository:</b> http://github.com/binarylogic/settingslogic/tree/master
|
12
|
+
* <b>Issues:</b> http://github.com/binarylogic/settingslogic/issues
|
13
|
+
|
14
|
+
== Installation
|
15
|
+
|
16
|
+
Install from rubyforge/gemcutter:
|
17
|
+
|
18
|
+
sudo gem install settingslogic
|
19
|
+
|
20
|
+
Or as a Rails plugin:
|
21
|
+
|
22
|
+
script/plugin install git://github.com/binarylogic/settingslogic.git
|
23
|
+
|
24
|
+
Settingslogic does not have any dependencies on Rails. Installing as a gem is recommended.
|
25
|
+
|
26
|
+
== Usage
|
27
|
+
|
28
|
+
=== 1. Define your class
|
29
|
+
|
30
|
+
Instead of defining a Settings constant for you, that task is left to you. Simply create a class in your application
|
31
|
+
that looks like:
|
32
|
+
|
33
|
+
class Settings < Settingslogic
|
34
|
+
source "#{Rails.root}/config/application.yml"
|
35
|
+
namespace Rails.env
|
36
|
+
end
|
37
|
+
|
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
|
40
|
+
|
41
|
+
I felt adding a settings file in your app was more straightforward, less tricky, and more flexible.
|
42
|
+
|
43
|
+
=== 2. Create your settings
|
44
|
+
|
45
|
+
Notice above we specified an absolute path to our settings file called "application.yml". This is just a typical YAML file.
|
46
|
+
Also notice above that we specified a namespace for our environment. A namespace is just an optional string that corresponds
|
47
|
+
to a key in the YAML file.
|
48
|
+
|
49
|
+
Using a namespace allows us to change our configuration depending on our environment:
|
50
|
+
|
51
|
+
# app/config/application.yml
|
52
|
+
defaults: &defaults
|
53
|
+
cool:
|
54
|
+
saweet: nested settings
|
55
|
+
neat_setting: 24
|
56
|
+
awesome_setting: <%= "Did you know 5 + 5 = #{5 + 5}?" %>
|
57
|
+
|
58
|
+
development:
|
59
|
+
<<: *defaults
|
60
|
+
neat_setting: 800
|
61
|
+
|
62
|
+
test:
|
63
|
+
<<: *defaults
|
64
|
+
|
65
|
+
production:
|
66
|
+
<<: *defaults
|
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
|
+
== 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
|
+
== Author
|
143
|
+
|
144
|
+
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].
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "santuxus-settingslogic"
|
8
|
+
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, santuxus@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/santuxus/settingslogic"
|
11
|
+
gem.authors = ["Ben Johnson of Binary Logic, Santuxus"]
|
12
|
+
end
|
13
|
+
Jeweler::GemcutterTasks.new
|
14
|
+
rescue LoadError
|
15
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'spec/rake/spectask'
|
19
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
20
|
+
spec.libs << 'lib' << 'spec'
|
21
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
22
|
+
end
|
23
|
+
|
24
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
25
|
+
spec.libs << 'lib' << 'spec'
|
26
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
27
|
+
spec.rcov = true
|
28
|
+
end
|
29
|
+
|
30
|
+
task :spec => :check_dependencies
|
31
|
+
|
32
|
+
task :default => :spec
|
data/VERSION.yml
ADDED
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/rails/init.rb"
|
@@ -0,0 +1,164 @@
|
|
1
|
+
require "yaml"
|
2
|
+
require "erb"
|
3
|
+
|
4
|
+
# A simple settings solution using a YAML file. See README for more information.
|
5
|
+
class Settingslogic < Hash
|
6
|
+
class MissingSetting < StandardError; end
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def name # :nodoc:
|
10
|
+
instance.key?("name") ? instance.name : super
|
11
|
+
end
|
12
|
+
|
13
|
+
# Enables Settings.get('nested.key.name') for dynamic access
|
14
|
+
def get(key)
|
15
|
+
parts = key.split('.')
|
16
|
+
curs = self
|
17
|
+
while p = parts.shift
|
18
|
+
curs = curs.send(p)
|
19
|
+
end
|
20
|
+
curs
|
21
|
+
end
|
22
|
+
|
23
|
+
def source(value = nil)
|
24
|
+
if value.nil?
|
25
|
+
@source
|
26
|
+
else
|
27
|
+
@source = value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def namespace(value = nil)
|
32
|
+
if value.nil?
|
33
|
+
@namespace
|
34
|
+
else
|
35
|
+
@namespace = value
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def [](key)
|
40
|
+
instance.fetch(key.to_s, nil)
|
41
|
+
end
|
42
|
+
|
43
|
+
def []=(key, val)
|
44
|
+
# Setting[:key][:key2] = 'value' for dynamic settings
|
45
|
+
val = new(val, source) if val.is_a? Hash
|
46
|
+
instance.store(key.to_s, val)
|
47
|
+
instance.create_accessor_for(key, val)
|
48
|
+
end
|
49
|
+
|
50
|
+
def load!
|
51
|
+
instance
|
52
|
+
true
|
53
|
+
end
|
54
|
+
|
55
|
+
def reload!
|
56
|
+
@instance = nil
|
57
|
+
load!
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
def instance
|
62
|
+
return @instance if @instance
|
63
|
+
@instance = new
|
64
|
+
create_accessors!
|
65
|
+
@instance
|
66
|
+
end
|
67
|
+
|
68
|
+
def method_missing(name, *args, &block)
|
69
|
+
instance.send(name, *args, &block)
|
70
|
+
end
|
71
|
+
|
72
|
+
# It would be great to DRY this up somehow, someday, but it's difficult because
|
73
|
+
# of the singleton pattern. Basically this proxies Setting.foo to Setting.instance.foo
|
74
|
+
def create_accessors!
|
75
|
+
instance.each do |key,val|
|
76
|
+
create_accessor_for(key)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def create_accessor_for(key)
|
81
|
+
return unless key.to_s =~ /^\w+$/ # could have "some-setting:" which blows up eval
|
82
|
+
instance_eval "def #{key}; instance.send(:#{key}); end"
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
# Initializes a new settings object. You can initialize an object in any of the following ways:
|
88
|
+
#
|
89
|
+
# Settings.new(:application) # will look for config/application.yml
|
90
|
+
# Settings.new("application.yaml") # will look for application.yaml
|
91
|
+
# Settings.new("/var/configs/application.yml") # will look for /var/configs/application.yml
|
92
|
+
# Settings.new(:config1 => 1, :config2 => 2)
|
93
|
+
# Settings.new(["defaults.yml", "test.yml"]) # will look for defaults.yml and test.yml and merge them
|
94
|
+
#
|
95
|
+
# Basically if you pass a symbol it will look for that file in the configs directory of your rails app,
|
96
|
+
# if you are using this in rails. If you pass a string it should be an absolute path to your settings file.
|
97
|
+
# Then you can pass a hash, and it just allows you to access the hash via methods.
|
98
|
+
def initialize(hash_or_file = self.class.source, section = nil)
|
99
|
+
#puts "new! #{hash_or_file}"
|
100
|
+
case hash_or_file
|
101
|
+
when nil
|
102
|
+
raise Errno::ENOENT, "No file specified as Settingslogic source"
|
103
|
+
when Hash
|
104
|
+
self.replace hash_or_file
|
105
|
+
when Array
|
106
|
+
hash = hash_or_file.inject({}){|sum, file| sum.merge!(YAML.load(ERB.new(File.read(file)).result).to_hash)}
|
107
|
+
hash = hash[self.class.namespace] if self.class.namespace
|
108
|
+
self.replace hash
|
109
|
+
else
|
110
|
+
hash = YAML.load(ERB.new(File.read(hash_or_file)).result).to_hash
|
111
|
+
hash = hash[self.class.namespace] if self.class.namespace
|
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
|
+
raise MissingSetting, "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
|
+
# This handles naming collisions with Sinatra/Vlad/Capistrano. Since these use a set()
|
140
|
+
# helper that defines methods in Object, ANY method_missing ANYWHERE picks up the Vlad/Sinatra
|
141
|
+
# settings! So settings.deploy_to title actually calls Object.deploy_to (from set :deploy_to, "host"),
|
142
|
+
# rather than the app_yml['deploy_to'] hash. Jeezus.
|
143
|
+
def create_accessors!
|
144
|
+
self.each do |key,val|
|
145
|
+
create_accessor_for(key)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
# Use instance_eval/class_eval because they're actually more efficient than define_method{}
|
150
|
+
# http://stackoverflow.com/questions/185947/ruby-definemethod-vs-def
|
151
|
+
# http://bmorearty.wordpress.com/2009/01/09/fun-with-rubys-instance_eval-and-class_eval/
|
152
|
+
def create_accessor_for(key, val=nil)
|
153
|
+
return unless key.to_s =~ /^\w+$/ # could have "some-setting:" which blows up eval
|
154
|
+
instance_variable_set("@#{key}", val) if val
|
155
|
+
self.class.class_eval <<-EndEval
|
156
|
+
def #{key}
|
157
|
+
return @#{key} if @#{key}
|
158
|
+
raise MissingSetting, "Missing setting '#{key}' in #{@section}" unless has_key? '#{key}'
|
159
|
+
value = fetch('#{key}')
|
160
|
+
@#{key} = value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value
|
161
|
+
end
|
162
|
+
EndEval
|
163
|
+
end
|
164
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "settingslogic"
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{settingslogic}
|
8
|
+
s.version = "2.0.6"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Ben Johnson of Binary Logic"]
|
12
|
+
s.date = %q{2010-02-12}
|
13
|
+
s.email = %q{bjohnson@binarylogic.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"LICENSE",
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
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 = [
|
42
|
+
"spec/settings.rb",
|
43
|
+
"spec/settings2.rb",
|
44
|
+
"spec/settings3.rb",
|
45
|
+
"spec/settingslogic_spec.rb",
|
46
|
+
"spec/spec_helper.rb"
|
47
|
+
]
|
48
|
+
|
49
|
+
if s.respond_to? :specification_version then
|
50
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
51
|
+
s.specification_version = 3
|
52
|
+
|
53
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
54
|
+
else
|
55
|
+
end
|
56
|
+
else
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
data/spec/settings.rb
ADDED
data/spec/settings.yml
ADDED
@@ -0,0 +1,22 @@
|
|
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
|
data/spec/settings2.rb
ADDED
data/spec/settings2.yml
ADDED
data/spec/settings3.rb
ADDED
data/spec/settings3.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
another_setting: from settings3.yml
|
data/spec/settings4.rb
ADDED
@@ -0,0 +1,148 @@
|
|
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 deep nested settings" do
|
13
|
+
Settings.setting1.deep.another.should == "my value"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should access extra deep nested settings" do
|
17
|
+
Settings.setting1.deep.child.value.should == 2
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should enable erb" do
|
21
|
+
Settings.setting3.should == 25
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should namespace settings" do
|
25
|
+
Settings2.setting1_child.should == "saweet"
|
26
|
+
Settings2.deep.another.should == "my value"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should return the namespace" do
|
30
|
+
Settings.namespace.should be_nil
|
31
|
+
Settings2.namespace.should == 'setting1'
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should distinguish nested keys" do
|
35
|
+
Settings.language.haskell.paradigm.should == 'functional'
|
36
|
+
Settings.language.smalltalk.paradigm.should == 'object oriented'
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should not collide with global methods" do
|
40
|
+
Settings3.nested.collides.does.should == 'not either'
|
41
|
+
Settings3[:nested] = 'fooey'
|
42
|
+
Settings3[:nested].should == 'fooey'
|
43
|
+
Settings3.nested.should == 'fooey'
|
44
|
+
Settings3.collides.does.should == 'not'
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should raise a helpful error message" do
|
48
|
+
e = nil
|
49
|
+
begin
|
50
|
+
Settings.missing
|
51
|
+
rescue => e
|
52
|
+
e.should be_kind_of Settingslogic::MissingSetting
|
53
|
+
end
|
54
|
+
e.should_not be_nil
|
55
|
+
e.message.should =~ /Missing setting 'missing' in/
|
56
|
+
|
57
|
+
e = nil
|
58
|
+
begin
|
59
|
+
Settings.language.missing
|
60
|
+
rescue => e
|
61
|
+
e.should be_kind_of Settingslogic::MissingSetting
|
62
|
+
end
|
63
|
+
e.should_not be_nil
|
64
|
+
e.message.should =~ /Missing setting 'missing' in 'language' section/
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should handle optional / dynamic settings" do
|
68
|
+
e = nil
|
69
|
+
begin
|
70
|
+
Settings.language.erlang
|
71
|
+
rescue => e
|
72
|
+
e.should be_kind_of Settingslogic::MissingSetting
|
73
|
+
end
|
74
|
+
e.should_not be_nil
|
75
|
+
e.message.should =~ /Missing setting 'erlang' in 'language' section/
|
76
|
+
|
77
|
+
Settings.language['erlang'].should be_nil
|
78
|
+
Settings.language['erlang'] = 5
|
79
|
+
Settings.language['erlang'].should == 5
|
80
|
+
|
81
|
+
Settings.language['erlang'] = {'paradigm' => 'functional'}
|
82
|
+
Settings.language.erlang.paradigm.should == 'functional'
|
83
|
+
Settings.respond_to?('erlang').should be_false
|
84
|
+
|
85
|
+
Settings.reload!
|
86
|
+
Settings.language['erlang'].should be_nil
|
87
|
+
|
88
|
+
Settings.language[:erlang] ||= 5
|
89
|
+
Settings.language[:erlang].should == 5
|
90
|
+
|
91
|
+
Settings.language[:erlang] = {}
|
92
|
+
Settings.language[:erlang][:paradigm] = 'functional'
|
93
|
+
Settings.language.erlang.paradigm.should == 'functional'
|
94
|
+
|
95
|
+
Settings[:toplevel] = '42'
|
96
|
+
Settings.toplevel.should == '42'
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should raise an error on a nil source argument" do
|
100
|
+
class NoSource < Settingslogic; end
|
101
|
+
e = nil
|
102
|
+
begin
|
103
|
+
NoSource.foo.bar
|
104
|
+
rescue => e
|
105
|
+
e.should be_kind_of Errno::ENOENT
|
106
|
+
end
|
107
|
+
e.should_not be_nil
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should accept an array of file names as a source argument and load all of them" do
|
111
|
+
Settings4.setting1.setting1_child.should == "saweet"
|
112
|
+
Settings4.setting2.should == "overwritten!"
|
113
|
+
Settings4.setting4.should == "from settings2.yml"
|
114
|
+
Settings4.another_setting.should == "from settings3.yml"
|
115
|
+
end
|
116
|
+
|
117
|
+
# This one edge case currently does not pass, because it requires very
|
118
|
+
# esoteric code in order to make it pass. It was judged not worth fixing,
|
119
|
+
# as it introduces significant complexity for minor gain.
|
120
|
+
# it "should handle reloading top-level settings"
|
121
|
+
# Settings[:inspect] = 'yeah baby'
|
122
|
+
# Settings.inspect.should == 'yeah baby'
|
123
|
+
# Settings.reload!
|
124
|
+
# Settings.inspect.should == 'Settings'
|
125
|
+
# end
|
126
|
+
|
127
|
+
it "should handle oddly-named settings" do
|
128
|
+
Settings.language['some-dash-setting#'] = 'dashtastic'
|
129
|
+
Settings.language['some-dash-setting#'].should == 'dashtastic'
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should support instance usage as well" do
|
133
|
+
settings = SettingsInst.new(Settings.source)
|
134
|
+
settings.setting1.setting1_child.should == "saweet"
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should be able to get() a key with dot.notation" do
|
138
|
+
Settings.get('setting1.setting1_child').should == "saweet"
|
139
|
+
Settings.get('setting1.deep.another').should == "my value"
|
140
|
+
Settings.get('setting1.deep.child.value').should == 2
|
141
|
+
end
|
142
|
+
|
143
|
+
# Put this test last or else call to .instance will load @instance,
|
144
|
+
# masking bugs.
|
145
|
+
it "should be a hash" do
|
146
|
+
Settings.send(:instance).should be_is_a(Hash)
|
147
|
+
end
|
148
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
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
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
7
|
+
require 'settingslogic'
|
8
|
+
require 'settings'
|
9
|
+
require 'settings2'
|
10
|
+
require 'settings3'
|
11
|
+
require 'settings4'
|
12
|
+
|
13
|
+
# Needed to test Settings3
|
14
|
+
Object.send :define_method, 'collides' do
|
15
|
+
'collision'
|
16
|
+
end
|
17
|
+
|
18
|
+
Spec::Runner.configure do |config|
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: santuxus-settingslogic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 3
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
- 6
|
10
|
+
version: 2.0.6
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ben Johnson of Binary Logic, Santuxus
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-04-26 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description:
|
23
|
+
email: bjohnson@binarylogic.com, santuxus@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- LICENSE
|
30
|
+
- README.rdoc
|
31
|
+
files:
|
32
|
+
- CHANGELOG.rdoc
|
33
|
+
- LICENSE
|
34
|
+
- README.rdoc
|
35
|
+
- Rakefile
|
36
|
+
- VERSION.yml
|
37
|
+
- init.rb
|
38
|
+
- lib/settingslogic.rb
|
39
|
+
- rails/init.rb
|
40
|
+
- settingslogic.gemspec
|
41
|
+
- spec/settings.rb
|
42
|
+
- spec/settings.yml
|
43
|
+
- spec/settings2.rb
|
44
|
+
- spec/settings2.yml
|
45
|
+
- spec/settings3.rb
|
46
|
+
- spec/settings3.yml
|
47
|
+
- spec/settings4.rb
|
48
|
+
- spec/settingslogic_spec.rb
|
49
|
+
- spec/spec_helper.rb
|
50
|
+
has_rdoc: true
|
51
|
+
homepage: http://github.com/santuxus/settingslogic
|
52
|
+
licenses: []
|
53
|
+
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
hash: 3
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
requirements: []
|
78
|
+
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 1.3.7
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: A simple and straightforward settings solution that uses an ERB enabled YAML file and a singleton design pattern.
|
84
|
+
test_files:
|
85
|
+
- spec/settings.rb
|
86
|
+
- spec/settings2.rb
|
87
|
+
- spec/settings3.rb
|
88
|
+
- spec/settings4.rb
|
89
|
+
- spec/settingslogic_spec.rb
|
90
|
+
- spec/spec_helper.rb
|