settingslogic 1.0.4 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +4 -0
- data/README.rdoc +25 -32
- data/Rakefile +11 -27
- data/VERSION.yml +2 -2
- data/lib/settingslogic.rb +83 -10
- data/settingslogic.gemspec +15 -17
- data/spec/settings.rb +3 -0
- data/{test/application.yml → spec/settings.yml} +0 -0
- data/spec/settings2.rb +4 -0
- data/spec/settingslogic_spec.rb +27 -0
- data/spec/spec_helper.rb +12 -0
- metadata +14 -25
- data/lib/settingslogic/config.rb +0 -25
- data/lib/settingslogic/settings.rb +0 -80
- data/test/application2.yml +0 -6
- data/test/application3.yml +0 -9
- data/test/config_test.rb +0 -14
- data/test/setting_test.rb +0 -63
- data/test/test_helper.rb +0 -27
data/CHANGELOG.rdoc
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
== 2.0.0 released 2009-08-22
|
2
|
+
|
3
|
+
* Less magic, instead of automatically defining a Settings constant, you should define your own constant. See the readme for an example.
|
4
|
+
|
1
5
|
== 1.0.3 released 2009-04-23
|
2
6
|
|
3
7
|
* Fix Settings initialized with a Hash to work with both symbol and string hash keys.
|
data/README.rdoc
CHANGED
@@ -1,31 +1,45 @@
|
|
1
1
|
= Settingslogic
|
2
2
|
|
3
|
-
Settingslogic is
|
3
|
+
Settingslogic is a simple configuration / settings solution that uses an ERB enabled YAML file. It has been great for my apps, maybe you will enjoy it too.
|
4
4
|
|
5
5
|
So here is my question to you.....is Settingslogic a great settings solution or the greatest?
|
6
6
|
|
7
7
|
== Helpful links
|
8
8
|
|
9
|
-
* <b>Documentation:</b> http://settingslogic
|
9
|
+
* <b>Documentation:</b> http://rdoc.info/projects/binarylogic/settingslogic
|
10
10
|
* <b>Repository:</b> http://github.com/binarylogic/settingslogic/tree/master
|
11
|
-
* <b>
|
11
|
+
* <b>Issues:</b> http://github.com/binarylogic/settingslogic/issues
|
12
12
|
|
13
13
|
== Install and use
|
14
14
|
|
15
|
+
Install from rubyforge:
|
16
|
+
|
15
17
|
sudo gem install settingslogic
|
16
18
|
|
17
|
-
|
19
|
+
Install from github:
|
18
20
|
|
19
|
-
|
20
|
-
config.gem "settingslogic"
|
21
|
+
sudo gem install binarylogic-settingslogic
|
21
22
|
|
22
|
-
Or as a plugin
|
23
|
+
Or as a plugin
|
23
24
|
|
24
25
|
script/plugin install git://github.com/binarylogic/settingslogic.git
|
25
26
|
|
26
|
-
==
|
27
|
+
== 1. Define your constant
|
28
|
+
|
29
|
+
Instead of defining a Settings constant for you, that task is left to you. Simply create a class in your application that looks like:
|
30
|
+
|
31
|
+
class Settings < Settingslogic
|
32
|
+
source "#{Rails.root}/config/application.yml"
|
33
|
+
namespace Rails.env
|
34
|
+
end
|
35
|
+
|
36
|
+
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 this file in a rails app is models/settings.rb
|
37
|
+
|
38
|
+
I felt adding a settings file in your app was more straightforward, less tricky, and more flexible.
|
39
|
+
|
40
|
+
== 2. Create your settings
|
27
41
|
|
28
|
-
|
42
|
+
Notice above we specified an absolute path to our settings file called "application.yml". This is just a typical YAML file. Also notice above that we specified a namespace for our environment. This allows us to namespace our configuration depending on our environment:
|
29
43
|
|
30
44
|
# app/config/application.yml
|
31
45
|
defaults: &defaults
|
@@ -44,12 +58,10 @@ By default Settingslogic tries to load config/application.yml. This is just a ty
|
|
44
58
|
production:
|
45
59
|
<<: *defaults
|
46
60
|
|
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
61
|
== Access your settings
|
50
62
|
|
51
|
-
>>
|
52
|
-
=>
|
63
|
+
>> Rails.env.development?
|
64
|
+
=> true
|
53
65
|
|
54
66
|
>> Settings.cool
|
55
67
|
=> "#<Settingslogic::Settings ... >"
|
@@ -63,24 +75,5 @@ Take note of the environment namespacing. If your framework supports environment
|
|
63
75
|
>> Settings.awesome_setting
|
64
76
|
=> "Did you know 5 + 5 = 10?"
|
65
77
|
|
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
78
|
|
86
79
|
Copyright (c) 2008 {Ben Johnson}[http://github.com/binarylogic] of {Binary Logic}[http://www.binarylogic.com], released under the MIT license
|
data/Rakefile
CHANGED
@@ -10,40 +10,24 @@ begin
|
|
10
10
|
gem.homepage = "http://github.com/binarylogic/settingslogic"
|
11
11
|
gem.authors = ["Ben Johnson of Binary Logic"]
|
12
12
|
gem.rubyforge_project = "settingslogic"
|
13
|
-
gem.add_dependency "activesupport"
|
14
13
|
end
|
14
|
+
Jeweler::RubyforgeTasks.new
|
15
15
|
rescue LoadError
|
16
16
|
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
17
17
|
end
|
18
18
|
|
19
|
-
require 'rake/
|
20
|
-
Rake::
|
21
|
-
|
22
|
-
|
23
|
-
test.verbose = true
|
19
|
+
require 'spec/rake/spectask'
|
20
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
21
|
+
spec.libs << 'lib' << 'spec'
|
22
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
24
23
|
end
|
25
24
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
25
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
26
|
+
spec.libs << 'lib' << 'spec'
|
27
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
28
|
+
spec.rcov = true
|
37
29
|
end
|
38
30
|
|
39
|
-
task :
|
31
|
+
task :spec => :check_dependencies
|
40
32
|
|
41
|
-
|
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
|
33
|
+
task :default => :spec
|
data/VERSION.yml
CHANGED
data/lib/settingslogic.rb
CHANGED
@@ -1,13 +1,86 @@
|
|
1
1
|
require "yaml"
|
2
2
|
require "erb"
|
3
|
-
require File.dirname(__FILE__) + "/settingslogic/config"
|
4
|
-
require File.dirname(__FILE__) + "/settingslogic/settings"
|
5
3
|
|
6
|
-
#
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
4
|
+
# A simple settings solution using a YAML file. See README for more information.
|
5
|
+
class Settingslogic < Hash
|
6
|
+
class UndefinedSetting < StandardError; end
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def name # :nodoc:
|
10
|
+
instance.key?("name") ? instance.name : super
|
11
|
+
end
|
12
|
+
|
13
|
+
def source(value = nil)
|
14
|
+
if value.nil?
|
15
|
+
@source
|
16
|
+
else
|
17
|
+
@source = value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def namespace(value = nil)
|
22
|
+
if value.nil?
|
23
|
+
@namespace
|
24
|
+
else
|
25
|
+
@namespace = value
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def instance
|
31
|
+
@instance ||= new
|
32
|
+
end
|
33
|
+
|
34
|
+
def method_missing(name, *args, &block)
|
35
|
+
instance.send(name, *args, &block)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Initializes a new settings object. You can initialize an object in any of the following ways:
|
40
|
+
#
|
41
|
+
# Settings.new(:application) # will look for config/application.yml
|
42
|
+
# Settings.new("application.yaml") # will look for application.yaml
|
43
|
+
# Settings.new("/var/configs/application.yml") # will look for /var/configs/application.yml
|
44
|
+
# Settings.new(:config1 => 1, :config2 => 2)
|
45
|
+
#
|
46
|
+
# 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.
|
47
|
+
# Then you can pass a hash, and it just allows you to access the hash via methods.
|
48
|
+
def initialize(hash_or_file = self.class.source)
|
49
|
+
case hash_or_file
|
50
|
+
when Hash
|
51
|
+
self.update hash_or_file
|
52
|
+
else
|
53
|
+
self.update YAML.load(ERB.new(File.read(hash_or_file)).result).to_hash
|
54
|
+
self.update self[self.class.namespace] if self.class.namespace
|
55
|
+
end
|
56
|
+
|
57
|
+
define_settings!
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
def method_missing(name, *args, &block)
|
62
|
+
raise UndefinedSetting.new("The '#{name}' was not found in your configuration file: #{self.class.source}")
|
63
|
+
end
|
64
|
+
|
65
|
+
def define_settings!
|
66
|
+
self.each do |key, value|
|
67
|
+
case value
|
68
|
+
when Hash
|
69
|
+
instance_eval <<-"end_eval", __FILE__, __LINE__
|
70
|
+
def #{key}
|
71
|
+
@#{key} ||= self.class.new(self[#{key.inspect}])
|
72
|
+
end
|
73
|
+
end_eval
|
74
|
+
else
|
75
|
+
instance_eval <<-"end_eval", __FILE__, __LINE__
|
76
|
+
def #{key}
|
77
|
+
@#{key} ||= self[#{key.inspect}]
|
78
|
+
end
|
79
|
+
def #{key}=(value)
|
80
|
+
@#{key} = value
|
81
|
+
end
|
82
|
+
end_eval
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/settingslogic.gemspec
CHANGED
@@ -1,12 +1,15 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
1
4
|
# -*- encoding: utf-8 -*-
|
2
5
|
|
3
6
|
Gem::Specification.new do |s|
|
4
7
|
s.name = %q{settingslogic}
|
5
|
-
s.version = "
|
8
|
+
s.version = "2.0.0"
|
6
9
|
|
7
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
11
|
s.authors = ["Ben Johnson of Binary Logic"]
|
9
|
-
s.date = %q{2009-
|
12
|
+
s.date = %q{2009-08-22}
|
10
13
|
s.email = %q{bjohnson@binarylogic.com}
|
11
14
|
s.extra_rdoc_files = [
|
12
15
|
"LICENSE",
|
@@ -21,27 +24,25 @@ Gem::Specification.new do |s|
|
|
21
24
|
"VERSION.yml",
|
22
25
|
"init.rb",
|
23
26
|
"lib/settingslogic.rb",
|
24
|
-
"lib/settingslogic/config.rb",
|
25
|
-
"lib/settingslogic/settings.rb",
|
26
27
|
"rails/init.rb",
|
27
28
|
"settingslogic.gemspec",
|
28
|
-
"
|
29
|
-
"
|
30
|
-
"
|
31
|
-
"
|
32
|
-
"
|
33
|
-
"test/test_helper.rb"
|
29
|
+
"spec/settings.rb",
|
30
|
+
"spec/settings.yml",
|
31
|
+
"spec/settings2.rb",
|
32
|
+
"spec/settingslogic_spec.rb",
|
33
|
+
"spec/spec_helper.rb"
|
34
34
|
]
|
35
35
|
s.homepage = %q{http://github.com/binarylogic/settingslogic}
|
36
36
|
s.rdoc_options = ["--charset=UTF-8"]
|
37
37
|
s.require_paths = ["lib"]
|
38
38
|
s.rubyforge_project = %q{settingslogic}
|
39
|
-
s.rubygems_version = %q{1.3.
|
39
|
+
s.rubygems_version = %q{1.3.5}
|
40
40
|
s.summary = %q{A simple and straightforward settings solution that uses an ERB enabled YAML file and a singleton design pattern.}
|
41
41
|
s.test_files = [
|
42
|
-
"
|
43
|
-
"
|
44
|
-
"
|
42
|
+
"spec/settings.rb",
|
43
|
+
"spec/settings2.rb",
|
44
|
+
"spec/settingslogic_spec.rb",
|
45
|
+
"spec/spec_helper.rb"
|
45
46
|
]
|
46
47
|
|
47
48
|
if s.respond_to? :specification_version then
|
@@ -49,11 +50,8 @@ Gem::Specification.new do |s|
|
|
49
50
|
s.specification_version = 3
|
50
51
|
|
51
52
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
52
|
-
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
53
53
|
else
|
54
|
-
s.add_dependency(%q<activesupport>, [">= 0"])
|
55
54
|
end
|
56
55
|
else
|
57
|
-
s.add_dependency(%q<activesupport>, [">= 0"])
|
58
56
|
end
|
59
57
|
end
|
data/spec/settings.rb
ADDED
File without changes
|
data/spec/settings2.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
|
2
|
+
|
3
|
+
describe "Settingslogic" do
|
4
|
+
it "should be a hash" do
|
5
|
+
Settings.send(:instance).should be_is_a(Hash)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should access settings" do
|
9
|
+
Settings.setting1.should == {"setting1_child" => "saweet"}
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should access nested settings" do
|
13
|
+
Settings.setting1.setting1_child.should == "saweet"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should enable erb" do
|
17
|
+
Settings.setting3.should == 25
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should raise an error for unfound settings" do
|
21
|
+
lambda { Settings.undefined }.should raise_error(Settingslogic::UndefinedSetting)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should namespace settings" do
|
25
|
+
Settings2.setting1_child.should == "saweet"
|
26
|
+
end
|
27
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'ruby-debug'
|
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
|
+
|
11
|
+
Spec::Runner.configure do |config|
|
12
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: settingslogic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Johnson of Binary Logic
|
@@ -9,19 +9,10 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-08-22 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
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:
|
14
|
+
dependencies: []
|
15
|
+
|
25
16
|
description:
|
26
17
|
email: bjohnson@binarylogic.com
|
27
18
|
executables: []
|
@@ -40,16 +31,13 @@ files:
|
|
40
31
|
- VERSION.yml
|
41
32
|
- init.rb
|
42
33
|
- lib/settingslogic.rb
|
43
|
-
- lib/settingslogic/config.rb
|
44
|
-
- lib/settingslogic/settings.rb
|
45
34
|
- rails/init.rb
|
46
35
|
- settingslogic.gemspec
|
47
|
-
-
|
48
|
-
-
|
49
|
-
-
|
50
|
-
-
|
51
|
-
-
|
52
|
-
- test/test_helper.rb
|
36
|
+
- spec/settings.rb
|
37
|
+
- spec/settings.yml
|
38
|
+
- spec/settings2.rb
|
39
|
+
- spec/settingslogic_spec.rb
|
40
|
+
- spec/spec_helper.rb
|
53
41
|
has_rdoc: true
|
54
42
|
homepage: http://github.com/binarylogic/settingslogic
|
55
43
|
licenses: []
|
@@ -74,11 +62,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
62
|
requirements: []
|
75
63
|
|
76
64
|
rubyforge_project: settingslogic
|
77
|
-
rubygems_version: 1.3.
|
65
|
+
rubygems_version: 1.3.5
|
78
66
|
signing_key:
|
79
67
|
specification_version: 3
|
80
68
|
summary: A simple and straightforward settings solution that uses an ERB enabled YAML file and a singleton design pattern.
|
81
69
|
test_files:
|
82
|
-
-
|
83
|
-
-
|
84
|
-
-
|
70
|
+
- spec/settings.rb
|
71
|
+
- spec/settings2.rb
|
72
|
+
- spec/settingslogic_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
data/lib/settingslogic/config.rb
DELETED
@@ -1,25 +0,0 @@
|
|
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
|
@@ -1,80 +0,0 @@
|
|
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/test/application2.yml
DELETED
data/test/application3.yml
DELETED
data/test/config_test.rb
DELETED
@@ -1,14 +0,0 @@
|
|
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
|
data/test/setting_test.rb
DELETED
@@ -1,63 +0,0 @@
|
|
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
DELETED
@@ -1,27 +0,0 @@
|
|
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
|