ambience 0.3.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ .DS_Store
2
+ .yardoc
3
+ doc
4
+ coverage
5
+ tmp
6
+ *~
7
+ *.gem
8
+ .bundle
9
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+
@@ -0,0 +1,8 @@
1
+ script: "rake"
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - ree
6
+ - rbx
7
+ - jruby
8
+
@@ -1,38 +1,50 @@
1
+ == 1.0.0 (2011-06-15)
2
+
3
+ * Cleaned up the project's setup.
4
+ * Moved the config into a module. Instead of calling `Ambience.new`, you can now use
5
+ `Ambience.create` to create the config.
6
+ * Added a Railtie which gets loaded when `Rails` is defined. The Railtie creates an
7
+ `AppConfig` mash from `config/ambience.yml` if such a file exists.
8
+ * Added travis integration: http://travis-ci.org/#!/rubiii/ambience
9
+
1
10
  == 0.3.1 (2010-05-27)
11
+
2
12
  * Ambience now requires "java" (for JRuby support) if it's available.
3
13
 
4
14
  == 0.3.0 (2010-05-13)
15
+
5
16
  * Ambience now operates on the instance level. So after setting up a new Ambience config
6
17
  like before:
7
-
8
- AppConfig = Ambience.new File.join(Rails.root, "config", "ambience.yml")
9
-
18
+
19
+ AppConfig = Ambience.new File.join(Rails.root, "config", "ambience.yml")
20
+
10
21
  you now have to choose whether you like to have the config returned as a simple Hash
11
22
  or a Hashie::Mash (which was the default since version 0.2.0):
12
-
13
- AppConfig.to_hash
14
- AppConfig.to_mash
15
-
23
+
24
+ AppConfig.to_hash
25
+ AppConfig.to_mash
26
+
16
27
  * Along with support for a basic config and JVM properties, version 0.3.0 adds support
17
28
  for local (user-specific) settings. By default, Ambience tries to load a local YAML
18
29
  config from:
19
-
20
- File.join ENV["HOME"].to_s, ".ambience", "ambience.yml"
21
-
30
+
31
+ File.join ENV["HOME"].to_s, ".ambience", "ambience.yml"
32
+
22
33
  Ambience will still work fine if the file does not exist. But if you want to use a
23
34
  local config and change the default location, you can use the +local_config+ class
24
35
  method to do so.
25
-
26
- Ambience.local_config # => "/Users/you/.ambience/ambience.yml"
27
-
28
- # Change the location of the local config file:
29
- Ambience.local_config = File.join "config", "user_config.yml"
36
+
37
+ Ambience.local_config # => "/Users/you/.ambience/ambience.yml"
38
+
39
+ # Change the location of the local config file:
40
+ Ambience.local_config = File.join "config", "user_config.yml"
30
41
 
31
42
  == 0.2.0 (2010-03-06)
43
+
32
44
  * Complete rewrite. Removed Rails-specific defaults and returning the Ambience config
33
45
  as a Hashie::Mash (http://github.com/intridea/hashie). Take a look at the new Readme
34
46
  and Specs for examples.
35
47
 
36
48
  == 0.1.0 (2009-12-12)
37
- * Initial release.
38
49
 
50
+ * Initial release.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2010 Daniel Harrington
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.
21
+
@@ -0,0 +1,74 @@
1
+ Ambience [![Build Status](http://travis-ci.org/rubiii/ambience.png)](http://travis-ci.org/rubiii/ambience)
2
+ ========
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.
6
+
7
+
8
+ Installation
9
+ ------------
10
+
11
+ Ambience is available through [Rubygems](http://rubygems.org/gems/ambience) and can be installed via:
12
+
13
+ ```
14
+ $ gem install ambience
15
+ ```
16
+
17
+
18
+ Getting started
19
+ ---------------
20
+
21
+ Given you created a YAML config like this:
22
+
23
+ ``` yml
24
+ auth:
25
+ address: http://example.com
26
+ username: ferris
27
+ password: test
28
+ ```
29
+
30
+ You can instantiate an Ambience config by passing in the path to your config file:
31
+
32
+ ``` ruby
33
+ AppConfig = Ambience.create Rails.root.join("config", "ambience.yml")
34
+ ```
35
+
36
+ Ambience will load and convert your config into a Hash:
37
+
38
+ ``` ruby
39
+ { "auth" => { "address" => "http://example.com", "username" => "ferris", "password" => "test" } }
40
+ ```
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:
44
+
45
+ ``` ruby
46
+ auth.address = "http://live.example.com"
47
+ auth.password = "topsecret"
48
+ ```
49
+
50
+ The result would be something like this:
51
+
52
+ ``` ruby
53
+ { "auth" => { "address" => "http://live.example.com", "username" => "ferris", "password" => "topsecret" } }
54
+ ```
55
+
56
+ In the end you can decide whether you want to return the config as a Hash:
57
+
58
+ ``` ruby
59
+ AppConfig = Ambience.create(Rails.root.join("config", "ambience.yml")).to_hash
60
+ ```
61
+
62
+ or a [Hashie::Mash](http://github.com/intridea/hashie):
63
+
64
+ ``` ruby
65
+ AppConfig = Ambience.create(Rails.root.join("config", "ambience.yml")).to_mash
66
+ ```
67
+
68
+
69
+ Railtie
70
+ -------
71
+
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.
74
+ All this happens before Rails evaluates your environment config.
data/Rakefile CHANGED
@@ -1,31 +1,11 @@
1
- require "rake"
2
- require "spec/rake/spectask"
3
- require "spec/rake/verify_rcov"
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
4
3
 
5
- task :default => :spec
6
-
7
- Spec::Rake::SpecTask.new do |spec|
8
- spec.spec_files = FileList["spec/**/*_spec.rb"]
9
- spec.spec_opts << "--color"
10
- spec.libs += ["lib", "spec"]
11
- spec.rcov = true
12
- end
4
+ require "rspec/core/rake_task"
13
5
 
14
- RCov::VerifyTask.new(:spec_verify => :spec) do |verify|
15
- verify.threshold = 100.0
16
- verify.index_html = "rcov/index.html"
6
+ RSpec::Core::RakeTask.new do |t|
7
+ t.rspec_opts = %w(-c)
17
8
  end
18
9
 
19
- begin
20
- require "hanna/rdoctask"
21
-
22
- Rake::RDocTask.new do |rdoc|
23
- rdoc.title = "Ambience - App configuration feat. YAML and JVM properties"
24
- rdoc.rdoc_dir = "doc"
25
- rdoc.rdoc_files.include("**/*.rdoc").include("lib/**/*.rb")
26
- rdoc.options << "--line-numbers"
27
- rdoc.options << "--webcvs=http://github.com/rubiii/ambience/tree/master/"
28
- end
29
- rescue LoadError
30
- puts "'gem install hanna' for documentation"
31
- end
10
+ task :default => :spec
11
+ task :test => :spec
@@ -0,0 +1,25 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "ambience/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "ambience"
6
+ s.version = Ambience::VERSION
7
+ s.author = "Daniel Harrington"
8
+ s.email = "me@rubiii.com"
9
+ s.homepage = "http://github.com/rubiii/#{s.name}"
10
+ s.summary = %q{App configuration feat. YAML and JVM properties}
11
+ s.description = s.summary
12
+
13
+ s.rubyforge_project = s.name
14
+
15
+ s.add_dependency "hashie", ">= 0.2.0"
16
+
17
+ s.add_development_dependency "rspec", "~> 2.6.0"
18
+ s.add_development_dependency "mocha", "~> 0.9.12"
19
+ s.add_development_dependency "autotest"
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
25
+ end
@@ -1,7 +1,16 @@
1
- require "yaml" unless defined? YAML
2
- require "erb" unless defined? ERB
1
+ require "ambience/config"
2
+ require "ambience/version"
3
3
 
4
- require "hashie"
4
+ require "ambience/railtie" if defined? Rails
5
5
 
6
- require "ambience/core_ext"
7
- require "ambience/ambience"
6
+ module Ambience
7
+
8
+ def self.create(config_file, env = nil)
9
+ Config.new(config_file, env)
10
+ end
11
+
12
+ def self.jruby?
13
+ RUBY_PLATFORM =~ /java/
14
+ end
15
+
16
+ end
@@ -0,0 +1,86 @@
1
+ require "yaml" unless defined? YAML
2
+ require "erb" unless defined? ERB
3
+
4
+ require "hashie"
5
+ require "ambience/core_ext"
6
+
7
+ begin
8
+ require "java"
9
+ rescue LoadError
10
+ end
11
+
12
+ module Ambience
13
+
14
+ # = Ambience::Config
15
+ #
16
+ # App configuration feat. YAML and JVM properties. Lets you specify a default configuration
17
+ # in a YAML file and overwrite details via local settings and JVM properties for production.
18
+ class Config
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
24
+
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
+ end
30
+
31
+ def initialize(config_file, env = nil)
32
+ @config_file, @env = config_file, env
33
+ end
34
+
35
+ # Returns the Ambience config as a Hash.
36
+ def to_hash
37
+ config = load_config @config_file
38
+ config = config.deep_merge local_config
39
+ config.deep_merge jvm_config
40
+ end
41
+
42
+ # Returns the Ambience config as a Hashie::Mash.
43
+ def to_mash
44
+ Hashie::Mash.new to_hash
45
+ end
46
+
47
+ private
48
+
49
+ def load_config(config_file)
50
+ raise ArgumentError, "Missing config: #{config_file}" unless File.exist? config_file
51
+
52
+ config = File.read config_file
53
+ config = YAML.load ERB.new(config).result || {}
54
+ config = config[@env.to_s] || config[@env.to_sym] if @env
55
+ config
56
+ end
57
+
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
65
+ jvm_properties.inject({}) do |hash, (key, value)|
66
+ hash.deep_merge hash_from_property(key, value)
67
+ end
68
+ end
69
+
70
+ # Returns the JVM properties.
71
+ def jvm_properties
72
+ Ambience.jruby? ? java.lang.System.get_properties : {}
73
+ end
74
+
75
+ # Returns a Hash generated from a JVM +property+ and its +value+.
76
+ #
77
+ # ==== Example:
78
+ #
79
+ # hash_from_property "webservice.auth.address", "http://auth.example.com"
80
+ # # => { "webservice" => { "auth" => { "address" => "http://auth.example.com" } } }
81
+ def hash_from_property(property, value)
82
+ property.split(".").reverse.inject(value) { |value, item| { item => value } }
83
+ end
84
+
85
+ end
86
+ end
@@ -1,19 +1,25 @@
1
- class Hash
1
+ module Ambience
2
+ module CoreExt
3
+ module Hash
2
4
 
3
- # Returns a new Hash with self and +other_hash+ merged recursively.
4
- # Implementation from ActiveSupport::CoreExtensions::Hash::DeepMerge.
5
- def deep_merge(other_hash)
6
- merge(other_hash) do |key, oldval, newval|
7
- oldval = oldval.to_hash if oldval.respond_to? :to_hash
8
- newval = newval.to_hash if newval.respond_to? :to_hash
9
- oldval.class.to_s == "Hash" && newval.class.to_s == "Hash" ? oldval.deep_merge(newval) : newval
10
- end
11
- end
5
+ # Returns a new Hash with self and +other_hash+ merged recursively.
6
+ # Implementation from ActiveSupport::CoreExtensions::Hash::DeepMerge.
7
+ def deep_merge(other_hash)
8
+ merge(other_hash) do |key, oldval, newval|
9
+ oldval = oldval.to_hash if oldval.respond_to? :to_hash
10
+ newval = newval.to_hash if newval.respond_to? :to_hash
11
+ oldval.class.to_s == "Hash" && newval.class.to_s == "Hash" ? oldval.deep_merge(newval) : newval
12
+ end
13
+ end
12
14
 
13
- # Returns a new Hash with self and +other_hash+ merged recursively. Modifies the receiver in place.
14
- # Implementation from ActiveSupport::CoreExtensions::Hash::DeepMerge.
15
- def deep_merge!(other_hash)
16
- replace deep_merge(other_hash)
17
- end
15
+ # Returns a new Hash with self and +other_hash+ merged recursively. Modifies the receiver in place.
16
+ # Implementation from ActiveSupport::CoreExtensions::Hash::DeepMerge.
17
+ def deep_merge!(other_hash)
18
+ replace deep_merge(other_hash)
19
+ end
18
20
 
21
+ end
22
+ end
19
23
  end
24
+
25
+ Hash.send :include, Ambience::CoreExt::Hash
@@ -0,0 +1,13 @@
1
+ module Ambience
2
+ class Railtie < Rails::Railtie
3
+
4
+ initializer 'ambience.railtie', :before => :load_environment_config do |app|
5
+ default_config_file = Rails.root.join("config", "ambience.yml")
6
+
7
+ if File.exist?(default_config_file)
8
+ ::AppConfig = Ambience.create(default_config_file, Rails.env).to_mash
9
+ end
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module Ambience
2
+
3
+ VERSION = "1.0.0"
4
+
5
+ end
@@ -1,57 +1,70 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Ambience do
4
- include SpecHelper
5
4
 
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
-
5
+ it "returns the content of a given config file as a Hash" do
6
+ config = Ambience.create(config_file(:basic)).to_hash
7
+
9
8
  config.should be_a(Hash)
10
9
  config["auth"]["username"].should == "ferris"
11
10
  config["auth"]["password"].should == "test"
12
11
  end
13
12
 
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
-
13
+ it "returns the content of a given config file as a Hashie::Mash" do
14
+ config = Ambience.create(config_file(:basic)).to_mash
15
+
17
16
  config.should be_a(Hashie::Mash)
18
17
  config.auth.username.should == "ferris"
19
18
  config[:auth][:address].should == "http://example.com"
20
19
  config["auth"]["password"].should == "test"
21
20
  end
22
21
 
23
- it "should return the config for an optional environment as a Hashie::Mash" do
24
- config = Ambience.new(config_file(:environments), :production).to_mash
25
-
22
+ it "returns the config for an optional environment as a Hashie::Mash" do
23
+ config = Ambience.create(config_file(:environments), :production).to_mash
24
+
26
25
  config.should be_a(Hashie::Mash)
27
26
  config.auth.username.should == "ferris"
28
27
  config[:auth]["password"].should == "topsecret"
29
28
  end
30
29
 
31
- it "should return the config specified as Symbols as a Hashie::Mash" do
32
- config = Ambience.new(config_file(:symbols), "production").to_mash
33
-
30
+ it "returns the config specified as Symbols as a Hashie::Mash" do
31
+ config = Ambience.create(config_file(:symbols), "production").to_mash
32
+
34
33
  config.should be_a(Hashie::Mash)
35
34
  config.auth.username.should == "ferris"
36
35
  config[:auth]["password"].should == "test"
37
36
  end
38
37
 
39
- it "should merge the config with any JVM properties when using JRuby" do
40
- Ambience.stubs(:jruby?).returns(true)
41
- Ambience.any_instance.stubs(:jvm_properties).returns(
42
- "auth.address" => "http://live.example.com",
43
- "auth.password" => "topsecret"
44
- )
45
- config = Ambience.new(config_file(:basic)).to_mash
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)
40
+ end
41
+
42
+ context "when using JRuby" do
43
+ before do
44
+ Ambience::Config.send(:define_method, :java) { JavaMock }
45
+ end
46
46
 
47
- config.should be_a(Hashie::Mash)
48
- config.auth.username.should == "ferris"
49
- config[:auth][:address].should == "http://live.example.com"
50
- config["auth"]["password"].should == "topsecret"
47
+ after do
48
+ Ambience::Config.send(:remove_method, :java)
49
+ end
50
+
51
+ 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
+ config = Ambience.create(config_file(:basic)).to_mash
58
+
59
+ config.should be_a(Hashie::Mash)
60
+ config.auth.username.should == "ferris"
61
+ config[:auth][:address].should == "http://live.example.com"
62
+ config["auth"]["password"].should == "topsecret"
63
+ end
51
64
  end
52
65
 
53
- it "should raise an ArgumentError in case the given config file could not be found" do
54
- lambda { Ambience.new("missing_config.yml").to_hash }.should raise_error(ArgumentError)
66
+ def config_file(name)
67
+ File.expand_path("../../fixtures/#{name}.yml", __FILE__)
55
68
  end
56
69
 
57
70
  end
@@ -1,16 +1,4 @@
1
- require "spec"
2
- require "mocha"
1
+ require "bundler"
2
+ Bundler.require :default, :development
3
3
 
4
- Spec::Runner.configure do |config|
5
- config.mock_with :mocha
6
- end
7
-
8
- require "ambience"
9
-
10
- module SpecHelper
11
-
12
- def config_file(name)
13
- File.join File.dirname(__FILE__), "fixtures", "#{name}.yml"
14
- end
15
-
16
- end
4
+ require "support/java_mock"
@@ -0,0 +1,15 @@
1
+ class JavaMock
2
+
3
+ def self.lang
4
+ self
5
+ end
6
+
7
+ def self.System
8
+ self
9
+ end
10
+
11
+ def self.get_properties
12
+ { "auth.address" => "http://live.example.com", "auth.password" => "topsecret" }
13
+ end
14
+
15
+ end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ambience
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 23
5
+ prerelease:
5
6
  segments:
6
- - 0
7
- - 3
8
7
  - 1
9
- version: 0.3.1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - Daniel Harrington
@@ -14,16 +15,18 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-05-27 00:00:00 +02:00
18
+ date: 2011-06-15 00:00:00 +02:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: hashie
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ">="
26
28
  - !ruby/object:Gem::Version
29
+ hash: 23
27
30
  segments:
28
31
  - 0
29
32
  - 2
@@ -35,84 +38,115 @@ dependencies:
35
38
  name: rspec
36
39
  prerelease: false
37
40
  requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
38
42
  requirements:
39
- - - ">="
43
+ - - ~>
40
44
  - !ruby/object:Gem::Version
45
+ hash: 23
41
46
  segments:
42
- - 1
43
47
  - 2
44
- - 8
45
- version: 1.2.8
48
+ - 6
49
+ - 0
50
+ version: 2.6.0
46
51
  type: :development
47
52
  version_requirements: *id002
48
53
  - !ruby/object:Gem::Dependency
49
54
  name: mocha
50
55
  prerelease: false
51
56
  requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
52
58
  requirements:
53
- - - ">="
59
+ - - ~>
54
60
  - !ruby/object:Gem::Version
61
+ hash: 35
55
62
  segments:
56
63
  - 0
57
64
  - 9
58
- - 7
59
- version: 0.9.7
65
+ - 12
66
+ version: 0.9.12
60
67
  type: :development
61
68
  version_requirements: *id003
62
- description:
69
+ - !ruby/object:Gem::Dependency
70
+ name: autotest
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ type: :development
82
+ version_requirements: *id004
83
+ description: App configuration feat. YAML and JVM properties
63
84
  email: me@rubiii.com
64
85
  executables: []
65
86
 
66
87
  extensions: []
67
88
 
68
- extra_rdoc_files:
69
- - README.rdoc
89
+ extra_rdoc_files: []
90
+
70
91
  files:
71
- - CHANGELOG
92
+ - .gitignore
93
+ - .rspec
94
+ - .travis.yml
95
+ - CHANGELOG.md
96
+ - Gemfile
97
+ - LICENSE
98
+ - README.md
72
99
  - Rakefile
73
- - README.rdoc
74
- - lib/ambience/ambience.rb
75
- - lib/ambience/core_ext.rb
100
+ - ambience.gemspec
76
101
  - lib/ambience.rb
102
+ - lib/ambience/config.rb
103
+ - lib/ambience/core_ext.rb
104
+ - lib/ambience/railtie.rb
105
+ - lib/ambience/version.rb
77
106
  - spec/ambience/ambience_spec.rb
78
- - spec/spec_helper.rb
79
107
  - spec/fixtures/basic.yml
80
108
  - spec/fixtures/environments.yml
81
109
  - spec/fixtures/symbols.yml
110
+ - spec/spec_helper.rb
111
+ - spec/support/java_mock.rb
82
112
  has_rdoc: true
83
113
  homepage: http://github.com/rubiii/ambience
84
114
  licenses: []
85
115
 
86
116
  post_install_message:
87
- rdoc_options:
88
- - --charset=UTF-8
89
- - --line-numbers
90
- - --inline-source
91
- - --title
92
- - Ambience - App configuration feat. YAML and JVM properties
117
+ rdoc_options: []
118
+
93
119
  require_paths:
94
120
  - lib
95
121
  required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
96
123
  requirements:
97
124
  - - ">="
98
125
  - !ruby/object:Gem::Version
126
+ hash: 3
99
127
  segments:
100
128
  - 0
101
129
  version: "0"
102
130
  required_rubygems_version: !ruby/object:Gem::Requirement
131
+ none: false
103
132
  requirements:
104
133
  - - ">="
105
134
  - !ruby/object:Gem::Version
135
+ hash: 3
106
136
  segments:
107
137
  - 0
108
138
  version: "0"
109
139
  requirements: []
110
140
 
111
- rubyforge_project:
112
- rubygems_version: 1.3.6
141
+ rubyforge_project: ambience
142
+ rubygems_version: 1.4.1
113
143
  signing_key:
114
144
  specification_version: 3
115
145
  summary: App configuration feat. YAML and JVM properties
116
146
  test_files:
117
147
  - spec/ambience/ambience_spec.rb
148
+ - spec/fixtures/basic.yml
149
+ - spec/fixtures/environments.yml
150
+ - spec/fixtures/symbols.yml
118
151
  - spec/spec_helper.rb
152
+ - spec/support/java_mock.rb
@@ -1,41 +0,0 @@
1
- = Ambience
2
-
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
-
5
- == Installation
6
-
7
- $ gem install ambience
8
-
9
- == How it works
10
-
11
- Given you created a YAML config like this:
12
-
13
- auth:
14
- address: http://example.com
15
- username: ferris
16
- password: test
17
-
18
- You can instantiate an Ambience config by passing in the path to your config file:
19
-
20
- AppConfig = Ambience.new File.join(Rails.root, "config", "ambience.yml")
21
-
22
- Ambience will load and convert your config into a Hash:
23
-
24
- { "auth" => { "address" => "http://example.com", "username" => "ferris", "password" => "test" } }
25
-
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
-
28
- auth.address = "http://live.example.com"
29
- auth.password = "topsecret"
30
-
31
- The result would be something like this:
32
-
33
- { "auth" => { "address" => "http://live.example.com", "username" => "ferris", "password" => "topsecret" } }
34
-
35
- In the end you can decide whether you want to return the config as a Hash:
36
-
37
- AppConfig = Ambience.new(File.join(Rails.root, "config", "ambience.yml")).to_hash
38
-
39
- or a {Hashie::Mash}[http://github.com/intridea/hashie]:
40
-
41
- AppConfig = Ambience.new(File.join(Rails.root, "config", "ambience.yml")).to_mash
@@ -1,93 +0,0 @@
1
- begin
2
- require "java"
3
- rescue LoadError
4
- end
5
-
6
- # = Ambience
7
- #
8
- # App configuration feat. YAML and JVM properties. Lets you specify a default configuration
9
- # in a YAML file and overwrite details via local settings and JVM properties for production.
10
- class Ambience
11
-
12
- # Sets the path to a local ambience config file.
13
- def self.local_config=(local_config)
14
- @@local_config = local_config
15
- end
16
-
17
- # Returns the path to a local ambience config file for specifying user-specific
18
- # settings that don't belong into the application config file.
19
- def self.local_config
20
- @@local_config ||= File.join ENV["HOME"].to_s, ".ambience", "ambience.yml"
21
- end
22
-
23
- # Returns whether the current Ruby platfrom is JRuby.
24
- def self.jruby?
25
- RUBY_PLATFORM =~ /java/
26
- end
27
-
28
- def initialize(config_file, env = nil)
29
- @config_file, @env = config_file, env
30
- end
31
-
32
- # Returns the Ambience config as a Hash.
33
- def to_hash
34
- config = load_config @config_file
35
- config = config.deep_merge local_config
36
- config.deep_merge jvm_config
37
- end
38
-
39
- # Returns the Ambience config as a Hashie::Mash.
40
- def to_mash
41
- Hashie::Mash.new to_hash
42
- end
43
-
44
- private
45
-
46
- def load_config(config_file)
47
- raise ArgumentError, "Missing config: #{config_file}" unless File.exist? config_file
48
-
49
- config = File.read config_file
50
- config = YAML.load ERB.new(config).result || {}
51
- config = config[@env.to_s] || config[@env.to_sym] if @env
52
- config
53
- end
54
-
55
- # Returns a Hash containing any local settings from the +@@local_config+.
56
- def local_config
57
- File.exist?(self.class.local_config) ? load_config(self.class.local_config) : {}
58
- end
59
-
60
- ## Returns a Hash containing any JVM properties.
61
- def jvm_config
62
- jvm_properties.inject({}) do |hash, (key, value)|
63
- hash.deep_merge hash_from_property(key, value)
64
- end
65
- end
66
-
67
- # Returns the JVM properties.
68
- def jvm_properties
69
- JavaLang::System.get_properties rescue {}
70
- end
71
-
72
- # Returns a Hash generated from a JVM +property+ and its +value+.
73
- #
74
- # ==== Example:
75
- #
76
- # hash_from_property "webservice.auth.address", "http://auth.example.com"
77
- # # => { "webservice" => { "auth" => { "address" => "http://auth.example.com" } } }
78
- def hash_from_property(property, value)
79
- property.split(".").reverse.inject(value) { |value, item| { item => value } }
80
- end
81
-
82
- # Returns the JVM properties.
83
- def jvm_properties
84
- self.class.jruby? ? JavaLang::System.get_properties : {}
85
- end
86
-
87
- if jruby?
88
- module JavaLang
89
- include_package "java.lang"
90
- end
91
- end
92
-
93
- end