binarylogic-settingslogic 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -0
- data/CHANGELOG.rdoc +19 -0
- data/LICENSE +20 -0
- data/README.rdoc +86 -0
- data/Rakefile +49 -0
- data/VERSION.yml +4 -0
- data/init.rb +1 -0
- data/lib/settingslogic.rb +13 -0
- data/lib/settingslogic/config.rb +25 -0
- data/lib/settingslogic/settings.rb +80 -0
- data/rails/init.rb +1 -0
- data/test/application.yml +6 -0
- data/test/application2.yml +6 -0
- data/test/application3.yml +9 -0
- data/test/config_test.rb +14 -0
- data/test/setting_test.rb +63 -0
- data/test/test_helper.rb +27 -0
- metadata +81 -0
data/.gitignore
ADDED
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
== 1.0.3 released 2009-04-23
|
2
|
+
|
3
|
+
* Fix Settings initialized with a Hash to work with both symbol and string hash keys.
|
4
|
+
|
5
|
+
== 1.0.2 released 2009-04-09
|
6
|
+
|
7
|
+
* Call key? off of the self in the class level name method.
|
8
|
+
|
9
|
+
== 1.0.1 released 2009-04-02
|
10
|
+
|
11
|
+
* Inherit from hash.
|
12
|
+
|
13
|
+
== 1.0.0 released 2008-12-05
|
14
|
+
|
15
|
+
* Only define methods if we have settings.
|
16
|
+
|
17
|
+
== 0.9.0 released 2008-10-30
|
18
|
+
|
19
|
+
* 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,86 @@
|
|
1
|
+
= Settingslogic
|
2
|
+
|
3
|
+
Settingslogic is an old library of mine that I decided to go ahead and share with the world. It's nothing crazy or new. Just a simple solution to a simple problem. Settingslogic provides globally accessible settings via an ERB enabled YAML file using a singleton design pattern. It has been great for my apps, maybe you will enjoy it too.
|
4
|
+
|
5
|
+
So here is my question to you.....is Settingslogic a great settings solution or the greatest?
|
6
|
+
|
7
|
+
== Helpful links
|
8
|
+
|
9
|
+
* <b>Documentation:</b> http://settingslogic.rubyforge.org
|
10
|
+
* <b>Repository:</b> http://github.com/binarylogic/settingslogic/tree/master
|
11
|
+
* <b>Bugs / feature suggestions:</b> http://binarylogic.lighthouseapp.com/projects/19028-settingslogic
|
12
|
+
|
13
|
+
== Install and use
|
14
|
+
|
15
|
+
sudo gem install settingslogic
|
16
|
+
|
17
|
+
For rails, as a gem (recommended):
|
18
|
+
|
19
|
+
# config/environment.rb
|
20
|
+
config.gem "settingslogic"
|
21
|
+
|
22
|
+
Or as a plugin (for older versions of rails)
|
23
|
+
|
24
|
+
script/plugin install git://github.com/binarylogic/settingslogic.git
|
25
|
+
|
26
|
+
== Create your settings
|
27
|
+
|
28
|
+
By default Settingslogic tries to load config/application.yml. This is just a typical YAML file, notice ERB is allowed.
|
29
|
+
|
30
|
+
# app/config/application.yml
|
31
|
+
defaults: &defaults
|
32
|
+
cool:
|
33
|
+
saweet: nested settings
|
34
|
+
neat_setting: 24
|
35
|
+
awesome_setting: <%= "Did you know 5 + 5 = " + (5 + 5) + "?" %>
|
36
|
+
|
37
|
+
development:
|
38
|
+
<<: *defaults
|
39
|
+
neat_setting: 800
|
40
|
+
|
41
|
+
test:
|
42
|
+
<<: *defaults
|
43
|
+
|
44
|
+
production:
|
45
|
+
<<: *defaults
|
46
|
+
|
47
|
+
Take note of the environment namespacing. If your framework supports environments this is a good way to support environment specific settings. If you are using this in an area where there are no environment disregard the namespacing. It will work just fine without it.
|
48
|
+
|
49
|
+
== Access your settings
|
50
|
+
|
51
|
+
>> RAILS_ENV
|
52
|
+
=> "development"
|
53
|
+
|
54
|
+
>> Settings.cool
|
55
|
+
=> "#<Settingslogic::Settings ... >"
|
56
|
+
|
57
|
+
>> Settings.cool.saweet
|
58
|
+
=> "nested settings"
|
59
|
+
|
60
|
+
>> Settings.neat_setting
|
61
|
+
=> 800
|
62
|
+
|
63
|
+
>> Settings.awesome_setting
|
64
|
+
=> "Did you know 5 + 5 = 10?"
|
65
|
+
|
66
|
+
== Multiple settings
|
67
|
+
|
68
|
+
settings1 = Settings.new(:settings1) # looks for config/settings1.yml
|
69
|
+
settings2 = Settings.new("settings2.yaml") # looks for settings2.yml
|
70
|
+
settings2 = Settings.new("/abs/path/settings2.yaml") # looks for /abs/path/settings2.yml
|
71
|
+
settings3 = Settings.new(:some_setting => "some value")
|
72
|
+
|
73
|
+
== Configure
|
74
|
+
|
75
|
+
Configuration is optional. See Settingslogic::Config for more details.
|
76
|
+
|
77
|
+
# config/initializers/settingslogic.rb
|
78
|
+
Settingslogic::Config.configure do |config|
|
79
|
+
config.file_name = :config # will look for config/config.yml
|
80
|
+
config.file_name = "config" # will look for config
|
81
|
+
config.file_name = "config.yaml" # will look for confg.yaml
|
82
|
+
config.file_name = "/absolute/path/config.yml" # will look for /absolute/path/config.yml
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
Copyright (c) 2008 {Ben Johnson}[http://github.com/binarylogic] of {Binary Logic}[http://www.binarylogic.com], released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "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"
|
10
|
+
gem.homepage = "http://github.com/binarylogic/settingslogic"
|
11
|
+
gem.authors = ["Ben Johnson of Binary Logic"]
|
12
|
+
gem.rubyforge_project = "settingslogic"
|
13
|
+
gem.add_dependency "activesupport"
|
14
|
+
end
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
Rake::TestTask.new(:test) do |test|
|
21
|
+
test.libs << 'lib' << 'test'
|
22
|
+
test.pattern = 'test/**/*_test.rb'
|
23
|
+
test.verbose = true
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'rcov/rcovtask'
|
28
|
+
Rcov::RcovTask.new do |test|
|
29
|
+
test.libs << 'test'
|
30
|
+
test.pattern = 'test/**/*_test.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
rescue LoadError
|
34
|
+
task :rcov do
|
35
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :test
|
40
|
+
|
41
|
+
begin
|
42
|
+
require 'rake/contrib/sshpublisher'
|
43
|
+
namespace :rubyforge do
|
44
|
+
desc "Release gem to RubyForge"
|
45
|
+
task :release => ["rubyforge:release:gem"]
|
46
|
+
end
|
47
|
+
rescue LoadError
|
48
|
+
puts "Rake SshDirPublisher is unavailable or your rubyforge environment is not configured."
|
49
|
+
end
|
data/VERSION.yml
ADDED
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/rails/init.rb"
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require "yaml"
|
2
|
+
require "erb"
|
3
|
+
require File.dirname(__FILE__) + "/settingslogic/config"
|
4
|
+
require File.dirname(__FILE__) + "/settingslogic/settings"
|
5
|
+
|
6
|
+
# Try to load conflicting Settings classes
|
7
|
+
begin
|
8
|
+
Settings
|
9
|
+
rescue(NameError)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Since we don't have a Settings constant, lets go ahead and use it
|
13
|
+
::Settings = Settingslogic::Settings unless defined?(Settings)
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Settingslogic
|
2
|
+
# Sets configuration on Settingslogic.
|
3
|
+
class Config
|
4
|
+
class << self
|
5
|
+
def configure
|
6
|
+
yield self
|
7
|
+
end
|
8
|
+
|
9
|
+
# The name of the file that your settings will be stored for singleton access. Meaning the settings file that will be used when calling methods on the class level:
|
10
|
+
#
|
11
|
+
# Settings.setting1
|
12
|
+
# Settings.setting2
|
13
|
+
# # etc...
|
14
|
+
#
|
15
|
+
# All that you need to do is specify the name of the file. It will automatically look in the config directory.
|
16
|
+
#
|
17
|
+
# * <tt>Default:</tt> :application
|
18
|
+
# * <tt>Accepts:</tt> Symbol or String
|
19
|
+
def settings_file
|
20
|
+
@settings_file ||= :application
|
21
|
+
end
|
22
|
+
attr_writer :settings_file
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module Settingslogic
|
2
|
+
# A simple settings solution using a YAML file. See README for more information.
|
3
|
+
class Settings < Hash
|
4
|
+
class << self
|
5
|
+
def name # :nodoc:
|
6
|
+
instance.key?("name") ? instance.name : super
|
7
|
+
end
|
8
|
+
|
9
|
+
# Resets the singleton instance. Useful if you are changing the configuration on the fly. If you are changing the configuration on the fly you should consider creating instances.
|
10
|
+
def reset!
|
11
|
+
@instance = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
def instance
|
16
|
+
@instance ||= new
|
17
|
+
end
|
18
|
+
|
19
|
+
def method_missing(name, *args, &block)
|
20
|
+
instance.send(name, *args, &block)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
attr_accessor :_settings
|
25
|
+
|
26
|
+
# Initializes a new settings object. You can initialize an object in any of the following ways:
|
27
|
+
#
|
28
|
+
# Settings.new(:application) # will look for config/application.yml
|
29
|
+
# Settings.new("application.yaml") # will look for application.yaml
|
30
|
+
# Settings.new("/var/configs/application.yml") # will look for /var/configs/application.yml
|
31
|
+
# Settings.new(:config1 => 1, :config2 => 2)
|
32
|
+
#
|
33
|
+
# Basically if you pass a symbol it will look for that file in the configs directory of your rails app, if you are using this in rails. If you pass a string it should be an absolute path to your settings file.
|
34
|
+
# Then you can pass a hash, and it just allows you to access the hash via methods.
|
35
|
+
def initialize(name_or_hash = Config.settings_file)
|
36
|
+
case name_or_hash
|
37
|
+
when Hash
|
38
|
+
self.update name_or_hash
|
39
|
+
when String, Symbol
|
40
|
+
root_path = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/config/" : ""
|
41
|
+
file_path = name_or_hash.is_a?(Symbol) ? "#{root_path}#{name_or_hash}.yml" : name_or_hash
|
42
|
+
self.update YAML.load(ERB.new(File.read(file_path)).result).to_hash
|
43
|
+
else
|
44
|
+
raise ArgumentError.new("Your settings must be a hash, a symbol representing the name of the .yml file in your config directory, or a string representing the abosolute path to your settings file.")
|
45
|
+
end
|
46
|
+
if defined?(RAILS_ENV)
|
47
|
+
rails_env = self.keys.include?(RAILS_ENV) ? RAILS_ENV : RAILS_ENV.to_sym
|
48
|
+
self.update self[rails_env] if self[rails_env]
|
49
|
+
end
|
50
|
+
define_settings!
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
def method_missing(name, *args, &block)
|
55
|
+
raise NoMethodError.new("no configuration was specified for #{name}")
|
56
|
+
end
|
57
|
+
|
58
|
+
def define_settings!
|
59
|
+
self.each do |key, value|
|
60
|
+
case value
|
61
|
+
when Hash
|
62
|
+
instance_eval <<-"end_eval", __FILE__, __LINE__
|
63
|
+
def #{key}
|
64
|
+
@#{key} ||= self.class.new(self[#{key.inspect}])
|
65
|
+
end
|
66
|
+
end_eval
|
67
|
+
else
|
68
|
+
instance_eval <<-"end_eval", __FILE__, __LINE__
|
69
|
+
def #{key}
|
70
|
+
@#{key} ||= self[#{key.inspect}]
|
71
|
+
end
|
72
|
+
def #{key}=(value)
|
73
|
+
@#{key} = value
|
74
|
+
end
|
75
|
+
end_eval
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "settingslogic"
|
data/test/config_test.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class TestConfig < Test::Unit::TestCase
|
4
|
+
def test_settings_file
|
5
|
+
Settingslogic::Config.configure do |config|
|
6
|
+
config.settings_file = File.dirname(__FILE__) + '/application2.yml'
|
7
|
+
end
|
8
|
+
|
9
|
+
Settings.reset!
|
10
|
+
assert_equal "BenJohnson", Settings.neat.cool.awesome
|
11
|
+
assert_equal 5, Settings.silly
|
12
|
+
assert_equal 25, Settings.fun
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class TestSetting < Test::Unit::TestCase
|
4
|
+
def test_conflicting_class_methods
|
5
|
+
assert_equal "test", Settings.name
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_singleton_access
|
9
|
+
assert_equal Settings, Settings.setting1.class
|
10
|
+
assert_equal "saweet", Settings.setting1.setting1_child
|
11
|
+
assert_equal 5, Settings.setting2
|
12
|
+
assert_equal 25, Settings.setting3
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_instances
|
16
|
+
settings1 = Settings.new(File.dirname(__FILE__) + '/application.yml')
|
17
|
+
assert_equal "saweet", settings1.setting1.setting1_child
|
18
|
+
assert_equal 5, settings1.setting2
|
19
|
+
assert_equal 25, settings1.setting3
|
20
|
+
|
21
|
+
settings2 = Settings.new(File.dirname(__FILE__) + '/application2.yml')
|
22
|
+
assert_equal "BenJohnson", settings2.neat.cool.awesome
|
23
|
+
assert_equal 5, settings2.silly
|
24
|
+
assert_equal 25, settings2.fun
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_method_missing
|
28
|
+
assert_raise(NoMethodError) { Settings.doesnt_exist }
|
29
|
+
settings1 = Settings.new(File.dirname(__FILE__) + '/application.yml')
|
30
|
+
assert_raise(NoMethodError) { settings1.doesnt_exist }
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_initialized_with_hash
|
34
|
+
settings1 = Settings.new(
|
35
|
+
:silly => 5,
|
36
|
+
'fun' => 25,
|
37
|
+
:neat => { 'cool' => { :awesome => "BenJohnson" } }
|
38
|
+
)
|
39
|
+
assert_equal "BenJohnson", settings1.neat.cool.awesome
|
40
|
+
assert_equal 5, settings1.silly
|
41
|
+
assert_equal 25, settings1.fun
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_environment_specific_settings
|
45
|
+
in_test_environment do
|
46
|
+
settings1 = Settings.new(File.dirname(__FILE__) + '/application3.yml')
|
47
|
+
assert_equal 25, settings1.fun
|
48
|
+
assert_equal "test_specific", settings1.silly
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_environment_specific_settings_when_initialized_with_hash
|
53
|
+
in_test_environment do
|
54
|
+
settings1 = Settings.new(
|
55
|
+
:silly => 5,
|
56
|
+
'fun' => 25,
|
57
|
+
:test => { :silly => "test_specific" }
|
58
|
+
)
|
59
|
+
assert_equal 25, settings1.fun
|
60
|
+
assert_equal "test_specific", settings1.silly
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "rubygems"
|
3
|
+
require "ruby-debug"
|
4
|
+
require File.dirname(__FILE__) + "/../lib/settingslogic"
|
5
|
+
|
6
|
+
class Test::Unit::TestCase
|
7
|
+
def configure
|
8
|
+
Settingslogic::Config.configure do |config|
|
9
|
+
config.settings_file = File.dirname(__FILE__) + "/application.yml"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def setup
|
14
|
+
configure
|
15
|
+
end
|
16
|
+
|
17
|
+
def teardown
|
18
|
+
configure
|
19
|
+
Settingslogic::Settings.reset!
|
20
|
+
end
|
21
|
+
|
22
|
+
def in_test_environment(&block)
|
23
|
+
Settingslogic::Settings.const_set :RAILS_ENV, "test"
|
24
|
+
block.call
|
25
|
+
Settingslogic::Settings.send :remove_const, :RAILS_ENV
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: binarylogic-settingslogic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Johnson of Binary Logic
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-28 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activesupport
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: bjohnson@binarylogic.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .gitignore
|
36
|
+
- CHANGELOG.rdoc
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION.yml
|
41
|
+
- init.rb
|
42
|
+
- lib/settingslogic.rb
|
43
|
+
- lib/settingslogic/config.rb
|
44
|
+
- lib/settingslogic/settings.rb
|
45
|
+
- rails/init.rb
|
46
|
+
- test/application.yml
|
47
|
+
- test/application2.yml
|
48
|
+
- test/application3.yml
|
49
|
+
- test/config_test.rb
|
50
|
+
- test/setting_test.rb
|
51
|
+
- test/test_helper.rb
|
52
|
+
has_rdoc: false
|
53
|
+
homepage: http://github.com/binarylogic/settingslogic
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options:
|
56
|
+
- --charset=UTF-8
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
version:
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project: settingslogic
|
74
|
+
rubygems_version: 1.2.0
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: A simple and straightforward settings solution that uses an ERB enabled YAML file and a singleton design pattern.
|
78
|
+
test_files:
|
79
|
+
- test/config_test.rb
|
80
|
+
- test/setting_test.rb
|
81
|
+
- test/test_helper.rb
|