app_constants 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +11 -0
- data/Gemfile.lock +25 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +97 -0
- data/Rakefile +62 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/install.rb +1 -0
- data/lib/app_constants.rb +40 -0
- data/lib/generators/USAGE +9 -0
- data/lib/generators/app_constants_generator.rb +10 -0
- data/lib/generators/templates/constants.yml +12 -0
- data/lib/generators/templates/load_app_constants.rb +4 -0
- data/test/app_constants_spec.rb +38 -0
- data/test/fixtures/constants.yml +6 -0
- data/test/fixtures/nested_constants.yml +9 -0
- data/test/fixtures/template_constants.yml +4 -0
- data/test/test_helper.rb +4 -0
- data/uninstall.rb +1 -0
- metadata +135 -0
data/Gemfile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rspec"
|
10
|
+
gem "jeweler"
|
11
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.5.2)
|
7
|
+
bundler (~> 1.0.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rake (0.8.7)
|
11
|
+
rspec (2.4.0)
|
12
|
+
rspec-core (~> 2.4.0)
|
13
|
+
rspec-expectations (~> 2.4.0)
|
14
|
+
rspec-mocks (~> 2.4.0)
|
15
|
+
rspec-core (2.4.0)
|
16
|
+
rspec-expectations (2.4.0)
|
17
|
+
diff-lcs (~> 1.1.2)
|
18
|
+
rspec-mocks (2.4.0)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
jeweler
|
25
|
+
rspec
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 [name of plugin creator]
|
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,97 @@
|
|
1
|
+
=== Note for Rails 2.3.x users
|
2
|
+
Check out the branch rails_2_3_x.
|
3
|
+
|
4
|
+
Master is Rails 3 compatible only.
|
5
|
+
|
6
|
+
= AppConstants
|
7
|
+
|
8
|
+
It's funny how every Rails application I work on ends up needing some sort of per-environment global constants.
|
9
|
+
|
10
|
+
Examples may include the application url - It might be used in account activation emails and thus should be different between the development and production environments.
|
11
|
+
|
12
|
+
Or perhaps your application depends on external services that, depending on the environment, are available in different URIs.
|
13
|
+
|
14
|
+
One common approach is to define these constants in your environment files:
|
15
|
+
|
16
|
+
# in environment.rb
|
17
|
+
Rails::Initializer.run do |config|
|
18
|
+
...
|
19
|
+
ACTIVATION_URL = "http://www.example.com/activate"
|
20
|
+
end
|
21
|
+
|
22
|
+
#and then in development.rb
|
23
|
+
ACTIVATION_URL = "http://localhost:3000/activate"
|
24
|
+
|
25
|
+
But in my experience constants end up being scattered all around, people don't know where to look for them and much less where new ones should be defined.
|
26
|
+
|
27
|
+
AppConstants can be used as a gem or as a plugin. It provides a clean way to handle this need.
|
28
|
+
|
29
|
+
|
30
|
+
== Installing AppConstants as a gem
|
31
|
+
|
32
|
+
Add it to your Gemfile:
|
33
|
+
|
34
|
+
gem 'app_constants'
|
35
|
+
|
36
|
+
Then bundle install and run the generator:
|
37
|
+
|
38
|
+
$ bundle install
|
39
|
+
|
40
|
+
$ rails generate app_constants
|
41
|
+
exists config
|
42
|
+
create config/constants.yml
|
43
|
+
exists config/initializers
|
44
|
+
create config/initializers/load_app_constants.rb
|
45
|
+
|
46
|
+
== Installing AppConstants as a plugin
|
47
|
+
|
48
|
+
Just install the plugin as you normally would and run the generator
|
49
|
+
|
50
|
+
$ rails plugin install git://github.com/leonardoborges/app_constants.git
|
51
|
+
|
52
|
+
$ rails generate app_constants
|
53
|
+
exists config
|
54
|
+
create config/constants.yml
|
55
|
+
exists config/initializers
|
56
|
+
create config/initializers/load_app_constants.rb
|
57
|
+
|
58
|
+
Now use the constants.yml file to define your own constants and the load_app_constants.rb initializer to customize the plugin. e.g: Change the default location for the constants.yml file.
|
59
|
+
|
60
|
+
== Example
|
61
|
+
|
62
|
+
If you have a constants.yml file that looks like this:
|
63
|
+
|
64
|
+
# Enter your per-environment constants below
|
65
|
+
|
66
|
+
development: &default
|
67
|
+
activation_url: "http://localhost:3000/activate"
|
68
|
+
max_upload_in_bytes: <%= 1.megabyte %>
|
69
|
+
|
70
|
+
production:
|
71
|
+
<<: *default
|
72
|
+
activation_url: "http://www.example.com/activate"
|
73
|
+
|
74
|
+
Then you can use the AppConstant class to retrieve your constants anywhere in your app:
|
75
|
+
|
76
|
+
# somewhere in the activation email view
|
77
|
+
Visit this url to activate your account:
|
78
|
+
<%= "#{AppConstants.activation_url}/#{activation_code}" %>
|
79
|
+
|
80
|
+
Notice how I used embedded ruby code in the second constant. It works just fine:
|
81
|
+
|
82
|
+
# somewhere in your upload handling code
|
83
|
+
max_size = AppConstants.max_upload_in_bytes # 1048576
|
84
|
+
|
85
|
+
== Contributing to app_constants
|
86
|
+
|
87
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
88
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
89
|
+
* Fork the project
|
90
|
+
* Start a feature/bugfix branch
|
91
|
+
* Commit and push until you are happy with your contribution
|
92
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
93
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
94
|
+
|
95
|
+
== Copyright
|
96
|
+
|
97
|
+
Copyright (c) 2010-2011 {Leonardo Borges}[http://www.leonardoborges.com], released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'rake'
|
12
|
+
require 'rake/rdoctask'
|
13
|
+
require 'rspec/core/rake_task'
|
14
|
+
require 'jeweler'
|
15
|
+
require 'jeweler/tasks'
|
16
|
+
require 'jeweler/specification'
|
17
|
+
require 'jeweler/rubygems_dot_org_tasks'
|
18
|
+
|
19
|
+
begin
|
20
|
+
Bundler.setup(:default, :development)
|
21
|
+
rescue Bundler::BundlerError => e
|
22
|
+
$stderr.puts e.message
|
23
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
24
|
+
exit e.status_code
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'Default: run unit tests.'
|
28
|
+
task :default => :spec
|
29
|
+
|
30
|
+
Jeweler::Tasks.new do |gem|
|
31
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
32
|
+
gem.name = "app_constants"
|
33
|
+
gem.homepage = "http://github.com/leonardoborges/app_constants"
|
34
|
+
gem.license = "MIT"
|
35
|
+
gem.summary = %Q{A clean and simple way to manage your application's per-environment constants}
|
36
|
+
gem.description = %Q{A clean and simple way to manage your application's per-environment constants}
|
37
|
+
gem.email = "leonardoborges.rj@gmail.com"
|
38
|
+
gem.authors = ["Leonardo Borges"]
|
39
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
40
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
41
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
42
|
+
gem.add_development_dependency 'rspec'
|
43
|
+
end
|
44
|
+
Jeweler::RubygemsDotOrgTasks.new
|
45
|
+
|
46
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
47
|
+
t.pattern = Dir.glob('test/**/*_spec.rb')
|
48
|
+
end
|
49
|
+
|
50
|
+
desc 'Generate documentation for the app_constants plugin.'
|
51
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
52
|
+
rdoc.rdoc_dir = 'rdoc'
|
53
|
+
rdoc.title = 'AppConstants'
|
54
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
55
|
+
rdoc.rdoc_files.include('README')
|
56
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'app_constants'
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'erb'
|
3
|
+
|
4
|
+
class AppConstants
|
5
|
+
@@config_path = Object.const_defined?(:Rails) ? "#{Rails.root}/config/constants.yml" : nil
|
6
|
+
@@environment = Object.const_defined?(:Rails) ? Rails.env : 'test'
|
7
|
+
|
8
|
+
def self.config_path=(path)
|
9
|
+
@@config_path = path
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.environment=(environment)
|
13
|
+
@@environment = environment
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.method_missing(method, *args)
|
17
|
+
@@instance.send(method).is_a?(Hash) ? AppConstants.new(@@instance.constants_hash[method.to_s]) : @@instance.send(method)
|
18
|
+
end
|
19
|
+
|
20
|
+
def method_missing(method, *args)
|
21
|
+
constants_hash[method.to_s].nil? ? "" : constants_hash[method.to_s]
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.load!
|
25
|
+
raise ArgumentError.new("No config file path specified. Use 'AppConstants.config_path = PATH' to set it up") if @@config_path.nil?
|
26
|
+
constants_config = YAML::load(pre_process_constants_file)
|
27
|
+
constants_hash = constants_config[@@environment] || {}
|
28
|
+
@@instance = AppConstants.new(constants_hash)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.pre_process_constants_file
|
32
|
+
template = File.open(@@config_path).read
|
33
|
+
ERB.new(template).result
|
34
|
+
end
|
35
|
+
|
36
|
+
attr_reader :constants_hash
|
37
|
+
def initialize(constants_hash)
|
38
|
+
@constants_hash = constants_hash
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
Description:
|
2
|
+
Creates the default constants.yml file the the load_app_constants.rb initializer
|
3
|
+
|
4
|
+
Example:
|
5
|
+
rails generate app_constants
|
6
|
+
|
7
|
+
This will create:
|
8
|
+
config/constants.yml - constants definition file
|
9
|
+
config/initializers/load_app_constants.yml - Initialization and customization file
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class AppConstantsGenerator < Rails::Generators::Base
|
2
|
+
def self.source_root
|
3
|
+
@source_root ||= File.expand_path('../templates', __FILE__)
|
4
|
+
end
|
5
|
+
|
6
|
+
def copy_config_files
|
7
|
+
copy_file('constants.yml', 'config/constants.yml')
|
8
|
+
copy_file('load_app_constants.rb', 'config/initializers/load_app_constants.rb')
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/app_constants')
|
2
|
+
|
3
|
+
describe "AppConstants" do
|
4
|
+
it "should access the constants in the config file" do
|
5
|
+
AppConstants.config_path = "#{File.dirname(__FILE__)}/fixtures/constants.yml"
|
6
|
+
AppConstants.environment = "development"
|
7
|
+
AppConstants.load!
|
8
|
+
|
9
|
+
AppConstants.public_url.should == "development.myawesomeapp.com"
|
10
|
+
AppConstants.app_name.should == "Master of awesomeness"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should return nil for non-existing environments" do
|
14
|
+
AppConstants.config_path = "#{File.dirname(__FILE__)}/fixtures/constants.yml"
|
15
|
+
AppConstants.environment = "invalid"
|
16
|
+
AppConstants.load!
|
17
|
+
AppConstants.public_url.should be_empty
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should process embedded code" do
|
22
|
+
AppConstants.config_path = "#{File.dirname(__FILE__)}/fixtures/template_constants.yml"
|
23
|
+
AppConstants.environment = "development"
|
24
|
+
AppConstants.load!
|
25
|
+
|
26
|
+
AppConstants.max_upload_in_bytes.should == 1048576
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#load!" do
|
30
|
+
|
31
|
+
before(:each) do
|
32
|
+
AppConstants.config_path = nil
|
33
|
+
end
|
34
|
+
it "should raise an ArgumentError if path is null" do
|
35
|
+
expect { AppConstants.load! }.to raise_error(ArgumentError)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/test/test_helper.rb
ADDED
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: app_constants
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Leonardo Borges
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-02-02 00:00:00 +11:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :development
|
31
|
+
prerelease: false
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rspec
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
segments:
|
41
|
+
- 0
|
42
|
+
version: "0"
|
43
|
+
type: :development
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: *id002
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: jeweler
|
48
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *id003
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: rspec
|
61
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
type: :development
|
70
|
+
prerelease: false
|
71
|
+
version_requirements: *id004
|
72
|
+
description: A clean and simple way to manage your application's per-environment constants
|
73
|
+
email: leonardoborges.rj@gmail.com
|
74
|
+
executables: []
|
75
|
+
|
76
|
+
extensions: []
|
77
|
+
|
78
|
+
extra_rdoc_files:
|
79
|
+
- README.rdoc
|
80
|
+
files:
|
81
|
+
- Gemfile
|
82
|
+
- Gemfile.lock
|
83
|
+
- MIT-LICENSE
|
84
|
+
- README.rdoc
|
85
|
+
- Rakefile
|
86
|
+
- VERSION
|
87
|
+
- init.rb
|
88
|
+
- install.rb
|
89
|
+
- lib/app_constants.rb
|
90
|
+
- lib/generators/USAGE
|
91
|
+
- lib/generators/app_constants_generator.rb
|
92
|
+
- lib/generators/templates/constants.yml
|
93
|
+
- lib/generators/templates/load_app_constants.rb
|
94
|
+
- test/app_constants_spec.rb
|
95
|
+
- test/fixtures/constants.yml
|
96
|
+
- test/fixtures/nested_constants.yml
|
97
|
+
- test/fixtures/template_constants.yml
|
98
|
+
- test/test_helper.rb
|
99
|
+
- uninstall.rb
|
100
|
+
has_rdoc: true
|
101
|
+
homepage: http://github.com/leonardoborges/app_constants
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
hash: -601260458832168315
|
115
|
+
segments:
|
116
|
+
- 0
|
117
|
+
version: "0"
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
segments:
|
124
|
+
- 0
|
125
|
+
version: "0"
|
126
|
+
requirements: []
|
127
|
+
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 1.3.7
|
130
|
+
signing_key:
|
131
|
+
specification_version: 3
|
132
|
+
summary: A clean and simple way to manage your application's per-environment constants
|
133
|
+
test_files:
|
134
|
+
- test/app_constants_spec.rb
|
135
|
+
- test/test_helper.rb
|