ambience 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,9 @@
1
- script: "rake"
2
1
  rvm:
3
2
  - 1.8.7
4
3
  - 1.9.2
4
+ - ruby-head
5
5
  - ree
6
6
  - rbx
7
+ - rbx-2.0
7
8
  - jruby
8
9
 
@@ -1,3 +1,9 @@
1
+ == 2.0.0 (2011-11-04)
2
+
3
+ * Replaced the automatic loading of a user-local ambience.yml with an option to
4
+ load an app-specific config by providing a path to the additonal config via
5
+ an AMBIENCE_CONFIG environment variable.
6
+
1
7
  == 1.0.0 (2011-06-15)
2
8
 
3
9
  * Cleaned up the project's setup.
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  Ambience [![Build Status](http://travis-ci.org/rubiii/ambience.png)](http://travis-ci.org/rubiii/ambience)
2
2
  ========
3
3
 
4
- App configuration feat. YAML and JVM properties. Lets you specify a default configuration in a YAML file
5
- and overwrite details via local settings and JVM properties for production.
4
+ App configuration feat. YAML and JVM properties. Lets you specify a default configuration in a YAML
5
+ file and overwrite details via local settings and JVM properties for production.
6
6
 
7
7
 
8
8
  Installation
@@ -18,7 +18,7 @@ $ gem install ambience
18
18
  Getting started
19
19
  ---------------
20
20
 
21
- Given you created a YAML config like this:
21
+ Ambience expects your configuration to live inside a YAML file:
22
22
 
23
23
  ``` yml
24
24
  auth:
@@ -27,20 +27,21 @@ auth:
27
27
  password: test
28
28
  ```
29
29
 
30
- You can instantiate an Ambience config by passing in the path to your config file:
30
+ You can create a new Ambience config by passing in the path to your config file:
31
31
 
32
32
  ``` ruby
33
33
  AppConfig = Ambience.create Rails.root.join("config", "ambience.yml")
34
34
  ```
35
35
 
36
- Ambience will load and convert your config into a Hash:
36
+ Ambience loads your config and converts it into a Hash:
37
37
 
38
38
  ``` ruby
39
39
  { "auth" => { "address" => "http://example.com", "username" => "ferris", "password" => "test" } }
40
40
  ```
41
41
 
42
- Afterwards it tries to merge these settings with local ones specified in an Ambience.local_config file.
43
- Finally it looks for any JVM properties (if your running JRuby) and merge these properties with your config:
42
+ Afterwards it tries to merge these settings with app-specific setting stored in a file which path is
43
+ provided through the AMBIENCE_CONFIG environment variable. Also, if you're using JRuby, Ambience will
44
+ merge all JVM properties with the config Hash:
44
45
 
45
46
  ``` ruby
46
47
  auth.address = "http://live.example.com"
@@ -53,7 +54,7 @@ The result would be something like this:
53
54
  { "auth" => { "address" => "http://live.example.com", "username" => "ferris", "password" => "topsecret" } }
54
55
  ```
55
56
 
56
- In the end you can decide whether you want to return the config as a Hash:
57
+ You can get the final config as a Hash:
57
58
 
58
59
  ``` ruby
59
60
  AppConfig = Ambience.create(Rails.root.join("config", "ambience.yml")).to_hash
@@ -69,6 +70,6 @@ AppConfig = Ambience.create(Rails.root.join("config", "ambience.yml")).to_mash
69
70
  Railtie
70
71
  -------
71
72
 
72
- Ambience comes with a Railtie which looks for `config/ambience.yml` inside your Rails project.
73
- If the file exists, Ambience loads the config and stores it in an `AppConfig` constant.
73
+ Ambience comes with a Railtie which looks for `config/ambience.yml` inside your Rails project.
74
+ If the file exists, Ambience loads the config and stores it in an `AppConfig` constant.
74
75
  All this happens before Rails evaluates your environment config.
@@ -14,8 +14,9 @@ Gem::Specification.new do |s|
14
14
 
15
15
  s.add_dependency "hashie", ">= 0.2.0"
16
16
 
17
- s.add_development_dependency "rspec", "~> 2.6.0"
18
- s.add_development_dependency "mocha", "~> 0.9.12"
17
+ s.add_development_dependency "rake", "0.8.7"
18
+ s.add_development_dependency "rspec", "~> 2.6.0"
19
+ s.add_development_dependency "mocha", "~> 0.9.12"
19
20
  s.add_development_dependency "autotest"
20
21
 
21
22
  s.files = `git ls-files`.split("\n")
@@ -17,26 +17,28 @@ module Ambience
17
17
  # in a YAML file and overwrite details via local settings and JVM properties for production.
18
18
  class Config
19
19
 
20
- # Sets the path to a local ambience config file.
21
- def self.local_config=(local_config)
22
- @@local_config = local_config
23
- end
20
+ class << self
21
+
22
+ def env_config
23
+ @env_config ||= "AMBIENCE_CONFIG"
24
+ end
25
+
26
+ attr_writer :env_config
24
27
 
25
- # Returns the path to a local ambience config file for specifying user-specific
26
- # settings that don't belong into the application config file.
27
- def self.local_config
28
- @@local_config ||= File.join ENV["HOME"].to_s, ".ambience", "ambience.yml"
29
28
  end
30
29
 
31
- def initialize(config_file, env = nil)
32
- @config_file, @env = config_file, env
30
+ def initialize(base_config, env = nil)
31
+ self.base_config = base_config
32
+ self.env = env
33
33
  end
34
34
 
35
+ attr_accessor :base_config, :env
36
+
35
37
  # Returns the Ambience config as a Hash.
36
38
  def to_hash
37
- config = load_config @config_file
38
- config = config.deep_merge local_config
39
- config.deep_merge jvm_config
39
+ config = load_base_config
40
+ config = config.deep_merge(load_env_config)
41
+ config.deep_merge(load_jvm_config)
40
42
  end
41
43
 
42
44
  # Returns the Ambience config as a Hashie::Mash.
@@ -46,28 +48,30 @@ module Ambience
46
48
 
47
49
  private
48
50
 
51
+ def load_base_config
52
+ load_config base_config
53
+ end
54
+
55
+ def load_env_config
56
+ config = ENV[self.class.env_config]
57
+ config ? load_config(config) : {}
58
+ end
59
+
49
60
  def load_config(config_file)
50
- raise ArgumentError, "Missing config: #{config_file}" unless File.exist? config_file
61
+ raise ArgumentError, "Missing config file: #{config_file}" unless File.exist?(config_file)
51
62
 
52
63
  config = File.read config_file
53
64
  config = YAML.load ERB.new(config).result || {}
54
- config = config[@env.to_s] || config[@env.to_sym] if @env
65
+ config = config[env.to_s] || config[env.to_sym] if env
55
66
  config
56
67
  end
57
68
 
58
- # Returns a Hash containing any local settings from the +@@local_config+.
59
- def local_config
60
- File.exist?(self.class.local_config) ? load_config(self.class.local_config) : {}
61
- end
62
-
63
- ## Returns a Hash containing any JVM properties.
64
- def jvm_config
69
+ def load_jvm_config
65
70
  jvm_properties.inject({}) do |hash, (key, value)|
66
71
  hash.deep_merge hash_from_property(key, value)
67
72
  end
68
73
  end
69
74
 
70
- # Returns the JVM properties.
71
75
  def jvm_properties
72
76
  Ambience.jruby? ? java.lang.System.get_properties : {}
73
77
  end
@@ -1,5 +1,5 @@
1
1
  module Ambience
2
2
 
3
- VERSION = "1.0.0"
3
+ VERSION = "2.0.0"
4
4
 
5
5
  end
@@ -36,11 +36,12 @@ describe Ambience do
36
36
  end
37
37
 
38
38
  it "raises an ArgumentError when the given config file could not be found" do
39
- lambda { Ambience.create("missing_config.yml").to_hash }.should raise_error(ArgumentError)
39
+ expect { Ambience.create("missing_config.yml").to_hash }.to raise_error(ArgumentError)
40
40
  end
41
41
 
42
42
  context "when using JRuby" do
43
43
  before do
44
+ Ambience.stubs(:jruby?).returns(true)
44
45
  Ambience::Config.send(:define_method, :java) { JavaMock }
45
46
  end
46
47
 
@@ -49,11 +50,6 @@ describe Ambience do
49
50
  end
50
51
 
51
52
  it "merges the config with any JVM properties" do
52
- Ambience.stubs(:jruby?).returns(true)
53
- Ambience::Config.any_instance.stubs(:jvm_properties).returns(
54
- "auth.address" => "http://live.example.com",
55
- "auth.password" => "topsecret"
56
- )
57
53
  config = Ambience.create(config_file(:basic)).to_mash
58
54
 
59
55
  config.should be_a(Hashie::Mash)
@@ -63,6 +59,15 @@ describe Ambience do
63
59
  end
64
60
  end
65
61
 
62
+ context "with an env config file" do
63
+ it "merges the config with the env config" do
64
+ ENV["AMBIENCE_CONFIG"] = config_file(:env_config)
65
+ config = Ambience.create(config_file(:basic)).to_mash
66
+
67
+ config.auth.address.should == "http://live.example.com"
68
+ end
69
+ end
70
+
66
71
  def config_file(name)
67
72
  File.expand_path("../../fixtures/#{name}.yml", __FILE__)
68
73
  end
@@ -1,5 +1,5 @@
1
1
  auth:
2
- address: http://example.com
3
- username: ferris
4
- password: test
2
+ address: "http://example.com"
3
+ username: "ferris"
4
+ password: "test"
5
5
 
@@ -0,0 +1,4 @@
1
+ auth:
2
+ address: "http://live.example.com"
3
+ username: "ferris"
4
+ password: "test"
@@ -1,14 +1,13 @@
1
1
  development: &development
2
2
  auth:
3
3
  username: "ferris"
4
- password: test
4
+ password: "test"
5
5
 
6
6
  test:
7
7
  <<: *development
8
8
 
9
9
  production:
10
- <<: *development
11
10
  auth:
12
11
  username: "ferris"
13
- password: topsecret
12
+ password: "topsecret"
14
13
 
@@ -1,7 +1,7 @@
1
1
  :development: &development
2
2
  :auth:
3
3
  :username: "ferris"
4
- :password: test
4
+ :password: "test"
5
5
 
6
6
  :test:
7
7
  <<: *development
@@ -1,4 +1,8 @@
1
1
  require "bundler"
2
2
  Bundler.require :default, :development
3
3
 
4
+ RSpec.configure do |config|
5
+ config.mock_with :mocha
6
+ end
7
+
4
8
  require "support/java_mock"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ambience
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
- - 1
7
+ - 2
8
8
  - 0
9
9
  - 0
10
- version: 1.0.0
10
+ version: 2.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Daniel Harrington
@@ -15,13 +15,10 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-15 00:00:00 +02:00
19
- default_executable:
18
+ date: 2011-11-04 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
- name: hashie
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
21
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
22
  none: false
26
23
  requirements:
27
24
  - - ">="
@@ -32,12 +29,28 @@ dependencies:
32
29
  - 2
33
30
  - 0
34
31
  version: 0.2.0
32
+ name: hashie
35
33
  type: :runtime
36
- version_requirements: *id001
34
+ prerelease: false
35
+ requirement: *id001
37
36
  - !ruby/object:Gem::Dependency
38
- name: rspec
37
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - "="
41
+ - !ruby/object:Gem::Version
42
+ hash: 49
43
+ segments:
44
+ - 0
45
+ - 8
46
+ - 7
47
+ version: 0.8.7
48
+ name: rake
49
+ type: :development
39
50
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
51
+ requirement: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
41
54
  none: false
42
55
  requirements:
43
56
  - - ~>
@@ -48,12 +61,12 @@ dependencies:
48
61
  - 6
49
62
  - 0
50
63
  version: 2.6.0
64
+ name: rspec
51
65
  type: :development
52
- version_requirements: *id002
53
- - !ruby/object:Gem::Dependency
54
- name: mocha
55
66
  prerelease: false
56
- requirement: &id003 !ruby/object:Gem::Requirement
67
+ requirement: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ version_requirements: &id004 !ruby/object:Gem::Requirement
57
70
  none: false
58
71
  requirements:
59
72
  - - ~>
@@ -64,12 +77,12 @@ dependencies:
64
77
  - 9
65
78
  - 12
66
79
  version: 0.9.12
80
+ name: mocha
67
81
  type: :development
68
- version_requirements: *id003
69
- - !ruby/object:Gem::Dependency
70
- name: autotest
71
82
  prerelease: false
72
- requirement: &id004 !ruby/object:Gem::Requirement
83
+ requirement: *id004
84
+ - !ruby/object:Gem::Dependency
85
+ version_requirements: &id005 !ruby/object:Gem::Requirement
73
86
  none: false
74
87
  requirements:
75
88
  - - ">="
@@ -78,8 +91,10 @@ dependencies:
78
91
  segments:
79
92
  - 0
80
93
  version: "0"
94
+ name: autotest
81
95
  type: :development
82
- version_requirements: *id004
96
+ prerelease: false
97
+ requirement: *id005
83
98
  description: App configuration feat. YAML and JVM properties
84
99
  email: me@rubiii.com
85
100
  executables: []
@@ -105,11 +120,11 @@ files:
105
120
  - lib/ambience/version.rb
106
121
  - spec/ambience/ambience_spec.rb
107
122
  - spec/fixtures/basic.yml
123
+ - spec/fixtures/env_config.yml
108
124
  - spec/fixtures/environments.yml
109
125
  - spec/fixtures/symbols.yml
110
126
  - spec/spec_helper.rb
111
127
  - spec/support/java_mock.rb
112
- has_rdoc: true
113
128
  homepage: http://github.com/rubiii/ambience
114
129
  licenses: []
115
130
 
@@ -139,13 +154,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
154
  requirements: []
140
155
 
141
156
  rubyforge_project: ambience
142
- rubygems_version: 1.4.1
157
+ rubygems_version: 1.8.6
143
158
  signing_key:
144
159
  specification_version: 3
145
160
  summary: App configuration feat. YAML and JVM properties
146
161
  test_files:
147
162
  - spec/ambience/ambience_spec.rb
148
163
  - spec/fixtures/basic.yml
164
+ - spec/fixtures/env_config.yml
149
165
  - spec/fixtures/environments.yml
150
166
  - spec/fixtures/symbols.yml
151
167
  - spec/spec_helper.rb