confing 0.0.1.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +26 -0
- data/LICENSE +20 -0
- data/README.rdoc +179 -0
- data/Rakefile +7 -0
- data/confing.gemspec +20 -0
- data/lib/settingslogic.rb +204 -0
- data/spec/settings.rb +16 -0
- data/spec/settings.yml +28 -0
- data/spec/settings2.rb +4 -0
- data/spec/settings3.rb +4 -0
- data/spec/settings4.rb +4 -0
- data/spec/settings_empty.rb +3 -0
- data/spec/settings_empty.yml +0 -0
- data/spec/settingslogic_spec.rb +220 -0
- data/spec/spec_helper.rb +17 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7a15e3a7b432619186b8f062632d847ba7436c84
|
4
|
+
data.tar.gz: 61b2cdbc14ef7c8f5dcb467f85050649c619580f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f35f68656b16ef51c8ada066e6fe674d51018e52723460fc5bdeabcd688a5c63041a00a4e79a856a0dbdce40f739233e34a18eff1c4be2a46aed8cc295102b79
|
7
|
+
data.tar.gz: f893b249319e953352dfeba22ac57a3d6acbe065751dcbf14150eddda38bed1e754e0dec9e75382c945c9b6455111c226e2a9fcd10a2a671c09d0da9c777baa9
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
settingslogic (2.0.9)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://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
|
+
rake
|
25
|
+
rspec
|
26
|
+
settingslogic!
|
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,179 @@
|
|
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
|
+
== Helpful links
|
7
|
+
|
8
|
+
* <b>Documentation:</b> http://rdoc.info/projects/binarylogic/settingslogic
|
9
|
+
* <b>Repository:</b> http://github.com/binarylogic/settingslogic/tree/master
|
10
|
+
* <b>Issues:</b> http://github.com/binarylogic/settingslogic/issues
|
11
|
+
|
12
|
+
== Installation
|
13
|
+
|
14
|
+
gem install settingslogic
|
15
|
+
|
16
|
+
== Usage
|
17
|
+
|
18
|
+
=== 1. Define your class
|
19
|
+
|
20
|
+
Instead of defining a Settings constant for you, that task is left to you. Simply create a class in your application
|
21
|
+
that looks like:
|
22
|
+
|
23
|
+
class Settings < Settingslogic
|
24
|
+
source "#{Rails.root}/config/application.yml"
|
25
|
+
namespace Rails.env
|
26
|
+
end
|
27
|
+
|
28
|
+
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
|
29
|
+
this file in a rails app is app/models/settings.rb
|
30
|
+
|
31
|
+
I felt adding a settings file in your app was more straightforward, less tricky, and more flexible.
|
32
|
+
|
33
|
+
=== 2. Create your settings
|
34
|
+
|
35
|
+
Notice above we specified an absolute path to our settings file called "application.yml". This is just a typical YAML file.
|
36
|
+
Also notice above that we specified a namespace for our environment. A namespace is just an optional string that corresponds
|
37
|
+
to a key in the YAML file.
|
38
|
+
|
39
|
+
Using a namespace allows us to change our configuration depending on our environment:
|
40
|
+
|
41
|
+
# config/application.yml
|
42
|
+
defaults: &defaults
|
43
|
+
cool:
|
44
|
+
saweet: nested settings
|
45
|
+
neat_setting: 24
|
46
|
+
awesome_setting: <%= "Did you know 5 + 5 = #{5 + 5}?" %>
|
47
|
+
|
48
|
+
development:
|
49
|
+
<<: *defaults
|
50
|
+
neat_setting: 800
|
51
|
+
|
52
|
+
test:
|
53
|
+
<<: *defaults
|
54
|
+
|
55
|
+
production:
|
56
|
+
<<: *defaults
|
57
|
+
|
58
|
+
_Note_: Certain Ruby/Bundler versions include a version of the Psych YAML parser which incorrectly handles merges (the `<<` in the example above.)
|
59
|
+
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:
|
60
|
+
|
61
|
+
require 'yaml'
|
62
|
+
YAML::ENGINE.yamler= 'syck'
|
63
|
+
|
64
|
+
==== Namespaced settings
|
65
|
+
|
66
|
+
Maybe you need to use another semantics for your namespace besides the current
|
67
|
+
environment. You can put a nested namespace in your settings and you are good
|
68
|
+
to go.
|
69
|
+
|
70
|
+
# /some/file/here.yml
|
71
|
+
professional:
|
72
|
+
preferences:
|
73
|
+
order: desc
|
74
|
+
client:
|
75
|
+
prefs:
|
76
|
+
order: asc
|
77
|
+
|
78
|
+
class ProfessionalPreferences < Settingslogic
|
79
|
+
source "/some/file/here.yml"
|
80
|
+
namespace "professional.preferences"
|
81
|
+
end
|
82
|
+
|
83
|
+
>> ProfessionalPreferences.order
|
84
|
+
=> "desc"
|
85
|
+
|
86
|
+
=== 3. Access your settings
|
87
|
+
|
88
|
+
>> Rails.env
|
89
|
+
=> "development"
|
90
|
+
|
91
|
+
>> Settings.cool
|
92
|
+
=> "#<Settingslogic::Settings ... >"
|
93
|
+
|
94
|
+
>> Settings.cool.saweet
|
95
|
+
=> "nested settings"
|
96
|
+
|
97
|
+
>> Settings.neat_setting
|
98
|
+
=> 800
|
99
|
+
|
100
|
+
>> Settings.awesome_setting
|
101
|
+
=> "Did you know 5 + 5 = 10?"
|
102
|
+
|
103
|
+
You can use these settings anywhere, for example in a model:
|
104
|
+
|
105
|
+
class Post < ActiveRecord::Base
|
106
|
+
self.per_page = Settings.pagination.posts_per_page
|
107
|
+
end
|
108
|
+
|
109
|
+
=== 4. Optional / dynamic settings
|
110
|
+
|
111
|
+
Often, you will want to handle defaults in your application logic itself, to reduce the number of settings
|
112
|
+
you need to put in your YAML file. You can access an optional setting by using Hash notation:
|
113
|
+
|
114
|
+
>> Settings.messaging.queue_name
|
115
|
+
=> Exception: Missing setting 'queue_name' in 'message' section in 'application.yml'
|
116
|
+
|
117
|
+
>> Settings.messaging['queue_name']
|
118
|
+
=> nil
|
119
|
+
|
120
|
+
>> Settings.messaging['queue_name'] ||= 'user_mail'
|
121
|
+
=> "user_mail"
|
122
|
+
|
123
|
+
>> Settings.messaging.queue_name
|
124
|
+
=> "user_mail"
|
125
|
+
|
126
|
+
Modifying our model example:
|
127
|
+
|
128
|
+
class Post < ActiveRecord::Base
|
129
|
+
self.per_page = Settings.posts['per_page'] || Settings.pagination.per_page
|
130
|
+
end
|
131
|
+
|
132
|
+
This would allow you to specify a custom value for per_page just for posts, or
|
133
|
+
to fall back to your default value if not specified.
|
134
|
+
|
135
|
+
=== 5. Suppressing Exceptions Conditionally
|
136
|
+
|
137
|
+
Raising exceptions for missing settings helps highlight configuration problems. However, in a
|
138
|
+
Rails app it may make sense to suppress this in production and return nil for missing settings.
|
139
|
+
While it's useful to stop and highlight an error in development or test environments, this is
|
140
|
+
often not the right answer for production.
|
141
|
+
|
142
|
+
class Settings < Settingslogic
|
143
|
+
source "#{Rails.root}/config/application.yml"
|
144
|
+
namespace Rails.env
|
145
|
+
suppress_errors Rails.env.production?
|
146
|
+
end
|
147
|
+
|
148
|
+
>> Settings.non_existent_key
|
149
|
+
=> nil
|
150
|
+
|
151
|
+
== Note on Sinatra / Capistrano / Vlad
|
152
|
+
|
153
|
+
Each of these frameworks uses a +set+ convention for settings, which actually defines methods
|
154
|
+
in the global Object namespace:
|
155
|
+
|
156
|
+
set :application, "myapp" # does "def application" globally
|
157
|
+
|
158
|
+
This can cause collisions with Settingslogic, since those methods are global. Luckily, the
|
159
|
+
solution is to just add a call to load! in your class:
|
160
|
+
|
161
|
+
class Settings < Settingslogic
|
162
|
+
source "#{Rails.root}/config/application.yml"
|
163
|
+
namespace Rails.env
|
164
|
+
load!
|
165
|
+
end
|
166
|
+
|
167
|
+
It's probably always safest to add load! to your class, since this guarantees settings will be
|
168
|
+
loaded at that time, rather than lazily later via method_missing.
|
169
|
+
|
170
|
+
Finally, you can reload all your settings later as well:
|
171
|
+
|
172
|
+
Settings.reload!
|
173
|
+
|
174
|
+
This is useful if you want to support changing your settings YAML without restarting your app.
|
175
|
+
|
176
|
+
== Author
|
177
|
+
|
178
|
+
Copyright (c) 2008-2010 {Ben Johnson}[http://github.com/binarylogic] of {Binary Logic}[http://www.binarylogic.com],
|
179
|
+
released under the MIT license. Support for optional settings and reloading by {Nate Wiger}[http://nate.wiger.org].
|
data/Rakefile
ADDED
data/confing.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "confing"
|
6
|
+
s.version = "0.0.1.beta"
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["Ricardo Valeriano"]
|
9
|
+
s.email = ["ricardo.valeriano@gmail.com"]
|
10
|
+
s.homepage = "http://github.com/ricardovaleriano/confing"
|
11
|
+
s.summary = %q{A thread safe settings solution that uses an ERB enabled YAML file.}
|
12
|
+
s.description = %q{A thread safe settings solution that uses an ERB enabled YAML file.}
|
13
|
+
|
14
|
+
s.add_development_dependency 'rake'
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
end
|
@@ -0,0 +1,204 @@
|
|
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, hash = nil)
|
16
|
+
parts = key.split('.')
|
17
|
+
curs = hash || self
|
18
|
+
while p = parts.shift
|
19
|
+
curs = curs[p] or (yield p if block_given?)
|
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 = file_contents.empty? ? {} : YAML.load(ERB.new(file_contents).result).to_hash
|
104
|
+
hash = namespaced_hash(hash, hash_or_file) if self.class.namespace
|
105
|
+
self.replace hash
|
106
|
+
end
|
107
|
+
@section = section || self.class.source # so end of error says "in application.yml"
|
108
|
+
create_accessors!
|
109
|
+
end
|
110
|
+
|
111
|
+
def namespaced_hash(hash, hash_or_file)
|
112
|
+
if self.class.namespace.include? "."
|
113
|
+
nested_namespaced_hash hash, hash_or_file
|
114
|
+
else
|
115
|
+
hash[namespace] or return missing_key("Missing setting '#{namespace}' in #{hash_or_file}")
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def nested_namespaced_hash(hash, hash_or_file)
|
120
|
+
self.class.get self.class.namespace, hash do |setting|
|
121
|
+
missing_key "Missing setting '#{setting}' for '#{self.class.namespace}' in #{hash_or_file}"
|
122
|
+
end
|
123
|
+
end
|
124
|
+
private :namespaced_hash, :nested_namespaced_hash
|
125
|
+
|
126
|
+
# Called for dynamically-defined keys, and also the first key deferenced at the top-level, if load! is not used.
|
127
|
+
# Otherwise, create_accessors! (called by new) will have created actual methods for each key.
|
128
|
+
def method_missing(name, *args, &block)
|
129
|
+
key = name.to_s
|
130
|
+
return missing_key("Missing setting '#{key}' in #{@section}") unless has_key? key
|
131
|
+
value = fetch(key)
|
132
|
+
create_accessor_for(key)
|
133
|
+
value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value
|
134
|
+
end
|
135
|
+
|
136
|
+
def [](key)
|
137
|
+
fetch(key.to_s, nil)
|
138
|
+
end
|
139
|
+
|
140
|
+
def []=(key,val)
|
141
|
+
# Setting[:key][:key2] = 'value' for dynamic settings
|
142
|
+
val = self.class.new(val, @section) if val.is_a? Hash
|
143
|
+
store(key.to_s, val)
|
144
|
+
create_accessor_for(key, val)
|
145
|
+
end
|
146
|
+
|
147
|
+
# Returns an instance of a Hash object
|
148
|
+
def to_hash
|
149
|
+
Hash[self]
|
150
|
+
end
|
151
|
+
|
152
|
+
# This handles naming collisions with Sinatra/Vlad/Capistrano. Since these use a set()
|
153
|
+
# helper that defines methods in Object, ANY method_missing ANYWHERE picks up the Vlad/Sinatra
|
154
|
+
# settings! So settings.deploy_to title actually calls Object.deploy_to (from set :deploy_to, "host"),
|
155
|
+
# rather than the app_yml['deploy_to'] hash. Jeezus.
|
156
|
+
def create_accessors!
|
157
|
+
self.each do |key,val|
|
158
|
+
create_accessor_for(key)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
# Use instance_eval/class_eval because they're actually more efficient than define_method{}
|
163
|
+
# http://stackoverflow.com/questions/185947/ruby-definemethod-vs-def
|
164
|
+
# http://bmorearty.wordpress.com/2009/01/09/fun-with-rubys-instance_eval-and-class_eval/
|
165
|
+
def create_accessor_for(key, val=nil)
|
166
|
+
return unless key.to_s =~ /^\w+$/ # could have "some-setting:" which blows up eval
|
167
|
+
instance_variable_set("@#{key}", val)
|
168
|
+
self.class.class_eval <<-EndEval
|
169
|
+
def #{key}
|
170
|
+
return @#{key} if @#{key}
|
171
|
+
return missing_key("Missing setting '#{key}' in #{@section}") unless has_key? '#{key}'
|
172
|
+
value = fetch('#{key}')
|
173
|
+
@#{key} = if value.is_a?(Hash)
|
174
|
+
self.class.new(value, "'#{key}' section in #{@section}")
|
175
|
+
elsif value.is_a?(Array) && value.all?{|v| v.is_a? Hash}
|
176
|
+
value.map{|v| self.class.new(v)}
|
177
|
+
else
|
178
|
+
value
|
179
|
+
end
|
180
|
+
end
|
181
|
+
EndEval
|
182
|
+
end
|
183
|
+
|
184
|
+
def symbolize_keys
|
185
|
+
|
186
|
+
inject({}) do |memo, tuple|
|
187
|
+
|
188
|
+
k = (tuple.first.to_sym rescue tuple.first) || tuple.first
|
189
|
+
|
190
|
+
v = k.is_a?(Symbol) ? send(k) : tuple.last # make sure the value is accessed the same way Settings.foo.bar works
|
191
|
+
|
192
|
+
memo[k] = v && v.respond_to?(:symbolize_keys) ? v.symbolize_keys : v #recurse for nested hashes
|
193
|
+
|
194
|
+
memo
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
def missing_key(msg)
|
200
|
+
return nil if self.class.suppress_errors
|
201
|
+
|
202
|
+
raise MissingSetting, msg
|
203
|
+
end
|
204
|
+
end
|
data/spec/settings.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
class Settings < Settingslogic
|
2
|
+
source "#{File.dirname(__FILE__)}/settings.yml"
|
3
|
+
end
|
4
|
+
|
5
|
+
class SettingsInst < Settingslogic
|
6
|
+
end
|
7
|
+
|
8
|
+
class SettingsNestedNamespace < Settingslogic
|
9
|
+
source "#{File.dirname(__FILE__)}/settings.yml"
|
10
|
+
namespace "language.smalltalk"
|
11
|
+
end
|
12
|
+
|
13
|
+
class SettingsInvalidNestedNamespace < Settingslogic
|
14
|
+
source "#{File.dirname(__FILE__)}/settings.yml"
|
15
|
+
namespace "inexistent.namespace.omg"
|
16
|
+
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
data/spec/settings3.rb
ADDED
data/spec/settings4.rb
ADDED
File without changes
|
@@ -0,0 +1,220 @@
|
|
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
|
+
context "using a nested namespace (`language.smalltalk`)" do
|
194
|
+
it "returns the right internal nested value" do
|
195
|
+
expect(SettingsNestedNamespace.paradigm).to eq "object oriented"
|
196
|
+
end
|
197
|
+
|
198
|
+
context "when namespace doesn't exists" do
|
199
|
+
it "raises the missing key error" do
|
200
|
+
expect { SettingsInvalidNestedNamespace.paradigm }.to \
|
201
|
+
raise_error Settingslogic::MissingSetting
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
# Put this test last or else call to .instance will load @instance,
|
207
|
+
# masking bugs.
|
208
|
+
it "should be a hash" do
|
209
|
+
Settings.send(:instance).should be_is_a(Hash)
|
210
|
+
end
|
211
|
+
|
212
|
+
describe "#to_hash" do
|
213
|
+
it "should return a new instance of a Hash object" do
|
214
|
+
Settings.to_hash.should be_kind_of(Hash)
|
215
|
+
Settings.to_hash.class.name.should == "Hash"
|
216
|
+
Settings.to_hash.object_id.should_not == Settings.object_id
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'rspec'
|
4
|
+
require 'settingslogic'
|
5
|
+
require 'settings'
|
6
|
+
require 'settings2'
|
7
|
+
require 'settings3'
|
8
|
+
require 'settings4'
|
9
|
+
require 'settings_empty'
|
10
|
+
|
11
|
+
# Needed to test Settings3
|
12
|
+
Object.send :define_method, 'collides' do
|
13
|
+
'collision'
|
14
|
+
end
|
15
|
+
|
16
|
+
RSpec.configure do |config|
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: confing
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.beta
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ricardo Valeriano
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: A thread safe settings solution that uses an ERB enabled YAML file.
|
28
|
+
email:
|
29
|
+
- ricardo.valeriano@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- .gitignore
|
35
|
+
- Gemfile
|
36
|
+
- Gemfile.lock
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- confing.gemspec
|
41
|
+
- lib/settingslogic.rb
|
42
|
+
- spec/settings.rb
|
43
|
+
- spec/settings.yml
|
44
|
+
- spec/settings2.rb
|
45
|
+
- spec/settings3.rb
|
46
|
+
- spec/settings4.rb
|
47
|
+
- spec/settings_empty.rb
|
48
|
+
- spec/settings_empty.yml
|
49
|
+
- spec/settingslogic_spec.rb
|
50
|
+
- spec/spec_helper.rb
|
51
|
+
homepage: http://github.com/ricardovaleriano/confing
|
52
|
+
licenses: []
|
53
|
+
metadata: {}
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - '>'
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 1.3.1
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 2.1.11
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: A thread safe settings solution that uses an ERB enabled YAML file.
|
74
|
+
test_files: []
|