ambience 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +27 -0
- data/README.rdoc +9 -13
- data/Rakefile +0 -0
- data/lib/ambience/ambience.rb +65 -81
- data/lib/ambience/core_ext.rb +0 -0
- data/lib/ambience.rb +0 -0
- data/spec/ambience/ambience_spec.rb +17 -9
- data/spec/fixtures/basic.yml +0 -0
- data/spec/fixtures/environments.yml +0 -0
- data/spec/fixtures/symbols.yml +0 -0
- data/spec/spec_helper.rb +0 -0
- metadata +36 -17
data/CHANGELOG
CHANGED
@@ -1,3 +1,30 @@
|
|
1
|
+
== 0.3.0 (2010-05-13)
|
2
|
+
* Ambience now operates on the instance level. So after setting up a new Ambience config
|
3
|
+
like before:
|
4
|
+
|
5
|
+
AppConfig = Ambience.new File.join(Rails.root, "config", "ambience.yml")
|
6
|
+
|
7
|
+
you now have to choose whether you like to have the config returned as a simple Hash
|
8
|
+
or a Hashie::Mash (which was the default since version 0.2.0):
|
9
|
+
|
10
|
+
AppConfig.to_hash
|
11
|
+
AppConfig.to_mash
|
12
|
+
|
13
|
+
* Along with support for a basic config and JVM properties, version 0.3.0 adds support
|
14
|
+
for local (user-specific) settings. By default, Ambience tries to load a local YAML
|
15
|
+
config from:
|
16
|
+
|
17
|
+
File.join ENV["HOME"].to_s, ".ambience", "ambience.yml"
|
18
|
+
|
19
|
+
Ambience will still work fine if the file does not exist. But if you want to use a
|
20
|
+
local config and change the default location, you can use the +local_config+ class
|
21
|
+
method to do so.
|
22
|
+
|
23
|
+
Ambience.local_config # => "/Users/you/.ambience/ambience.yml"
|
24
|
+
|
25
|
+
# Change the location of the local config file:
|
26
|
+
Ambience.local_config = File.join "config", "user_config.yml"
|
27
|
+
|
1
28
|
== 0.2.0 (2010-03-06)
|
2
29
|
* Complete rewrite. Removed Rails-specific defaults and returning the Ambience config
|
3
30
|
as a Hashie::Mash (http://github.com/intridea/hashie). Take a look at the new Readme
|
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
= Ambience
|
2
2
|
|
3
|
-
App configuration feat. YAML and JVM properties. Lets you specify a default configuration in a YAML file and overwrite details via JVM properties for production.
|
3
|
+
App configuration feat. YAML and JVM properties. Lets you specify a default configuration in a YAML file and overwrite details via local settings and JVM properties for production.
|
4
4
|
|
5
5
|
== Installation
|
6
6
|
|
@@ -8,14 +8,14 @@ App configuration feat. YAML and JVM properties. Lets you specify a default conf
|
|
8
8
|
|
9
9
|
== How it works
|
10
10
|
|
11
|
-
Given you created a YAML config like this
|
11
|
+
Given you created a YAML config like this:
|
12
12
|
|
13
13
|
auth:
|
14
14
|
address: http://example.com
|
15
15
|
username: ferris
|
16
16
|
password: test
|
17
17
|
|
18
|
-
You
|
18
|
+
You can instantiate an Ambience config by passing in the path to your config file:
|
19
19
|
|
20
20
|
AppConfig = Ambience.new File.join(Rails.root, "config", "ambience.yml")
|
21
21
|
|
@@ -23,23 +23,19 @@ Ambience will load and convert your config into a Hash:
|
|
23
23
|
|
24
24
|
{ "auth" => { "address" => "http://example.com", "username" => "ferris", "password" => "test" } }
|
25
25
|
|
26
|
-
|
26
|
+
Afterwards it tries to merge these settings with local ones specified in an Ambience.local_config file. Finally it looks for any JVM properties (if your running JRuby) and merge these properties with your config:
|
27
27
|
|
28
28
|
auth.address = "http://live.example.com"
|
29
29
|
auth.password = "topsecret"
|
30
30
|
|
31
|
-
|
31
|
+
The result would be something like this:
|
32
32
|
|
33
33
|
{ "auth" => { "address" => "http://live.example.com", "username" => "ferris", "password" => "topsecret" } }
|
34
34
|
|
35
|
-
|
35
|
+
In the end you can decide whether you want to return the config as a Hash:
|
36
36
|
|
37
|
-
AppConfig
|
38
|
-
# => "http://live.example.com"
|
37
|
+
AppConfig = Ambience.new(File.join(Rails.root, "config", "ambience.yml")).to_hash
|
39
38
|
|
40
|
-
|
41
|
-
# => "http://live.example.com"
|
42
|
-
|
43
|
-
AppConfig.auth.password
|
44
|
-
# => "topsecret"
|
39
|
+
or a {Hashie::Mash}[http://github.com/intridea/hashie]:
|
45
40
|
|
41
|
+
AppConfig = Ambience.new(File.join(Rails.root, "config", "ambience.yml")).to_mash
|
data/Rakefile
CHANGED
File without changes
|
data/lib/ambience/ambience.rb
CHANGED
@@ -1,98 +1,82 @@
|
|
1
1
|
# = Ambience
|
2
2
|
#
|
3
3
|
# App configuration feat. YAML and JVM properties. Lets you specify a default configuration
|
4
|
-
# in a YAML file and overwrite details via JVM properties for production.
|
5
|
-
#
|
6
|
-
# == How it works
|
7
|
-
#
|
8
|
-
# Given you created a YAML config like this one:
|
9
|
-
#
|
10
|
-
# auth:
|
11
|
-
# address: http://example.com
|
12
|
-
# username: ferris
|
13
|
-
# password: test
|
14
|
-
#
|
15
|
-
# You create an Ambience config by passing in the path to your config file:
|
16
|
-
#
|
17
|
-
# AppConfig = Ambience.new File.join(Rails.root, "config", "ambience.yml")
|
18
|
-
#
|
19
|
-
# Ambience will load and convert your config into a Hash:
|
20
|
-
#
|
21
|
-
# { "auth" => { "address" => "http://example.com", "username" => "ferris", "password" => "test" } }
|
22
|
-
#
|
23
|
-
# Then it looks for any JVM properties (if your running JRuby)
|
24
|
-
#
|
25
|
-
# auth.address = "http://live.example.com"
|
26
|
-
# auth.password = "topsecret"
|
27
|
-
#
|
28
|
-
# and deep merge them with your Hash:
|
29
|
-
#
|
30
|
-
# { "auth" => { "address" => "http://live.example.com", "username" => "ferris", "password" => "topsecret" } }
|
31
|
-
#
|
32
|
-
# Finally, it returns the Hash as a {Hashie::Mash}[http://github.com/intridea/hashie] so you can
|
33
|
-
# access values by specifying keys as Symbols or Strings or using method-like accessors:
|
34
|
-
#
|
35
|
-
# AppConfig[:auth][:address]
|
36
|
-
# # => "http://live.example.com"
|
37
|
-
#
|
38
|
-
# AppConfig["auth"]["username"]
|
39
|
-
# # => "http://live.example.com"
|
40
|
-
#
|
41
|
-
# AppConfig.auth.password
|
42
|
-
# # => "topsecret"
|
4
|
+
# in a YAML file and overwrite details via local settings and JVM properties for production.
|
43
5
|
class Ambience
|
44
|
-
class << self
|
45
6
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
config_hash = config_hash[env.to_s] || config_hash[env.to_sym] if env
|
51
|
-
config_hash.deep_merge! jvm_property_hash
|
52
|
-
Hashie::Mash.new config_hash
|
53
|
-
end
|
7
|
+
# Sets the path to a local ambience config file.
|
8
|
+
def self.local_config=(local_config)
|
9
|
+
@@local_config = local_config
|
10
|
+
end
|
54
11
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
12
|
+
# Returns the path to a local ambience config file for specifying user-specific
|
13
|
+
# settings that don't belong into the application config file.
|
14
|
+
def self.local_config
|
15
|
+
@@local_config ||= File.join ENV["HOME"].to_s, ".ambience", "ambience.yml"
|
16
|
+
end
|
59
17
|
|
60
|
-
|
18
|
+
# Returns whether the current Ruby platfrom is JRuby.
|
19
|
+
def self.jruby?
|
20
|
+
RUBY_PLATFORM =~ /java/
|
21
|
+
end
|
61
22
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
File.read config_file
|
66
|
-
end
|
23
|
+
def initialize(config_file, env = nil)
|
24
|
+
@config_file, @env = config_file, env
|
25
|
+
end
|
67
26
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
27
|
+
# Returns the Ambience config as a Hash.
|
28
|
+
def to_hash
|
29
|
+
config = load_config @config_file
|
30
|
+
config = config.deep_merge local_config
|
31
|
+
config.deep_merge jvm_config
|
32
|
+
end
|
73
33
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
end
|
79
|
-
end
|
34
|
+
# Returns the Ambience config as a Hashie::Mash.
|
35
|
+
def to_mash
|
36
|
+
Hashie::Mash.new to_hash
|
37
|
+
end
|
80
38
|
|
81
|
-
|
82
|
-
#
|
83
|
-
# ==== Example:
|
84
|
-
#
|
85
|
-
# hash_from_property "webservice.auth.address", "http://auth.example.com"
|
86
|
-
# # => { "webservice" => { "auth" => { "address" => "http://auth.example.com" } } }
|
87
|
-
def hash_from_property(property, value)
|
88
|
-
property.split(".").reverse.inject(value) { |value, item| { item => value } }
|
89
|
-
end
|
39
|
+
private
|
90
40
|
|
91
|
-
|
92
|
-
|
93
|
-
|
41
|
+
def load_config(config_file)
|
42
|
+
raise ArgumentError, "Missing config: #{config_file}" unless File.exist? config_file
|
43
|
+
|
44
|
+
config = File.read config_file
|
45
|
+
config = YAML.load ERB.new(config).result || {}
|
46
|
+
config = config[@env.to_s] || config[@env.to_sym] if @env
|
47
|
+
config
|
48
|
+
end
|
49
|
+
|
50
|
+
# Returns a Hash containing any local settings from the +@@local_config+.
|
51
|
+
def local_config
|
52
|
+
File.exist?(self.class.local_config) ? load_config(self.class.local_config) : {}
|
53
|
+
end
|
54
|
+
|
55
|
+
## Returns a Hash containing any JVM properties.
|
56
|
+
def jvm_config
|
57
|
+
jvm_properties.inject({}) do |hash, (key, value)|
|
58
|
+
hash.deep_merge hash_from_property(key, value)
|
94
59
|
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Returns the JVM properties.
|
63
|
+
def jvm_properties
|
64
|
+
JavaLang::System.get_properties rescue {}
|
65
|
+
end
|
66
|
+
|
67
|
+
# Returns a Hash generated from a JVM +property+ and its +value+.
|
68
|
+
#
|
69
|
+
# ==== Example:
|
70
|
+
#
|
71
|
+
# hash_from_property "webservice.auth.address", "http://auth.example.com"
|
72
|
+
# # => { "webservice" => { "auth" => { "address" => "http://auth.example.com" } } }
|
73
|
+
def hash_from_property(property, value)
|
74
|
+
property.split(".").reverse.inject(value) { |value, item| { item => value } }
|
75
|
+
end
|
95
76
|
|
77
|
+
# Returns the JVM properties.
|
78
|
+
def jvm_properties
|
79
|
+
self.class.jruby? ? JavaLang::System.get_properties : {}
|
96
80
|
end
|
97
81
|
|
98
82
|
if jruby?
|
data/lib/ambience/core_ext.rb
CHANGED
File without changes
|
data/lib/ambience.rb
CHANGED
File without changes
|
@@ -3,9 +3,17 @@ require "spec_helper"
|
|
3
3
|
describe Ambience do
|
4
4
|
include SpecHelper
|
5
5
|
|
6
|
-
it "should return the content of a given config file as a
|
7
|
-
config = Ambience.new
|
6
|
+
it "should return the content of a given config file as a Hash" do
|
7
|
+
config = Ambience.new(config_file(:basic)).to_hash
|
8
|
+
|
9
|
+
config.should be_a(Hash)
|
10
|
+
config["auth"]["username"].should == "ferris"
|
11
|
+
config["auth"]["password"].should == "test"
|
12
|
+
end
|
8
13
|
|
14
|
+
it "should return the content of a given config file as a Hashie::Mash" do
|
15
|
+
config = Ambience.new(config_file(:basic)).to_mash
|
16
|
+
|
9
17
|
config.should be_a(Hashie::Mash)
|
10
18
|
config.auth.username.should == "ferris"
|
11
19
|
config[:auth][:address].should == "http://example.com"
|
@@ -13,16 +21,16 @@ describe Ambience do
|
|
13
21
|
end
|
14
22
|
|
15
23
|
it "should return the config for an optional environment as a Hashie::Mash" do
|
16
|
-
config = Ambience.new
|
17
|
-
|
24
|
+
config = Ambience.new(config_file(:environments), :production).to_mash
|
25
|
+
|
18
26
|
config.should be_a(Hashie::Mash)
|
19
27
|
config.auth.username.should == "ferris"
|
20
28
|
config[:auth]["password"].should == "topsecret"
|
21
29
|
end
|
22
30
|
|
23
31
|
it "should return the config specified as Symbols as a Hashie::Mash" do
|
24
|
-
config = Ambience.new
|
25
|
-
|
32
|
+
config = Ambience.new(config_file(:symbols), "production").to_mash
|
33
|
+
|
26
34
|
config.should be_a(Hashie::Mash)
|
27
35
|
config.auth.username.should == "ferris"
|
28
36
|
config[:auth]["password"].should == "test"
|
@@ -30,11 +38,11 @@ describe Ambience do
|
|
30
38
|
|
31
39
|
it "should merge the config with any JVM properties when using JRuby" do
|
32
40
|
Ambience.stubs(:jruby?).returns(true)
|
33
|
-
Ambience.stubs(:jvm_properties).returns(
|
41
|
+
Ambience.any_instance.stubs(:jvm_properties).returns(
|
34
42
|
"auth.address" => "http://live.example.com",
|
35
43
|
"auth.password" => "topsecret"
|
36
44
|
)
|
37
|
-
config = Ambience.new
|
45
|
+
config = Ambience.new(config_file(:basic)).to_mash
|
38
46
|
|
39
47
|
config.should be_a(Hashie::Mash)
|
40
48
|
config.auth.username.should == "ferris"
|
@@ -43,7 +51,7 @@ describe Ambience do
|
|
43
51
|
end
|
44
52
|
|
45
53
|
it "should raise an ArgumentError in case the given config file could not be found" do
|
46
|
-
lambda { Ambience.new
|
54
|
+
lambda { Ambience.new("missing_config.yml").to_hash }.should raise_error(ArgumentError)
|
47
55
|
end
|
48
56
|
|
49
57
|
end
|
data/spec/fixtures/basic.yml
CHANGED
File without changes
|
File without changes
|
data/spec/fixtures/symbols.yml
CHANGED
File without changes
|
data/spec/spec_helper.rb
CHANGED
File without changes
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ambience
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 3
|
8
|
+
- 0
|
9
|
+
version: 0.3.0
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Daniel Harrington
|
@@ -9,39 +14,51 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-
|
17
|
+
date: 2010-05-13 00:00:00 +02:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: hashie
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
24
|
requirements:
|
21
25
|
- - ">="
|
22
26
|
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 2
|
30
|
+
- 0
|
23
31
|
version: 0.2.0
|
24
|
-
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
25
34
|
- !ruby/object:Gem::Dependency
|
26
35
|
name: rspec
|
27
|
-
|
28
|
-
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
38
|
requirements:
|
31
39
|
- - ">="
|
32
40
|
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 1
|
43
|
+
- 2
|
44
|
+
- 8
|
33
45
|
version: 1.2.8
|
34
|
-
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
35
48
|
- !ruby/object:Gem::Dependency
|
36
49
|
name: mocha
|
37
|
-
|
38
|
-
|
39
|
-
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
40
52
|
requirements:
|
41
53
|
- - ">="
|
42
54
|
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
- 9
|
58
|
+
- 7
|
43
59
|
version: 0.9.7
|
44
|
-
|
60
|
+
type: :development
|
61
|
+
version_requirements: *id003
|
45
62
|
description:
|
46
63
|
email: me@rubiii.com
|
47
64
|
executables: []
|
@@ -79,18 +96,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
79
96
|
requirements:
|
80
97
|
- - ">="
|
81
98
|
- !ruby/object:Gem::Version
|
99
|
+
segments:
|
100
|
+
- 0
|
82
101
|
version: "0"
|
83
|
-
version:
|
84
102
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
103
|
requirements:
|
86
104
|
- - ">="
|
87
105
|
- !ruby/object:Gem::Version
|
106
|
+
segments:
|
107
|
+
- 0
|
88
108
|
version: "0"
|
89
|
-
version:
|
90
109
|
requirements: []
|
91
110
|
|
92
111
|
rubyforge_project:
|
93
|
-
rubygems_version: 1.3.
|
112
|
+
rubygems_version: 1.3.6
|
94
113
|
signing_key:
|
95
114
|
specification_version: 3
|
96
115
|
summary: App configuration feat. YAML and JVM properties
|