simpleconfig 1.2.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # Changelog
2
2
 
3
- ## master
3
+ ## 2.0.0
4
+
5
+ - Remove deprecated SimpleConfig::Utilities.
6
+ - Remove deprecated SimpleConfig::ControllerMixin.
7
+ - Remove pre-Rails 3 init.rb file.
8
+ - Add Rails 3 support using Railtie.
9
+
10
+ ## 1.2.0
4
11
 
5
12
  - Updated Rakefile.
6
13
  - Added support for Bundler.
@@ -9,7 +16,7 @@
9
16
  - Deprecated SimpleConfig::Utilities.
10
17
  - Deprecated SimpleConfig::ControllerMixin.
11
18
 
12
- ## 1.1
19
+ ## 1.1.0
13
20
 
14
21
  - Added #to_hash method (via markschmidt)
15
22
 
@@ -1,4 +1,6 @@
1
- Copyright (c) 2010 Luke Redpath
1
+ The MIT License
2
+
3
+ Copyright (c) 2010-2012 Luke Redpath
2
4
 
3
5
  Permission is hereby granted, free of charge, to any person obtaining
4
6
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,32 +1,41 @@
1
1
  Simple Config is a plugin designed to make application-wide configuration settings easy to set and access in an
2
2
  object-oriented fashion.
3
3
 
4
- 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
+ This library was originally designed to be a Rails plugin, but it's now a standard Ruby library with no dependency on Rails. You can use it in any Ruby application or project.
5
5
 
6
- One simple solution is to simply put all of your app configuration into a YAML file and load this somewhere in your environment, but I 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.
7
6
 
8
- SimpleConfig was originally written against Rails 1.x and may still work but as of version 1.1.1 the minimum required Rails version is 2.3.5. You may be able to use it with older versions of Rails but YMMV.
7
+ ## Rails Configuration vs SimpleConfig
9
8
 
10
- WARNING: SimpleConfig 1.x is not fully compatible with Rails 3. It works, but the rake tasks are not automatically loaded. Please upgrade to SimpleConfig 2.0 if you are using Rails 3.
9
+ Rails already provides a way of configuring the framework on a per-environment or application basis, but the more the application becomes complex, the more the feature shows its limit.
10
+
11
+ One common solution is to put your app configuration into YAML files and load them somewhere in your environment, but when you have many developers and dynamic configurations this is not always the best choice.
12
+
13
+ Compared to the default Rails configuration system, SimpleConfig provides the following additional features:
14
+
15
+ - Ability to define per developer settings using the `local.rb` file
16
+ - Ability to nest configurations in groups
17
+ - Ability to clone configs
18
+ - Ability to load unlimited configuration scripts
11
19
 
12
20
 
13
21
  ## Getting started
14
22
 
15
23
  The plugin comes with a rake task to get you up and running quickly, so start by running that.
16
24
 
17
- $ rake simple_config:setup
25
+ $ rake simpleconfig:setup
18
26
 
19
- 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.
27
+ 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`.
20
28
 
21
- Now, if you open up the configuration.rb initializer, you will see something like this:
29
+ Now, if you open up the `configuration.rb` initializer, you will see something like this:
22
30
 
23
31
  ```ruby
24
32
  SimpleConfig.for :application do
25
33
 
26
34
  # your app configuration here
27
35
 
28
- load File.join(Rails.root, 'config', "settings", "#{RAILS_ENV}.rb"), :if_exists? => true
29
- load File.join(Rails.root, 'config', "settings", "local.rb"), :if_exists? => true
36
+ load File.join(Rails.root, "config", "settings", "application.rb"), :if_exists? => true
37
+ load File.join(Rails.root, "config", "settings", "#{RAILS_ENV}.rb"), :if_exists? => true
38
+ load File.join(Rails.root, "config", "settings", "local.rb"), :if_exists? => true
30
39
 
31
40
  end
32
41
  ```
@@ -37,6 +46,7 @@ Variables can be overwritten, and are defined in the order that they are loaded,
37
46
 
38
47
  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[1].
39
48
 
49
+
40
50
  ## Variables
41
51
 
42
52
  ### Setting Variables
@@ -117,38 +127,56 @@ config.awesome_stuff.my_variable # => "hello world"
117
127
 
118
128
  ## Using your configuration in your Rails app
119
129
 
120
- The plugin provides a convenient mixin for your `ApplicationController` to make configuration access as simple as possible. Assuming a configuration called "application" (as in the above examples), it defines a `config` method which can be used in any of your controllers. It also defines this as a method as a view helper using the Rails `helper_method` macro so you can access configuration data in your views.
130
+ When the application is initalized, by default the configurations are stored in the `:application` stack. You can access them anywhere using
121
131
 
122
- Note - there is no direct way of accessing your configuration variables in your models other than making a direct call to `SimpleConfig.for`. 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.
132
+ ```ruby
133
+ SimpleConfig.for(:application)
134
+ ```
123
135
 
124
- To use the mixin, simply include it in your `ApplicationController`:
136
+ It's a common habit to define a `config` method in your Rails application or Rails libraries to have quick access to the configuration object. You can also use a mixin.
125
137
 
126
138
  ```ruby
139
+ class Configurable
140
+ def config
141
+ SimpleConfig.for(:application)
142
+ end
143
+ end
144
+
127
145
  class ApplicationController < ActionController::Base
128
- include SimpleConfig::ControllerMixin
146
+ extend Configurable
147
+ include Configurable
148
+
149
+ def do_something
150
+ # here you can use config
151
+ if config.my_variable
152
+ render :foo
153
+ end
154
+ render :bar
155
+ else
156
+ end
157
+
129
158
  end
130
159
  ```
131
160
 
132
- Then in your controllers:
161
+ An other very common pattern is to assing your configuration object to a constant so that it becomes globally available in your Rails project.
133
162
 
134
163
  ```ruby
135
- class MyController < ApplicationController
136
- def index
137
- render :text => config.my_config_variable
138
- end
139
- end
164
+ # config/initializers/configuration.rb
165
+ # after the initialization block
166
+ CONFIG = SimpleConfig.for :app
140
167
  ```
141
168
 
142
- The mixin provides also a class-level `config` method to access the configuration when you don't have a controller instance available.
169
+ Then anywhere in your app
143
170
 
144
171
  ```ruby
145
- class MyController < ApplicationController
146
- protect_from_forgery :secret => config.secret_token
147
-
148
- def index
149
- render :text => config.my_config_variable
172
+ def do_something
173
+ if CONFIG.my_variable
174
+ render :foo
150
175
  end
176
+ render :bar
177
+ else
151
178
  end
152
179
  ```
153
180
 
154
- fn1(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.
181
+
182
+ fn1(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!
data/Rakefile CHANGED
@@ -2,6 +2,9 @@ require 'rubygems'
2
2
  require 'rubygems/package_task'
3
3
  require 'bundler'
4
4
 
5
+ $:.unshift(File.dirname(__FILE__) + "/lib")
6
+ require 'simple_config/version'
7
+
5
8
 
6
9
  # Run test by default.
7
10
  task :default => :test
@@ -14,12 +17,12 @@ task :default => :test
14
17
  #
15
18
  spec = Gem::Specification.new do |s|
16
19
  s.name = "simpleconfig"
17
- s.version = "1.2.0"
20
+ s.version = SimpleConfig::VERSION
18
21
  s.summary = "Simple object-oriented application settings for Ruby applications"
19
22
  s.description = "SimpleConfig 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."
20
23
 
21
- s.authors = ["Luke Redpath"]
22
- s.email = "luke@lukeredpath.co.uk"
24
+ s.authors = ["Luke Redpath", "Simone Carletti"]
25
+ s.email = ["luke@lukeredpath.co.uk", "weppos@weppos.net"]
23
26
  s.homepage = "http://github.com/lukeredpath/simpleconfig"
24
27
 
25
28
  s.files = `git ls-files`.split("\n")
data/lib/simple_config.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require 'simple_config/version'
2
+ require 'simple_config/railtie' if defined?(Rails)
3
+
1
4
  module SimpleConfig
2
5
 
3
6
  class << self
@@ -0,0 +1,29 @@
1
+ require 'simple_config'
2
+ require 'rails'
3
+
4
+ module Airbrake
5
+ class Railtie < ::Rails::Railtie
6
+
7
+ rake_tasks do
8
+ namespace :simpleconfig do
9
+ desc "Initialize SimpleConfig configurations."
10
+ task :setup do
11
+ abort("Already found config/settings. Have you already run this task?.") if File.exist?("config/settings")
12
+
13
+ mkdir("config/settings")
14
+ mkdir("config/initializers") unless File.exist?("config/initializers")
15
+
16
+ environments = Dir["config/environments/*.rb"].map { |f| File.basename(f, ".rb") }
17
+ environments << "application"
18
+ environments.each { |env| touch("config/settings/#{env}.rb") }
19
+
20
+ cp(
21
+ File.expand_path("../../../templates/configuration.rb", __FILE__),
22
+ "config/initializers/configuration.rb"
23
+ )
24
+ end
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,14 @@
1
+ module SimpleConfig
2
+
3
+ module Version
4
+ MAJOR = 2
5
+ MINOR = 0
6
+ PATCH = 0
7
+ BUILD = nil
8
+
9
+ STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join(".")
10
+ end
11
+
12
+ VERSION = Version::STRING
13
+
14
+ end
data/simpleconfig.gemspec CHANGED
@@ -2,19 +2,19 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "simpleconfig"
5
- s.version = "1.2.0"
5
+ s.version = "2.0.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
- s.authors = ["Luke Redpath"]
8
+ s.authors = ["Luke Redpath", "Simone Carletti"]
9
9
  s.date = "2012-06-27"
10
10
  s.description = "SimpleConfig 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."
11
- s.email = "luke@lukeredpath.co.uk"
12
- s.files = [".gitignore", "CHANGELOG.md", "Gemfile", "MIT-LICENSE", "README.md", "Rakefile", "init.rb", "lib/simple_config.rb", "lib/simple_config/controller_mixin.rb", "lib/simple_config/utilities.rb", "lib/simpleconfig.rb", "lib/tasks/simple_config.rake", "simpleconfig.gemspec", "templates/configuration.rb", "test/controller_mixin_test.rb", "test/network_host_test.rb", "test/simple_config_functional_test.rb", "test/simple_config_test.rb", "test/test_helper.rb", "test/yaml_parser_test.rb"]
11
+ s.email = ["luke@lukeredpath.co.uk", "weppos@weppos.net"]
12
+ s.files = [".gitignore", "CHANGELOG.md", "Gemfile", "LICENSE", "README.md", "Rakefile", "lib/simple_config.rb", "lib/simple_config/railtie.rb", "lib/simple_config/version.rb", "lib/simpleconfig.rb", "simpleconfig.gemspec", "templates/configuration.rb", "test/simple_config_functional_test.rb", "test/simple_config_test.rb", "test/test_helper.rb", "test/yaml_parser_test.rb"]
13
13
  s.homepage = "http://github.com/lukeredpath/simpleconfig"
14
14
  s.require_paths = ["lib"]
15
15
  s.rubygems_version = "1.8.24"
16
16
  s.summary = "Simple object-oriented application settings for Ruby applications"
17
- s.test_files = ["test/controller_mixin_test.rb", "test/network_host_test.rb", "test/simple_config_functional_test.rb", "test/simple_config_test.rb", "test/test_helper.rb", "test/yaml_parser_test.rb"]
17
+ s.test_files = ["test/simple_config_functional_test.rb", "test/simple_config_test.rb", "test/test_helper.rb", "test/yaml_parser_test.rb"]
18
18
 
19
19
  if s.respond_to? :specification_version then
20
20
  s.specification_version = 3
@@ -2,17 +2,17 @@ SimpleConfig.for :application do
2
2
 
3
3
  # Set here your global configuration.
4
4
  # All settings can be overwritten later per-environment.
5
- load File.join(Rails.root.to_s, 'config', "settings", "application.rb"), :if_exists? => true
5
+ load File.join(Rails.root.to_s, "config", "settings", "application.rb"), :if_exists? => true
6
6
 
7
7
  # Per Environment settings.
8
8
  # At startup only the file matching current environment will be loaded.
9
9
  # Settings stored here will overwrite settings with the same name stored in application.rb
10
- load File.join(Rails.root.to_s, 'config', "settings", "#{RAILS_ENV}.rb"), :if_exists? => true
10
+ load File.join(Rails.root.to_s, "config", "settings", "#{Rails.env}.rb"), :if_exists? => true
11
11
 
12
12
  # Local settings. It is designed as a place for you to override variables
13
13
  # specific to your own development environment.
14
14
  # Make sure your version control system ignores this file otherwise
15
15
  # you risk checking in a file that will override values in production
16
- load File.join(Rails.root.to_s, 'config', "settings", "local.rb"), :if_exists? => true
16
+ load File.join(Rails.root.to_s, "config", "settings", "local.rb"), :if_exists? => true
17
17
 
18
18
  end
@@ -1,3 +1,4 @@
1
+ require 'test_helper'
1
2
  require 'fileutils'
2
3
 
3
4
  class SimpleConfigFunctionalTest < Test::Unit::TestCase
metadata CHANGED
@@ -1,11 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simpleconfig
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 2.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Luke Redpath
9
+ - Simone Carletti
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
@@ -61,7 +62,9 @@ dependencies:
61
62
  version: '0'
62
63
  description: SimpleConfig is a plugin designed to make application-wide configuration
63
64
  settings (e.g. in a Rails app) easy to set and access in an object-oriented fashion.
64
- email: luke@lukeredpath.co.uk
65
+ email:
66
+ - luke@lukeredpath.co.uk
67
+ - weppos@weppos.net
65
68
  executables: []
66
69
  extensions: []
67
70
  extra_rdoc_files: []
@@ -69,19 +72,15 @@ files:
69
72
  - .gitignore
70
73
  - CHANGELOG.md
71
74
  - Gemfile
72
- - MIT-LICENSE
75
+ - LICENSE
73
76
  - README.md
74
77
  - Rakefile
75
- - init.rb
76
78
  - lib/simple_config.rb
77
- - lib/simple_config/controller_mixin.rb
78
- - lib/simple_config/utilities.rb
79
+ - lib/simple_config/railtie.rb
80
+ - lib/simple_config/version.rb
79
81
  - lib/simpleconfig.rb
80
- - lib/tasks/simple_config.rake
81
82
  - simpleconfig.gemspec
82
83
  - templates/configuration.rb
83
- - test/controller_mixin_test.rb
84
- - test/network_host_test.rb
85
84
  - test/simple_config_functional_test.rb
86
85
  - test/simple_config_test.rb
87
86
  - test/test_helper.rb
@@ -100,7 +99,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
99
  version: '0'
101
100
  segments:
102
101
  - 0
103
- hash: -580664195664011592
102
+ hash: 1619246843325472384
104
103
  required_rubygems_version: !ruby/object:Gem::Requirement
105
104
  none: false
106
105
  requirements:
@@ -114,8 +113,6 @@ signing_key:
114
113
  specification_version: 3
115
114
  summary: Simple object-oriented application settings for Ruby applications
116
115
  test_files:
117
- - test/controller_mixin_test.rb
118
- - test/network_host_test.rb
119
116
  - test/simple_config_functional_test.rb
120
117
  - test/simple_config_test.rb
121
118
  - test/test_helper.rb
data/init.rb DELETED
@@ -1,4 +0,0 @@
1
- $LOAD_PATH << File.join(File.dirname(__FILE__), 'lib')
2
-
3
- require 'simple_config'
4
- require 'simple_config/controller_mixin'
@@ -1,37 +0,0 @@
1
- warn <<-EOS
2
- DEPRECATION WARNING: SimpleConfig::ControllerMixin is deprecated and will be removed in SimpleConfig 2.0. If you really need this functionality, add the code to your app or consider creating a MicroGem.
3
- See http://jeffkreeftmeijer.com/2011/microgems-five-minute-rubygems/
4
- EOS
5
-
6
-
7
- module SimpleConfig
8
- module ControllerMixin
9
-
10
- def self.included(base)
11
- base.extend ClassMethods
12
- base.class_eval do
13
- include InstanceMethods
14
- helper_method :config
15
- end
16
- end
17
-
18
- module ClassMethods
19
-
20
- # Returns the application config.
21
- def config
22
- SimpleConfig.for(:application)
23
- end
24
-
25
- end
26
-
27
- module InstanceMethods
28
-
29
- # Instance-level proxy to class-level +config+ method.
30
- def config
31
- self.class.config
32
- end
33
-
34
- end
35
-
36
- end
37
- end
@@ -1,45 +0,0 @@
1
- require 'uri'
2
-
3
- warn <<-EOS
4
- DEPRECATION WARNING: SimpleConfig::Utilities is deprecated and will be removed in SimpleConfig 2.0. Please package it as a Gem or MicroGem.
5
- See http://jeffkreeftmeijer.com/2011/microgems-five-minute-rubygems/
6
- EOS
7
-
8
- module SimpleConfig
9
- module Utilities
10
- class NetworkHost
11
- attr_reader :name, :port
12
-
13
- def initialize(name, port = nil, secure = false)
14
- @name, @port = name, port
15
- @secure = secure
16
- end
17
-
18
- def secure?
19
- @secure
20
- end
21
-
22
- def self.from_string(host_string)
23
- host, port = host_string.split(':')
24
- new(host, port.to_i)
25
- end
26
-
27
- def to_uri(uri_options = {})
28
- [:host, :port].each { |opt| uri_options.delete(opt) }
29
- URI::Generic.build({:host => name, :port => port, :scheme => default_uri_scheme}.merge(uri_options))
30
- end
31
-
32
- def url_for_path(path)
33
- to_uri(:path => path).to_s
34
- end
35
-
36
- def to_s
37
- [name, port].compact.join(':')
38
- end
39
-
40
- def default_uri_scheme
41
- secure? ? 'https' : 'http'
42
- end
43
- end
44
- end
45
- end
@@ -1,21 +0,0 @@
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
@@ -1,36 +0,0 @@
1
- require 'test_helper'
2
- require 'simple_config/controller_mixin'
3
-
4
- class RailsController
5
- class << self
6
- attr_reader :helper_methods
7
- def helper_method(name)
8
- (@helper_methods ||= []) << name
9
- end
10
- end
11
- end
12
-
13
- class ControllerMixinTest < Test::Unit::TestCase
14
-
15
- def setup
16
- @app_config = SimpleConfig.for(:application) do
17
- end
18
-
19
- @controller_klass = Class.new(RailsController)
20
- end
21
-
22
- def test_should_define_a_config_class_method_that_returns_the_application_config_when_included
23
- @controller_klass.send(:include, SimpleConfig::ControllerMixin)
24
- assert_equal @app_config, @controller_klass.config
25
- end
26
-
27
- def test_should_define_a_config_instance_method_that_returns_the_application_config_when_included
28
- @controller_klass.send(:include, SimpleConfig::ControllerMixin)
29
- assert_equal @app_config, @controller_klass.new.config
30
- end
31
-
32
- def test_should_define_the_config_method_as_helper_method_to_make_it_available_to_views_when_included
33
- @controller_klass.send(:include, SimpleConfig::ControllerMixin)
34
- assert_equal [:config], @controller_klass.helper_methods
35
- end
36
- end
@@ -1,37 +0,0 @@
1
- require 'test_helper'
2
- require 'simple_config/utilities'
3
-
4
- class NetworkHostTest < Test::Unit::TestCase
5
- include SimpleConfig::Utilities
6
-
7
- def test_should_default_to_no_port
8
- host = NetworkHost.new('www.example.com')
9
- assert_nil host.port
10
- end
11
-
12
- def test_should_build_a_uri_object_with_the_specified_hostname_and_port_and_a_http_scheme
13
- uri = NetworkHost.new('www.example.com', 9000).to_uri
14
- assert_instance_of URI::Generic, uri
15
- assert_equal 'http', uri.scheme
16
- end
17
-
18
- def test_should_build_a_uri_object_with_an_https_scheme_if_secure
19
- uri = NetworkHost.new('www.example.com', 443, secure = true).to_uri
20
- assert_equal 'https', uri.scheme
21
- end
22
-
23
- def test_should_return_a_url_for_a_given_path
24
- host = NetworkHost.new('www.example.com')
25
- assert_equal 'http://www.example.com/foo/bar', host.url_for_path('/foo/bar')
26
- end
27
-
28
- def test_should_return_a_string_representation
29
- assert_equal 'www.example.com:9000', NetworkHost.new('www.example.com', 9000).to_s
30
- end
31
-
32
- def test_should_be_constructed_from_a_string_representation
33
- host = NetworkHost.from_string('www.example.com:9000')
34
- assert_equal 'www.example.com', host.name
35
- assert_equal 9000, host.port
36
- end
37
- end