ambience 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,8 @@
1
+ == 0.2.0 (2010-03-06)
2
+ * Complete rewrite. Removed Rails-specific defaults and returning the Ambience config
3
+ as a Hashie::Mash (http://github.com/intridea/hashie). Take a look at the new Readme
4
+ and Specs for examples.
5
+
6
+ == 0.1.0 (2009-12-12)
7
+ * Initial release.
8
+
data/README.rdoc CHANGED
@@ -1,78 +1,45 @@
1
1
  = Ambience
2
2
 
3
- Ruby on Rails configuration with JVM Parameter spicing.
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.
4
4
 
5
- Loads the content from a default configuration file. Merges these defaults
6
- with the available JVM properties when running the application on JRuby.
7
-
8
- == Install
9
-
10
- As a gem:
5
+ == Installation
11
6
 
12
7
  $ gem install ambience
13
8
 
14
- As a Rails plugin:
15
-
16
- $ script/plugin install git://github.com/rubiii/ambience.git
17
-
18
- == Default configuration
19
-
20
- Default configuration at config/ambience.yml
21
-
22
- defaults: &defaults
23
- auth:
24
- user: "client"
25
- password: "secret"
26
- service: "http://test.example.com"
27
-
28
- development:
29
- <<: *defaults
9
+ == How it works
30
10
 
31
- test:
32
- <<: *defaults
11
+ Given you created a YAML config like this one:
33
12
 
34
- production:
35
- <<: *defaults
13
+ auth:
14
+ address: http://example.com
15
+ username: ferris
16
+ password: test
36
17
 
37
- == Loading the configuration
18
+ You create an Ambience config by passing in the path to your config file:
38
19
 
39
- Place this at the bottom of your config/environment.rb file:
20
+ AppConfig = Ambience.new File.join(Rails.root, "config", "ambience.yml")
40
21
 
41
- AppConfig = Ambience.new
22
+ Ambience will load and convert your config into a Hash:
42
23
 
43
- == Running your application on MRI
24
+ { "auth" => { "address" => "http://example.com", "username" => "ferris", "password" => "test" } }
44
25
 
45
- The configuration Hash equals the default configuration file:
26
+ Then it looks for any JVM properties (if your running JRuby)
46
27
 
47
- "auth" => {
48
- "user" => "client",
49
- "password => "secret",
50
- "service" => "http://test.example.com"
51
- }
28
+ auth.address = "http://live.example.com"
29
+ auth.password = "topsecret"
52
30
 
53
- == Running the application on JRuby
31
+ and deep merge them with your Hash:
54
32
 
55
- Expecting these default JVM properties:
33
+ { "auth" => { "address" => "http://live.example.com", "username" => "ferris", "password" => "topsecret" } }
56
34
 
57
- jruby.home = "/usr/local/lib/jruby"
58
- java.runtime.name = "Java(TM) SE Runtime Environment"
59
- java.runtime.version = "1.6.0_14-b08"
35
+ Finally, it returns the Hash as a {Hashie::Mash}[http://github.com/intridea/hashie] so you can access values by specifying keys as Symbols or Strings or using method-like accessors:
60
36
 
61
- And the following custom property:
37
+ AppConfig[:auth][:address]
38
+ # => "http://live.example.com"
62
39
 
63
- auth.service = "http://live.example.com"
40
+ AppConfig["auth"]["username"]
41
+ # => "http://live.example.com"
64
42
 
65
- The default configuration will be extended by JVM properties:
43
+ AppConfig.auth.password
44
+ # => "topsecret"
66
45
 
67
- "auth" => {
68
- "user" => "client",
69
- "password => "secret",
70
- "service" => "http://live.example.com"
71
- },
72
- "java" => {
73
- "runtime" => {
74
- "name" => "Java(TM) SE Runtime Environment",
75
- "version" => "1.6.0_14-b08"
76
- }
77
- },
78
- "jruby" => { "home" => "/usr/local/lib/jruby" }
data/Rakefile CHANGED
@@ -1,42 +1,31 @@
1
- require "rubygems"
2
1
  require "rake"
3
- require "rake/testtask"
4
- require "rake/rdoctask"
2
+ require "spec/rake/spectask"
3
+ require "spec/rake/verify_rcov"
5
4
 
6
- task :default => :test
5
+ task :default => :spec
7
6
 
8
- Rake::TestTask.new(:test) do |t|
9
- t.libs << "lib"
10
- t.libs << "test"
11
- t.pattern = "test/**/*_test.rb"
12
- t.verbose = true
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
13
12
  end
14
13
 
15
- Rake::RDocTask.new do |rdoc|
16
- rdoc.title = "Ambience"
17
- rdoc.rdoc_dir = "rdoc"
18
- rdoc.rdoc_files.include("lib/**/*.rb")
19
- rdoc.options = ["--line-numbers", "--inline-source"]
14
+ RCov::VerifyTask.new(:spec_verify => :spec) do |verify|
15
+ verify.threshold = 100.0
16
+ verify.index_html = "rcov/index.html"
20
17
  end
21
18
 
22
19
  begin
23
- require "jeweler"
24
- Jeweler::Tasks.new do |spec|
25
- spec.name = "ambience"
26
- spec.author = "Daniel Harrington"
27
- spec.email = "me@rubiii.com"
28
- spec.homepage = "http://github.com/rubiii/ambience"
29
- spec.summary = "JVM-Parameters for your JRuby app"
30
- spec.description = spec.summary
20
+ require "hanna/rdoctask"
31
21
 
32
- spec.files = FileList["[A-Z]*", "init.rb", "{lib,test}/**/*.{rb,yml}"]
33
-
34
- spec.rdoc_options += [
35
- "--title", "Ambience",
36
- "--line-numbers",
37
- "--inline-source"
38
- ]
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/"
39
28
  end
40
29
  rescue LoadError
41
- puts "Jeweler missing. Install with: gem install jeweler"
42
- end
30
+ puts "'gem install hanna' for documentation"
31
+ end
data/lib/ambience.rb CHANGED
@@ -1,88 +1,7 @@
1
- require "rubygems"
2
1
  require "yaml" unless defined? YAML
3
2
  require "erb" unless defined? ERB
4
3
 
5
- class Ambience
6
- class << self
4
+ require "hashie"
7
5
 
8
- # Returns a new config Hash for a given Rails +environment+ from a given
9
- # +config_file+. Adds the current JVM properties to the config Hash in case
10
- # the application is running on JRuby.
11
- def new(environment = RAILS_ENV, config_file = nil)
12
- hash = setup_config environment, load_config_file(config_file)
13
- hash = setup_jvm hash
14
- end
15
-
16
- # Returns if the application's running on JRuby.
17
- def jruby?
18
- RUBY_PLATFORM =~ /java/
19
- end
20
-
21
- private
22
-
23
- # Loads the given +config_file+. Tries to load "ambience.yml" from the
24
- # application's config folder in case no +config_file+ was given. Returns
25
- # the content from the config file or nil in case no config file was found.
26
- def load_config_file(config_file)
27
- config_file ||= File.join(RAILS_ROOT, "config", "ambience.yml")
28
- config = File.expand_path(config_file)
29
- file = File.read(config) if File.exist? config
30
- file ||= nil
31
- end
32
-
33
- # Returns the ERB-interpreted content at the given +env+ from a given Yaml
34
- # +config+ String and returns a Hash containing the evaluated content.
35
- # Defaults to returning an empty Hash in case +config+ is nil.
36
- def setup_config(env, config)
37
- hash = YAML.load(ERB.new(config).result)[env] unless config.nil?
38
- hash ||= {}
39
- end
40
-
41
- # Expects the current config +hash+, iterates through the JVM properties,
42
- # adds them to the given +hash+ and returns the merged result.
43
- def setup_jvm(hash)
44
- if jruby?
45
- jvm_properties.each do |key, value|
46
- param = hash_from_property key, value
47
- hash = deep_merge(hash, param) unless hash.nil?
48
- end
49
- end
50
- hash
51
- end
52
-
53
- # Merges a given +hash+ with a +target+ Hash and returns the merged result.
54
- def deep_merge(hash, target)
55
- target.keys.each do |key|
56
- if target[key].is_a? Hash and hash[key].is_a? Hash
57
- hash[key] = deep_merge(hash[key], target[key])
58
- next
59
- end
60
- hash[key] = target[key]
61
- end
62
- hash
63
- end
64
-
65
- # Expects +key+ and +value+ from a JVM property and returns a Hash that
66
- # complies to the YAML format.
67
- def hash_from_property(key, value)
68
- hash, split = {}, key.split(".")
69
- (split.size-1).downto(0) do |i|
70
- v = i == (split.size-1) ? value : hash
71
- hash = { split[i] => v }
72
- end
73
- hash
74
- end
75
-
76
- # Returns the JVM properties.
77
- def jvm_properties
78
- JavaLang::System.get_properties
79
- end
80
-
81
- end
82
- end
83
-
84
- if Ambience.jruby?
85
- module JavaLang
86
- include_package "java.lang"
87
- end
88
- end
6
+ require "ambience/core_ext"
7
+ require "ambience/ambience"
@@ -0,0 +1,104 @@
1
+ # = Ambience
2
+ #
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"
43
+ class Ambience
44
+ class << self
45
+
46
+ # Returns a new Ambience config from a given YAML +config_file+ for an optional +env+.
47
+ # Overwrites properties with any JVM properties (in case the application is running on JRuby).
48
+ def new(config_file, env = nil)
49
+ config_hash = parse_config load_config(config_file)
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
54
+
55
+ # Returns whether the current Ruby platfrom is JRuby.
56
+ def jruby?
57
+ RUBY_PLATFORM =~ /java/
58
+ end
59
+
60
+ private
61
+
62
+ # Loads a given +config_file+. Raises an ArgumentError in case the config could not be found.
63
+ def load_config(config_file)
64
+ raise ArgumentError, "Missing config: #{config_file}" unless File.exist? config_file
65
+ File.read config_file
66
+ end
67
+
68
+ # Returns the ERB-interpreted content from a given YAML +config+ String and returns a Hash
69
+ # containing the evaluated content. Defaults to returning an empty Hash.
70
+ def parse_config(config)
71
+ YAML.load ERB.new(config).result || {}
72
+ end
73
+
74
+ # Returns a Hash containing any JVM properties.
75
+ def jvm_property_hash
76
+ jvm_properties.inject({}) do |hash, (key, value)|
77
+ hash.deep_merge hash_from_property(key, value)
78
+ end
79
+ end
80
+
81
+ # Returns a Hash generated from a JVM +property+ and its +value+.
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
90
+
91
+ # Returns the JVM properties.
92
+ def jvm_properties
93
+ jruby? ? JavaLang::System.get_properties : {}
94
+ end
95
+
96
+ end
97
+
98
+ if jruby?
99
+ module JavaLang
100
+ include_package "java.lang"
101
+ end
102
+ end
103
+
104
+ end
@@ -0,0 +1,19 @@
1
+ class Hash
2
+
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
12
+
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
18
+
19
+ end
@@ -0,0 +1,49 @@
1
+ require "spec_helper"
2
+
3
+ describe Ambience do
4
+ include SpecHelper
5
+
6
+ it "should return the content of a given config file as a Hashie::Mash" do
7
+ config = Ambience.new config_file(:basic)
8
+
9
+ config.should be_a(Hashie::Mash)
10
+ config.auth.username.should == "ferris"
11
+ config[:auth][:address].should == "http://example.com"
12
+ config["auth"]["password"].should == "test"
13
+ end
14
+
15
+ it "should return the config for an optional environment as a Hashie::Mash" do
16
+ config = Ambience.new config_file(:environments), :production
17
+
18
+ config.should be_a(Hashie::Mash)
19
+ config.auth.username.should == "ferris"
20
+ config[:auth]["password"].should == "topsecret"
21
+ end
22
+
23
+ it "should return the config specified as Symbols as a Hashie::Mash" do
24
+ config = Ambience.new config_file(:symbols), "production"
25
+
26
+ config.should be_a(Hashie::Mash)
27
+ config.auth.username.should == "ferris"
28
+ config[:auth]["password"].should == "test"
29
+ end
30
+
31
+ it "should merge the config with any JVM properties when using JRuby" do
32
+ Ambience.stubs(:jruby?).returns(true)
33
+ Ambience.stubs(:jvm_properties).returns(
34
+ "auth.address" => "http://live.example.com",
35
+ "auth.password" => "topsecret"
36
+ )
37
+ config = Ambience.new config_file(:basic)
38
+
39
+ config.should be_a(Hashie::Mash)
40
+ config.auth.username.should == "ferris"
41
+ config[:auth][:address].should == "http://live.example.com"
42
+ config["auth"]["password"].should == "topsecret"
43
+ end
44
+
45
+ it "should raise an ArgumentError in case the given config file could not be found" do
46
+ lambda { Ambience.new "missing_config.yml" }.should raise_error(ArgumentError)
47
+ end
48
+
49
+ end
@@ -0,0 +1,5 @@
1
+ auth:
2
+ address: http://example.com
3
+ username: ferris
4
+ password: test
5
+
@@ -0,0 +1,14 @@
1
+ development: &development
2
+ auth:
3
+ username: "ferris"
4
+ password: test
5
+
6
+ test:
7
+ <<: *development
8
+
9
+ production:
10
+ <<: *development
11
+ auth:
12
+ username: "ferris"
13
+ password: topsecret
14
+
@@ -0,0 +1,11 @@
1
+ :development: &development
2
+ :auth:
3
+ :username: "ferris"
4
+ :password: test
5
+
6
+ :test:
7
+ <<: *development
8
+
9
+ :production:
10
+ <<: *development
11
+
@@ -0,0 +1,16 @@
1
+ require "spec"
2
+ require "mocha"
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ambience
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Harrington
@@ -9,11 +9,40 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-13 00:00:00 +01:00
12
+ date: 2010-03-06 00:00:00 +01:00
13
13
  default_executable:
14
- dependencies: []
15
-
16
- description: JVM-Parameters for your JRuby app
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hashie
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.2.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.8
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: mocha
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.9.7
44
+ version:
45
+ description:
17
46
  email: me@rubiii.com
18
47
  executables: []
19
48
 
@@ -22,15 +51,17 @@ extensions: []
22
51
  extra_rdoc_files:
23
52
  - README.rdoc
24
53
  files:
25
- - README.rdoc
54
+ - CHANGELOG
26
55
  - Rakefile
27
- - VERSION
28
- - init.rb
56
+ - README.rdoc
57
+ - lib/ambience/ambience.rb
58
+ - lib/ambience/core_ext.rb
29
59
  - lib/ambience.rb
30
- - test/ambience_test.rb
31
- - test/fixtures/fixtures.rb
32
- - test/fixtures/some_config.yml
33
- - test/test_helper.rb
60
+ - spec/ambience/ambience_spec.rb
61
+ - spec/spec_helper.rb
62
+ - spec/fixtures/basic.yml
63
+ - spec/fixtures/environments.yml
64
+ - spec/fixtures/symbols.yml
34
65
  has_rdoc: true
35
66
  homepage: http://github.com/rubiii/ambience
36
67
  licenses: []
@@ -38,10 +69,10 @@ licenses: []
38
69
  post_install_message:
39
70
  rdoc_options:
40
71
  - --charset=UTF-8
41
- - --title
42
- - Ambience
43
72
  - --line-numbers
44
73
  - --inline-source
74
+ - --title
75
+ - Ambience - App configuration feat. YAML and JVM properties
45
76
  require_paths:
46
77
  - lib
47
78
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -62,8 +93,7 @@ rubyforge_project:
62
93
  rubygems_version: 1.3.5
63
94
  signing_key:
64
95
  specification_version: 3
65
- summary: JVM-Parameters for your JRuby app
96
+ summary: App configuration feat. YAML and JVM properties
66
97
  test_files:
67
- - test/ambience_test.rb
68
- - test/fixtures/fixtures.rb
69
- - test/test_helper.rb
98
+ - spec/ambience/ambience_spec.rb
99
+ - spec/spec_helper.rb
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.1.0
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require "ambience"
@@ -1,48 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "test_helper")
2
-
3
- class AmbienceTest < Test::Unit::TestCase
4
-
5
- include Fixtures
6
-
7
- context "Ambience using anything else than JRuby" do
8
- setup do
9
- Ambience.stubs(:jruby?).returns(false)
10
- end
11
-
12
- context "with config file set up" do
13
- should "return a Hash containing the config file content" do
14
- result = Ambience.new "test", some_config_file
15
- assert_equal some_config_hash, result
16
- end
17
- end
18
-
19
- context "with no config file available" do
20
- should "return an empty Hash" do
21
- result = Ambience.new "test", "nofile"
22
- assert_equal Hash.new, result
23
- end
24
- end
25
- end
26
-
27
- context "Ambience using JRuby" do
28
- setup do
29
- Ambience.stubs(:jruby?).returns(true)
30
- Ambience.stubs(:jvm_properties).returns(some_jvm_properties)
31
- end
32
-
33
- context "with config file set up" do
34
- should "return a Hash containing the config file and JVM properties" do
35
- result = Ambience.new "test", some_config_file
36
- assert_equal some_jvm_updated_config_hash, result
37
- end
38
- end
39
-
40
- context "with no config file available" do
41
- should "return a Hash containing the JVM properties" do
42
- result = Ambience.new "test", "nofile"
43
- assert_equal some_converted_jvm_properties, result
44
- end
45
- end
46
- end
47
-
48
- end
@@ -1,23 +0,0 @@
1
- module Fixtures
2
-
3
- def some_config_file
4
- File.join(File.dirname(__FILE__), "some_config.yml")
5
- end
6
-
7
- def some_config_hash
8
- { "auth" => { "user" => "test", "password" => "test" } }
9
- end
10
-
11
- def some_jvm_properties
12
- { "auth.user" => "jvm" }
13
- end
14
-
15
- def some_converted_jvm_properties
16
- { "auth" => { "user" => "jvm" } }
17
- end
18
-
19
- def some_jvm_updated_config_hash
20
- { "auth" => { "user" => "jvm", "password" => "test" } }
21
- end
22
-
23
- end
@@ -1,13 +0,0 @@
1
- defaults: &defaults
2
- auth:
3
- user: "test"
4
- password: "test"
5
-
6
- development:
7
- <<: *defaults
8
-
9
- test:
10
- <<: *defaults
11
-
12
- production:
13
- <<: *defaults
data/test/test_helper.rb DELETED
@@ -1,7 +0,0 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
- require "mocha"
4
- require "shoulda"
5
-
6
- require File.join(File.dirname(__FILE__), "fixtures", "fixtures")
7
- require File.join(File.dirname(__FILE__), "..", "lib", "ambience") unless defined? Ambience