app_constants 1.0.2 → 1.0.3

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/Gemfile.lock CHANGED
@@ -8,14 +8,14 @@ GEM
8
8
  git (>= 1.2.5)
9
9
  rake
10
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)
11
+ rspec (2.5.0)
12
+ rspec-core (~> 2.5.0)
13
+ rspec-expectations (~> 2.5.0)
14
+ rspec-mocks (~> 2.5.0)
15
+ rspec-core (2.5.1)
16
+ rspec-expectations (2.5.0)
17
17
  diff-lcs (~> 1.1.2)
18
- rspec-mocks (2.4.0)
18
+ rspec-mocks (2.5.0)
19
19
 
20
20
  PLATFORMS
21
21
  ruby
data/README.rdoc CHANGED
@@ -67,6 +67,10 @@ After installing the gem - either using bundler or otherwise - you'll be respons
67
67
  AppConstants.environment = "development"
68
68
  AppConstants.load!
69
69
 
70
+ Optionally you can set raise_error_on_missing which will raise a runtime error 1) during load if the environment doesn't exist and 2) all other times if a constant doesn't exist:
71
+
72
+ AppConstants.raise_error_on_missing = true
73
+
70
74
  == Example
71
75
 
72
76
  If you have a constants.yml file that looks like this:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.0.3
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{app_constants}
8
- s.version = "1.0.2"
8
+ s.version = "1.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Leonardo Borges"]
12
- s.date = %q{2011-02-02}
12
+ s.date = %q{2011-04-10}
13
13
  s.description = %q{A clean and simple way to manage your application's per-environment constants}
14
14
  s.email = %q{leonardoborges.rj@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -41,7 +41,7 @@ Gem::Specification.new do |s|
41
41
  s.homepage = %q{http://github.com/leonardoborges/app_constants}
42
42
  s.licenses = ["MIT"]
43
43
  s.require_paths = ["lib"]
44
- s.rubygems_version = %q{1.3.7}
44
+ s.rubygems_version = %q{1.5.0}
45
45
  s.summary = %q{A clean and simple way to manage your application's per-environment constants}
46
46
  s.test_files = [
47
47
  "test/app_constants_spec.rb",
@@ -49,7 +49,6 @@ Gem::Specification.new do |s|
49
49
  ]
50
50
 
51
51
  if s.respond_to? :specification_version then
52
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
53
52
  s.specification_version = 3
54
53
 
55
54
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
data/lib/app_constants.rb CHANGED
@@ -4,6 +4,7 @@ require 'erb'
4
4
  class AppConstants
5
5
  @@config_path = Object.const_defined?(:Rails) ? "#{Rails.root}/config/constants.yml" : nil
6
6
  @@environment = Object.const_defined?(:Rails) ? Rails.env : 'test'
7
+ @@raise_error_on_missing = false # if true this will raise an error if method you seek isn't in constants_hash
7
8
 
8
9
  def self.config_path=(path)
9
10
  @@config_path = path
@@ -13,20 +14,40 @@ class AppConstants
13
14
  @@environment = environment
14
15
  end
15
16
 
17
+ def self.raise_error_on_missing=(true_false)
18
+ @@raise_error_on_missing = true_false
19
+ end
20
+
16
21
  def self.method_missing(method, *args)
17
22
  @@instance.send(method).is_a?(Hash) ? AppConstants.new(@@instance.constants_hash[method.to_s]) : @@instance.send(method)
18
23
  end
19
24
 
20
25
  def method_missing(method, *args)
21
- constants_hash[method.to_s].nil? ? "" : constants_hash[method.to_s].freeze
26
+ fail_if_constant_missing(method.to_s)
27
+ constants_hash[method.to_s].nil? ? "" : constants_hash[method.to_s].freeze
22
28
  end
23
-
29
+
30
+ def fail_if_constant_missing(constant)
31
+ unless constants_hash.keys.include?(constant) || @@raise_error_on_missing == false
32
+ raise "Constant #{constant} undefined in yml file. Options are: #{constants_hash.keys.join(", ")}"
33
+ end
34
+ end
35
+ private :fail_if_constant_missing
36
+
24
37
  def self.load!
25
38
  raise ArgumentError.new("No config file path specified. Use 'AppConstants.config_path = PATH' to set it up") if @@config_path.nil?
26
39
  constants_config = YAML::load(pre_process_constants_file)
40
+ fail_if_environment_missing(@@environment, constants_config)
27
41
  constants_hash = constants_config[@@environment] || {}
28
42
  @@instance = AppConstants.new(constants_hash)
29
- end
43
+ end
44
+
45
+ def self.fail_if_environment_missing(env, config)
46
+ unless config.keys.include?(env) || @@raise_error_on_missing == false
47
+ raise "Environment #{env} not found in yml file. Options are: #{config.keys.join(", ")}"
48
+ end
49
+ end
50
+ private_class_method :fail_if_environment_missing
30
51
 
31
52
  def self.pre_process_constants_file
32
53
  template = File.open(@@config_path).read
@@ -10,7 +10,7 @@ describe "AppConstants" do
10
10
  AppConstants.app_name.should == "Master of awesomeness"
11
11
  end
12
12
 
13
- it "should raise a RuntiemError if trying to modify constants" do
13
+ it "should raise a RuntimeError if trying to modify constants" do
14
14
  AppConstants.config_path = "#{File.dirname(__FILE__)}/fixtures/constants.yml"
15
15
  AppConstants.environment = "development"
16
16
  AppConstants.load!
@@ -36,6 +36,25 @@ describe "AppConstants" do
36
36
  AppConstants.max_upload_in_bytes.should == 1048576
37
37
  end
38
38
 
39
+ it "should raise a runtime error if raise_error_on_missing is true" do
40
+ AppConstants.config_path = "#{File.dirname(__FILE__)}/fixtures/constants.yml"
41
+ AppConstants.raise_error_on_missing = true
42
+ AppConstants.environment = "development"
43
+ AppConstants.load!
44
+ expect { AppConstants.private_url }.to raise_error(RuntimeError)
45
+
46
+ AppConstants.public_url.should == "development.myawesomeapp.com"
47
+ AppConstants.app_name == "Master of awesomeness"
48
+ AppConstants.environment = "production"
49
+ AppConstants.load!
50
+ expect { AppConstants.private_url }.to raise_error(RuntimeError)
51
+
52
+ AppConstants.public_url.should == "www.myawesomeapp.com"
53
+ AppConstants.app_name == "Master of awesomeness"
54
+ AppConstants.environment = "playpen"
55
+ expect { AppConstants.load! }.to raise_error(RuntimeError)
56
+ end
57
+
39
58
  describe "#load!" do
40
59
 
41
60
  before(:each) do
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: app_constants
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 0
8
- - 2
9
- version: 1.0.2
4
+ prerelease:
5
+ version: 1.0.3
10
6
  platform: ruby
11
7
  authors:
12
8
  - Leonardo Borges
@@ -14,7 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2011-02-02 00:00:00 +11:00
13
+ date: 2011-04-10 00:00:00 +10:00
18
14
  default_executable:
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
@@ -24,8 +20,6 @@ dependencies:
24
20
  requirements:
25
21
  - - ">="
26
22
  - !ruby/object:Gem::Version
27
- segments:
28
- - 0
29
23
  version: "0"
30
24
  type: :development
31
25
  prerelease: false
@@ -37,8 +31,6 @@ dependencies:
37
31
  requirements:
38
32
  - - ">="
39
33
  - !ruby/object:Gem::Version
40
- segments:
41
- - 0
42
34
  version: "0"
43
35
  type: :development
44
36
  prerelease: false
@@ -50,8 +42,6 @@ dependencies:
50
42
  requirements:
51
43
  - - ">="
52
44
  - !ruby/object:Gem::Version
53
- segments:
54
- - 0
55
45
  version: "0"
56
46
  type: :development
57
47
  prerelease: false
@@ -63,8 +53,6 @@ dependencies:
63
53
  requirements:
64
54
  - - ">="
65
55
  - !ruby/object:Gem::Version
66
- segments:
67
- - 0
68
56
  version: "0"
69
57
  type: :development
70
58
  prerelease: false
@@ -113,7 +101,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
113
101
  requirements:
114
102
  - - ">="
115
103
  - !ruby/object:Gem::Version
116
- hash: -781544183204636773
104
+ hash: -1519006393797098489
117
105
  segments:
118
106
  - 0
119
107
  version: "0"
@@ -122,13 +110,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
110
  requirements:
123
111
  - - ">="
124
112
  - !ruby/object:Gem::Version
125
- segments:
126
- - 0
127
113
  version: "0"
128
114
  requirements: []
129
115
 
130
116
  rubyforge_project:
131
- rubygems_version: 1.3.7
117
+ rubygems_version: 1.5.0
132
118
  signing_key:
133
119
  specification_version: 3
134
120
  summary: A clean and simple way to manage your application's per-environment constants