ambience 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,78 @@
1
+ = Ambience
2
+
3
+ Ruby on Rails configuration with JVM Parameter spicing.
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:
11
+
12
+ $ gem install ambience
13
+
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
30
+
31
+ test:
32
+ <<: *defaults
33
+
34
+ production:
35
+ <<: *defaults
36
+
37
+ == Loading the configuration
38
+
39
+ Place this at the bottom of your config/environment.rb file:
40
+
41
+ AppConfig = Ambience.new
42
+
43
+ == Running your application on MRI
44
+
45
+ The configuration Hash equals the default configuration file:
46
+
47
+ "auth" => {
48
+ "user" => "client",
49
+ "password => "secret",
50
+ "service" => "http://test.example.com"
51
+ }
52
+
53
+ == Running the application on JRuby
54
+
55
+ Expecting these default JVM properties:
56
+
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"
60
+
61
+ And the following custom property:
62
+
63
+ auth.service = "http://live.example.com"
64
+
65
+ The default configuration will be extended by JVM properties:
66
+
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 ADDED
@@ -0,0 +1,42 @@
1
+ require "rubygems"
2
+ require "rake"
3
+ require "rake/testtask"
4
+ require "rake/rdoctask"
5
+
6
+ task :default => :test
7
+
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
13
+ end
14
+
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"]
20
+ end
21
+
22
+ 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
31
+
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
+ ]
39
+ end
40
+ rescue LoadError
41
+ puts "Jeweler missing. Install with: gem install jeweler"
42
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "ambience"
data/lib/ambience.rb ADDED
@@ -0,0 +1,88 @@
1
+ require "rubygems"
2
+ require "yaml" unless defined? YAML
3
+ require "erb" unless defined? ERB
4
+
5
+ class Ambience
6
+ class << self
7
+
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
@@ -0,0 +1,48 @@
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
@@ -0,0 +1,23 @@
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
@@ -0,0 +1,13 @@
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
@@ -0,0 +1,7 @@
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
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ambience
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Harrington
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-13 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: JVM-Parameters for your JRuby app
17
+ email: me@rubiii.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - README.rdoc
26
+ - Rakefile
27
+ - VERSION
28
+ - init.rb
29
+ - lib/ambience.rb
30
+ - test/ambience_test.rb
31
+ - test/fixtures/fixtures.rb
32
+ - test/fixtures/some_config.yml
33
+ - test/test_helper.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/rubiii/ambience
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --charset=UTF-8
41
+ - --title
42
+ - Ambience
43
+ - --line-numbers
44
+ - --inline-source
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.5
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: JVM-Parameters for your JRuby app
66
+ test_files:
67
+ - test/ambience_test.rb
68
+ - test/fixtures/fixtures.rb
69
+ - test/test_helper.rb