simpleconfig 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,158 @@
1
+ Simple Config is a plugin designed to make application-wide configuration settings easy to set and access in an object-oriented fashion.
2
+
3
+ Rails already provides a way of configuring the framework on a per-environment basis but other than global variables/constants set in each environment file or environment.rb, there isn't a built-in way of providing application-specific settings.
4
+
5
+ One simple solution is to simply put all of your app configuration into a YAML file and load this somewhere in your environment, but here at Reevoo we wanted something a little bit more flexible that we could use across all of our applications and Simple Config is what we came up with.
6
+
7
+ h2. Getting started
8
+
9
+ The plugin comes with a rake task to get you up and running quickly, so start by running that.
10
+
11
+ $ rake simple_config:setup
12
+
13
+ This will create a config/settings folder and a blank settings file for each of the main Rails environments. It will also create a copy of the SimpleConfig initializer in config/initializers/configuration.rb[1].
14
+
15
+ Now, if you open up the configuration.rb initializer, you will see something like this:
16
+
17
+ <pre><code class="ruby">
18
+ SimpleConfig.for :application do
19
+
20
+ # your app configuration here
21
+
22
+ load File.join(RAILS_ROOT, 'config', "settings", "#{RAILS_ENV}.rb"), :if_exists? => true
23
+ load File.join(RAILS_ROOT, 'config', "settings", "local.rb"), :if_exists? => true
24
+
25
+ end
26
+ </code></pre>
27
+
28
+ This is where you can set any configuration variables that are required across all Rails environments. The <code>load</code> method works just like Ruby's built-in load method, except the contents of the file it loads are evaluated within the context of the <code>SimpleConfig.for</code> block. The <code>:if_exists?</code> flag, when set to true, means that the file will only be loaded if it exists, otherwise it will simply be ignored.
29
+
30
+ Variables can be overwritten, and are defined in the order that they are loaded, so you can set up default values in the above file and override them in the environment files.
31
+
32
+ As well as loading a settings file for your current Rails environment, a file called "local.rb" is loaded which is designed as a place for you to override variables specific to your own development environment -- you can just keep a copy of this locally without having to check it into your version control system[2].
33
+
34
+ h2. Variables
35
+
36
+ h3. Setting Variables
37
+
38
+ Setting variables is simple and will be familiar to anybody who has used Capistrano. Whether in your main <code>SimpleConfig.for</code> block in configuration.rb, or one of your external settings files, use the <code>set</code> method:
39
+
40
+ <pre><code class="ruby">
41
+ SimpleConfig.for :application do
42
+ set :my_variable, 'hello world'
43
+ end
44
+ </code></pre>
45
+
46
+ SimpleConfig also supports a form of namespacing that allows you to group logical sets of variables together:
47
+
48
+ <pre><code class="ruby">
49
+ SimpleConfig.for :application do
50
+ group :awesome_stuff do
51
+ set :my_variable, 'hello world'
52
+ end
53
+ end
54
+ </code></pre>
55
+
56
+ Both the <code>set</code> and <code>load</code> methods are available within <code>group</code> blocks and files loaded inside groups will be evaluated in the context of that group.
57
+
58
+ Whilst I'd recommend not nesting your groups more than one-level, there is no limit on how deep they can be nested.
59
+
60
+ h3. Unsetting variables
61
+
62
+ Sometimes you might want to completely delete a variable from the collection. Simply setting its value to nil doesn't work because <code>nil</code> might be a valid value.
63
+ You can delete a variable using the <code>unset</code> method.
64
+
65
+ <pre><code class="ruby">
66
+ SimpleConfig.for :application do
67
+ set :my_variable, 'hello world'
68
+
69
+ ...
70
+
71
+ unset :my_variable
72
+ end
73
+ </code></pre>
74
+
75
+ For instance, this is useful to remove global settings at environment level instead of overwriting the default value with a nonsense-one.
76
+ <code>unset</code> returns the value of the variable just in case you need to use it elsewhere.
77
+
78
+ h3. Does a specific variable exist?
79
+
80
+ I don't know but you can check it yourself using <code>exists?</code> method.
81
+
82
+ <pre><code class="ruby">
83
+ config = SimpleConfig.for :application do
84
+ set :my_variable, 'hello world'
85
+ end
86
+
87
+ # write some nice code
88
+ config.exists? :my_variable # => true
89
+ config.exists? :your_variable # => false
90
+ </code></pre>
91
+
92
+ h2. Accessing your configuration
93
+
94
+ SimpleConfig allows you set as many separate configurations as you like using the <code>SimpleConfig.for</code> method, which takes a symbol representing the configuration name, although most people will just create a single "application" config as above. To access this config from anywhere in your application, you can also use <code>SimpleConfig.for</code> method without a block, which always returns the named configuration object.
95
+
96
+ It is worth pointing out that <code>SimpleConfig.for</code> provides an almost singleton-style access to a particular named config. Calling <code>SimpleConfig.for</code> with a block a second time for a particular named configuration will simply extend the existing configuration, not overwrite it.
97
+
98
+ Once you have a reference to your configuration object, you can access variables using method access. Given the above example, <code>:my_variable</code> would be accessed in the following way:
99
+
100
+ <pre><code class="ruby">
101
+ config = SimpleConfig.for(:application)
102
+ config.my_variable # => "hello world"
103
+ </code></pre>
104
+
105
+ Accessing grouped variables works as you would expect:
106
+
107
+ <pre><code class="ruby">
108
+ config = SimpleConfig.for(:application)
109
+ config.awesome_stuff.my_variable # => "hello world"
110
+ </code></pre>
111
+
112
+ h2. Using your configuration in your Rails app
113
+
114
+ The plugin provides a convenient mixin for your <code>ApplicationController</code> to make configuration access as simple as possible. Assuming a configuration called "application" (as in the above examples), it defines a <code>config</code> method which can be used in any of your controllers. It also defines this as a method as a view helper using the Rails <code>helper_method</code> macro so you can access configuration data in your views.
115
+
116
+ Note - there is no direct way of accessing your configuration variables in your models other than making a direct call to <code>SimpleConfig.for</code>. I'd recommend designing your models in such a way that configuration data can be passed into them at runtime as method arguments by your controller to avoid coupling your model to SimpleConfig.
117
+
118
+ To use the mixin, simply include it in your <code>ApplicationController</code>:
119
+
120
+ <pre><code class="ruby">
121
+ class ApplicationController < ActionController::Base
122
+ include SimpleConfig::ControllerMixin
123
+ end
124
+ </code></pre>
125
+
126
+ Then in your controllers:
127
+
128
+ <pre><code class="ruby">
129
+ class MyController < ApplicationController
130
+ def index
131
+ render :text => config.my_config_variable
132
+ end
133
+ end
134
+ </code></pre>
135
+
136
+ The mixin provides also a class-level <code>config</code> method to access the configuration when you don't have a controller instance available.
137
+
138
+ <pre><code class="ruby">
139
+ class MyController < ApplicationController
140
+ protect_from_forgery :secret => config.secret_token
141
+
142
+ def index
143
+ render :text => config.my_config_variable
144
+ end
145
+ end
146
+ </code></pre>
147
+
148
+
149
+ fn1(footnote). SimpleConfig was designed with Rails 2.0 in mind but it has been tested with Rails 1.2. To use the Rails-style initializers that SimpleConfig takes advantage of in Rails 1.2, simply add this to the bottom of your environment.rb file:
150
+
151
+ <pre><code class="ruby">
152
+ # backported Rails 2.x initializer folder functionality
153
+ Dir[File.join(RAILS_ROOT, 'config', 'initializers', '*.rb')].each do |initializer|
154
+ load initializer
155
+ end
156
+ </code></pre>
157
+
158
+ fn2(footnote). In fact, I recommend you make sure your version control system ignores this file otherwise you risk checking in a file that will override values in production! If you are using Subversion, simply add local.rb to the svn:ignore property for the config/settings folder.
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the simpleconfig plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the simpleconfig plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'Simpleconfig'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
@@ -0,0 +1,7 @@
1
+ unless Rails.respond_to?(:env)
2
+ Rails.instance_eval do
3
+ def env
4
+ RAILS_ENV
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,157 @@
1
+ require 'yaml'
2
+
3
+ unless defined?(Rails)
4
+ class Object
5
+ def returning(object, &block)
6
+ yield object if block_given?
7
+ object
8
+ end
9
+ end
10
+ end
11
+
12
+ module SimpleConfig
13
+
14
+ class << self
15
+ def for(config_name, &block)
16
+ default_manager.for(config_name, &block)
17
+ end
18
+
19
+ def default_manager
20
+ @default_manager ||= Manager.new
21
+ end
22
+ end
23
+
24
+ class Manager
25
+ def initialize
26
+ @configs = {}
27
+ end
28
+
29
+ def for(config_name, &block)
30
+ returning @configs[config_name] ||= Config.new do |config|
31
+ config.configure(&block) if block_given?
32
+ end
33
+ end
34
+ end
35
+
36
+ class Config
37
+ def initialize
38
+ @groups = {}
39
+ @settings = {}
40
+ end
41
+
42
+ def configure(&block)
43
+ instance_eval(&block)
44
+ end
45
+
46
+ def group(name, &block)
47
+ returning @groups[name] ||= Config.new do |group|
48
+ group.configure(&block) if block_given?
49
+ end
50
+ end
51
+
52
+ def set(key, value)
53
+ @settings[key] = value
54
+ end
55
+
56
+ def get(key)
57
+ @settings[key]
58
+ end
59
+
60
+ #
61
+ # Unsets any variable with given +key+
62
+ # and returns variable value if it exists, nil otherwise.
63
+ # Any successive call to exists? :key will return false.
64
+ #
65
+ # exists? :bar # => false
66
+ #
67
+ # set :bar, 'foo'
68
+ # exists? :bar # => true
69
+ #
70
+ # unset :bar # => 'foo'
71
+ # exists? :bar # => false
72
+ #
73
+ def unset(key)
74
+ @settings.delete(key)
75
+ end
76
+
77
+ #
78
+ # Returns whether a variable with given +key+ is set.
79
+ #
80
+ # Please note that this method doesn't care about variable value.
81
+ # A nil variable is considered as set.
82
+ #
83
+ # exists? :bar # => false
84
+ #
85
+ # set :bar, 'foo'
86
+ # exists? :bar # => true
87
+ #
88
+ # set :bar, nil
89
+ # exists? :bar # => true
90
+ #
91
+ # Use unset to completely remove a variable from the collection.
92
+ #
93
+ # set :bar, 'foo'
94
+ # exists? :bar # => true
95
+ #
96
+ # unset :bar
97
+ # exists? :bar # => false
98
+ #
99
+ def exists?(key)
100
+ @settings.key?(key)
101
+ end
102
+
103
+ def load(external_config_file, options={})
104
+ options = {:if_exists? => false}.merge(options)
105
+
106
+ if options[:if_exists?]
107
+ return unless File.exist?(external_config_file)
108
+ end
109
+
110
+ case File.extname(external_config_file)
111
+ when /rb/
112
+ instance_eval(File.read(external_config_file))
113
+ when /yml|yaml/
114
+ YAMLParser.parse_contents_of_file(external_config_file).parse_into(self)
115
+ end
116
+ end
117
+
118
+ private
119
+ def method_missing(method_name, *args)
120
+ case true
121
+ when @groups.key?(method_name)
122
+ return @groups[method_name]
123
+ when @settings.key?(method_name)
124
+ return get(method_name)
125
+ else
126
+ super(method_name, *args)
127
+ end
128
+ end
129
+ end
130
+
131
+ class YAMLParser
132
+ def initialize(raw_yaml_data)
133
+ @data = YAML.load(raw_yaml_data)
134
+ end
135
+
136
+ def self.parse_contents_of_file(yaml_file)
137
+ new(File.read(yaml_file))
138
+ end
139
+
140
+ def parse_into(config)
141
+ @data.each do |key, value|
142
+ parse(key, value, config)
143
+ end
144
+ end
145
+
146
+ private
147
+ def parse(key, value, config)
148
+ if value.is_a?(Hash)
149
+ group = config.group(key.to_sym)
150
+ value.each { |key, value| parse(key, value, group) }
151
+ else
152
+ config.set(key.to_sym, value)
153
+ end
154
+ end
155
+ end
156
+
157
+ end
@@ -0,0 +1,31 @@
1
+ module SimpleConfig
2
+ module ControllerMixin
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.class_eval do
7
+ include InstanceMethods
8
+ helper_method :config
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+
14
+ # Returns the application config.
15
+ def config
16
+ SimpleConfig.for(:application)
17
+ end
18
+
19
+ end
20
+
21
+ module InstanceMethods
22
+
23
+ # Instance-level proxy to class-level +config+ method.
24
+ def config
25
+ self.class.config
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,40 @@
1
+ require 'uri'
2
+
3
+ module SimpleConfig
4
+ module Utilities
5
+ class NetworkHost
6
+ attr_reader :name, :port
7
+
8
+ def initialize(name, port = nil, secure = false)
9
+ @name, @port = name, port
10
+ @secure = secure
11
+ end
12
+
13
+ def secure?
14
+ @secure
15
+ end
16
+
17
+ def self.from_string(host_string)
18
+ host, port = host_string.split(':')
19
+ new(host, port.to_i)
20
+ end
21
+
22
+ def to_uri(uri_options = {})
23
+ [:host, :port].each { |opt| uri_options.delete(opt) }
24
+ URI::Generic.build({:host => name, :port => port, :scheme => default_uri_scheme}.merge(uri_options))
25
+ end
26
+
27
+ def url_for_path(path)
28
+ to_uri(:path => path).to_s
29
+ end
30
+
31
+ def to_s
32
+ [name, port].compact.join(':')
33
+ end
34
+
35
+ def default_uri_scheme
36
+ secure? ? 'https' : 'http'
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), *%w[.. lib])
2
+
3
+ require 'rails_compatibility'
4
+ require 'simple_config'
5
+ require 'simple_config/controller_mixin'
@@ -0,0 +1,21 @@
1
+ require 'fileutils'
2
+
3
+ namespace :simple_config do
4
+ include FileUtils
5
+
6
+ task :setup do
7
+ raise "This task should be run from within a Rails application." unless File.exist?('config')
8
+ raise "Already found config/settings. Have you already run this task?." if File.exist?('config/settings')
9
+
10
+ mkdir('config/settings')
11
+ mkdir("config/initializers") unless File.exist?("config/initializers")
12
+
13
+ environments = Dir["config/environments/*.rb"].map { |f| File.basename(f, ".rb") }
14
+ environments << 'application'
15
+ environments.each { |env| touch("config/settings/#{env}.rb") }
16
+
17
+ cp(File.join(File.dirname(__FILE__), *%w[.. templates configuration.rb]),
18
+ "config/initializers/configuration.rb")
19
+ end
20
+
21
+ end
@@ -0,0 +1,18 @@
1
+ SimpleConfig.for :application do
2
+
3
+ # Set here your global configuration.
4
+ # All settings can be overwritten later per-environment.
5
+ load File.join(RAILS_ROOT, 'config', "settings", "application.rb"), :if_exists? => true
6
+
7
+ # Per Environment settings.
8
+ # At startup only the file matching current environment will be loaded.
9
+ # Settings stored here will overwrite settings with the same name stored in application.rb
10
+ load File.join(RAILS_ROOT, 'config', "settings", "#{RAILS_ENV}.rb"), :if_exists? => true
11
+
12
+ # Local settings. It is designed as a place for you to override variables
13
+ # specific to your own development environment.
14
+ # Make sure your version control system ignores this file otherwise
15
+ # you risk checking in a file that will override values in production
16
+ load File.join(RAILS_ROOT, 'config', "settings", "local.rb"), :if_exists? => true
17
+
18
+ end
@@ -0,0 +1,38 @@
1
+ require File.join(File.dirname(__FILE__), *%w[test_helper])
2
+
3
+ require 'simple_config'
4
+ require 'simple_config/controller_mixin'
5
+
6
+ class RailsController
7
+ class << self
8
+ attr_reader :helper_methods
9
+ def helper_method(name)
10
+ (@helper_methods ||= []) << name
11
+ end
12
+ end
13
+ end
14
+
15
+ class ControllerMixinTest < Test::Unit::TestCase
16
+
17
+ def setup
18
+ @app_config = SimpleConfig.for(:application) do
19
+ end
20
+
21
+ @controller_klass = Class.new(RailsController)
22
+ end
23
+
24
+ def test_should_define_a_config_class_method_that_returns_the_application_config_when_included
25
+ @controller_klass.send(:include, SimpleConfig::ControllerMixin)
26
+ assert_equal @app_config, @controller_klass.config
27
+ end
28
+
29
+ def test_should_define_a_config_instance_method_that_returns_the_application_config_when_included
30
+ @controller_klass.send(:include, SimpleConfig::ControllerMixin)
31
+ assert_equal @app_config, @controller_klass.new.config
32
+ end
33
+
34
+ def test_should_define_the_config_method_as_helper_method_to_make_it_available_to_views_when_included
35
+ @controller_klass.send(:include, SimpleConfig::ControllerMixin)
36
+ assert_equal [:config], @controller_klass.helper_methods
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ require File.join(File.dirname(__FILE__), *%w[test_helper])
2
+
3
+ require 'simple_config/utilities'
4
+
5
+ class NetworkHostTest < Test::Unit::TestCase
6
+ include SimpleConfig::Utilities
7
+
8
+ def test_should_default_to_no_port
9
+ host = NetworkHost.new('www.example.com')
10
+ assert_nil host.port
11
+ end
12
+
13
+ def test_should_build_a_uri_object_with_the_specified_hostname_and_port_and_a_http_scheme
14
+ uri = NetworkHost.new('www.example.com', 9000).to_uri
15
+ assert_instance_of URI::Generic, uri
16
+ assert_equal 'http', uri.scheme
17
+ end
18
+
19
+ def test_should_build_a_uri_object_with_an_https_scheme_if_secure
20
+ uri = NetworkHost.new('www.example.com', 443, secure = true).to_uri
21
+ assert_equal 'https', uri.scheme
22
+ end
23
+
24
+ def test_should_return_a_url_for_a_given_path
25
+ host = NetworkHost.new('www.example.com')
26
+ assert_equal 'http://www.example.com/foo/bar', host.url_for_path('/foo/bar')
27
+ end
28
+
29
+ def test_should_return_a_string_representation
30
+ assert_equal 'www.example.com:9000', NetworkHost.new('www.example.com', 9000).to_s
31
+ end
32
+
33
+ def test_should_be_constructed_from_a_string_representation
34
+ host = NetworkHost.from_string('www.example.com:9000')
35
+ assert_equal 'www.example.com', host.name
36
+ assert_equal 9000, host.port
37
+ end
38
+ end
@@ -0,0 +1,100 @@
1
+ require File.join(File.dirname(__FILE__), *%w[test_helper])
2
+
3
+ require 'simple_config'
4
+
5
+ class SimpleConfigFunctionalTest < Test::Unit::TestCase
6
+
7
+ def test_simple_config_with_top_level_settings
8
+ config = SimpleConfig.for(:my_test) do
9
+ set :var_one, 'foo'
10
+ set :var_two, 'bar'
11
+ end
12
+
13
+ assert_equal 'foo', config.var_one
14
+ assert_equal 'bar', config.var_two
15
+ end
16
+
17
+ def test_config_with_groups
18
+ config = SimpleConfig.for(:my_test) do
19
+ group :test_group do
20
+ set :var_one, 'foo'
21
+ end
22
+ end
23
+
24
+ assert_equal 'foo', config.test_group.var_one
25
+ end
26
+
27
+ def test_config_with_top_level_settings_and_groups
28
+ config = SimpleConfig.for(:my_test) do
29
+ group :test_group do
30
+ set :var_one, 'foo'
31
+ end
32
+
33
+ set :var_two, 'bar'
34
+ end
35
+
36
+ assert_equal 'foo', config.test_group.var_one
37
+ assert_equal 'bar', config.var_two
38
+ end
39
+
40
+ def test_config_with_nested_groups
41
+ config = SimpleConfig.for(:my_test) do
42
+ group :test_group do
43
+ group :inner_group do
44
+ set :var_one, 'foo'
45
+ end
46
+ end
47
+ end
48
+
49
+ assert_equal 'foo', config.test_group.inner_group.var_one
50
+ end
51
+
52
+ def test_config_with_externally_loaded_ruby_config
53
+ sample_file = File.join(File.dirname(__FILE__), *%w[example.rb])
54
+ File.open(sample_file, "w") do |io|
55
+ io << %(
56
+ set :foo, 'bar'
57
+ )
58
+ end
59
+
60
+ config = SimpleConfig.for(:my_test) do
61
+ load sample_file
62
+ end
63
+
64
+ assert_equal 'bar', config.foo
65
+
66
+ FileUtils.rm_f(sample_file)
67
+ end
68
+
69
+ def test_config_with_externally_loaded_yaml_config
70
+ sample_file = File.join(File.dirname(__FILE__), *%w[example.yml])
71
+ File.open(sample_file, "w") do |io|
72
+ io << %(
73
+ example:
74
+ foo: bar
75
+ baz: qux
76
+
77
+ test: foo
78
+ )
79
+ end
80
+
81
+ config = SimpleConfig.for(:my_test) do
82
+ load sample_file
83
+ end
84
+
85
+ assert_equal 'foo', config.test
86
+ assert_equal 'bar', config.example.foo
87
+ assert_equal 'qux', config.example.baz
88
+
89
+ FileUtils.rm_f(sample_file)
90
+ end
91
+
92
+ def test_config_with_optional_external_config
93
+ assert_nothing_raised do
94
+ SimpleConfig.for(:my_test) do
95
+ load "non_existent_file", :if_exists? => true
96
+ end
97
+ end
98
+ end
99
+
100
+ end
@@ -0,0 +1,108 @@
1
+ require File.join(File.dirname(__FILE__), *%w[test_helper])
2
+
3
+ require 'simple_config'
4
+
5
+ class SimpleConfigConfigTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @config = SimpleConfig::Config.new
9
+ end
10
+
11
+ def test_should_be_able_to_set_config_values
12
+ @config.set(:var_one, 'hello world')
13
+ assert_equal 'hello world', @config.get(:var_one)
14
+ end
15
+
16
+ def test_should_be_able_to_access_settings_using_method_access
17
+ @config.set(:foo, 'bar')
18
+ assert_equal 'bar', @config.foo
19
+ end
20
+
21
+ def test_should_raise_NoMethodError_if_setting_does_not_exist_when_using_method_access
22
+ assert_raises(NoMethodError) { @config.some_non_existent_variable }
23
+ end
24
+
25
+ def test_should_return_nil_if_setting_does_not_exist_when_using_get
26
+ assert_nil @config.get(:some_non_existent_variable)
27
+ end
28
+
29
+ def test_unset_should_delete_config_values
30
+ @config.set(:foo, 'bar')
31
+ assert_equal('bar', @config.foo)
32
+ @config.unset(:foo)
33
+ assert_raises(NoMethodError) { @config.foo }
34
+ end
35
+
36
+ def test_unset_should_return_deleted_value
37
+ @config.set(:foo, 'bar')
38
+ assert_equal('bar', @config.unset(:foo))
39
+ end
40
+
41
+ def test_exists_should_return_whether_variable_isset
42
+ assert(!@config.exists?(:foo))
43
+ @config.set(:foo, 'bar')
44
+ assert(@config.exists?(:foo))
45
+ @config.unset(:foo)
46
+ assert(!@config.exists?(:foo))
47
+ end
48
+
49
+ def test_exists_should_consider_empty_values_as_set
50
+ [nil, 0, ''].each do |empty_value|
51
+ @config.set(:foo, empty_value)
52
+ assert_equal(empty_value, @config.get(:foo))
53
+ assert(@config.exists?(:foo))
54
+ end
55
+ end
56
+
57
+ def test_should_return_a_new_group_as_a_separate_config
58
+ group = @config.group(:test)
59
+ assert_instance_of(SimpleConfig::Config, group)
60
+ assert_not_equal @config, group
61
+ end
62
+
63
+ def test_should_return_an_existing_group
64
+ group = @config.group(:test)
65
+ assert_equal group, @config.group(:test)
66
+ end
67
+
68
+ def test_should_configure_group_with_supplied_block_when_given
69
+ group = @config.group(:test) do
70
+ set :group_var, 'value'
71
+ end
72
+ assert_equal 'value', group.group_var
73
+ end
74
+
75
+ def test_should_load_and_parse_external_config_as_ruby_in_context_of_config_instance
76
+ File.stubs(:read).with('external_config.rb').returns(ruby_code = stub('ruby'))
77
+ @config.expects(:instance_eval).with(ruby_code)
78
+ @config.load('external_config.rb')
79
+ end
80
+
81
+ def test_should_laod_and_parse_external_config_as_yaml_in_context_of_config_instance
82
+ parser = stub('YAMLParser')
83
+ SimpleConfig::YAMLParser.stubs(:parse_contents_of_file).with('external_config.yml').returns(parser)
84
+ parser.expects(:parse_into).with(@config)
85
+ @config.load('external_config.yml')
86
+ end
87
+
88
+ def test_should_load_and_parse_external_config_as_yaml_if_config_file_has_full_yaml_extension
89
+ parser = stub('YAMLParser')
90
+ SimpleConfig::YAMLParser.expects(:parse_contents_of_file).with('external_config.yaml').returns(parser)
91
+ parser.stubs(:parse_into)
92
+ @config.load('external_config.yaml')
93
+ end
94
+
95
+ def test_should_load_and_parse_external_config_if_file_exists_when_if_exists_is_true
96
+ File.stubs(:read).with('external_config.rb').returns(ruby_code = stub('ruby'))
97
+ @config.expects(:instance_eval).with(ruby_code)
98
+ File.stubs(:exist?).with('external_config.rb').returns(true)
99
+ @config.load('external_config.rb', :if_exists? => true)
100
+ end
101
+
102
+ def test_should_not_load_and_parse_external_config_if_file_does_not_exist_when_if_exists_is_true
103
+ @config.expects(:instance_eval).never
104
+ File.stubs(:exist?).with('external_config.rb').returns(false)
105
+ @config.load('external_config.rb', :if_exists? => true)
106
+ end
107
+
108
+ end
@@ -0,0 +1,58 @@
1
+ require File.join(File.dirname(__FILE__), *%w[test_helper])
2
+
3
+ require 'simple_config'
4
+
5
+ class YAMLParserTest < Test::Unit::TestCase
6
+ include SimpleConfig
7
+
8
+ def setup
9
+ @config = Config.new
10
+ end
11
+
12
+ def test_parsing_of_a_single_variable
13
+ parser = YAMLParser.new({'foo' => 'bar'}.to_yaml)
14
+ parser.parse_into(@config)
15
+
16
+ assert_equal 'bar', @config.foo
17
+ end
18
+
19
+ def test_parsing_of_multiple_variables
20
+ parser = YAMLParser.new({'foo' => 'bar', 'baz' => 'qux'}.to_yaml)
21
+ parser.parse_into(@config)
22
+
23
+ assert_equal 'bar', @config.foo
24
+ assert_equal 'qux', @config.baz
25
+ end
26
+
27
+ def test_parsing_of_a_group_with_one_variable
28
+ parser = YAMLParser.new({'group1' => {'foo' => 'bar'}}.to_yaml)
29
+ parser.parse_into(@config)
30
+
31
+ assert_equal 'bar', @config.group1.foo
32
+ end
33
+
34
+ def test_parsing_of_a_group_with_two_variables
35
+ parser = YAMLParser.new({'group1' => {'foo' => 'bar', 'baz' => 'qux'}}.to_yaml)
36
+ parser.parse_into(@config)
37
+
38
+ assert_equal 'bar', @config.group1.foo
39
+ assert_equal 'qux', @config.group1.baz
40
+ end
41
+
42
+ def test_parsing_of_a_nested_group
43
+ parser = YAMLParser.new({'group1' => {'group2' => {'foo' => 'bar'}}}.to_yaml)
44
+ parser.parse_into(@config)
45
+
46
+ assert_equal 'bar', @config.group1.group2.foo
47
+ end
48
+ end
49
+
50
+ class YAMLParserFromContentsOfFile < Test::Unit::TestCase
51
+ include SimpleConfig
52
+
53
+ def test_should_initialize_new_parser_from_contents_of_given_file
54
+ File.stubs(:read).with('config.yml').returns(yaml_data = stub('yaml data'))
55
+ YAMLParser.expects(:new).with(yaml_data).returns(parser = stub('YAMLParser'))
56
+ assert_equal parser, YAMLParser.parse_contents_of_file('config.yml')
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simpleconfig
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Luke Redpath
8
+ autorequire: simple_config
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-05 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Simple Config is a plugin designed to make application-wide configuration settings (e.g. in a Rails app) easy to set and access in an object-oriented fashion.
17
+ email: luke@lukeredpath.co.uk
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/simple_config.rb
26
+ - lib/simple_config/controller_mixin.rb
27
+ - lib/simple_config/utilities.rb
28
+ - lib/rails_compatibility.rb
29
+ - rails/init.rb
30
+ - Rakefile
31
+ - README.textile
32
+ - tasks/simple_config.rake
33
+ - templates/configuration.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/lukeredpath/simpleconfig
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.3.5
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Simple object-oriented application settings
62
+ test_files:
63
+ - test/controller_mixin_test.rb
64
+ - test/network_host_test.rb
65
+ - test/simple_config_functional_test.rb
66
+ - test/simple_config_test.rb
67
+ - test/yaml_parser_test.rb