-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added core_ext and moved impl into subfolder
- Loading branch information
Showing
3 changed files
with
115 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,2 @@ | ||
require "rubygems" | ||
require "yaml" unless defined? YAML | ||
require "erb" unless defined? ERB | ||
|
||
class Ambience | ||
class << self | ||
|
||
# Returns a new config Hash for a given Rails +environment+ from a given | ||
# +config_file+. Adds the current JVM properties to the config Hash in case | ||
# the application is running on JRuby. | ||
def new(environment = RAILS_ENV, config_file = nil) | ||
hash = setup_config environment, load_config_file(config_file) | ||
hash = setup_jvm hash | ||
end | ||
|
||
# Returns if the application's running on JRuby. | ||
def jruby? | ||
RUBY_PLATFORM =~ /java/ | ||
end | ||
|
||
private | ||
|
||
# Loads the given +config_file+. Tries to load "ambience.yml" from the | ||
# application's config folder in case no +config_file+ was given. Returns | ||
# the content from the config file or nil in case no config file was found. | ||
def load_config_file(config_file) | ||
config_file ||= File.join(RAILS_ROOT, "config", "ambience.yml") | ||
config = File.expand_path(config_file) | ||
file = File.read(config) if File.exist? config | ||
file ||= nil | ||
end | ||
|
||
# Returns the ERB-interpreted content at the given +env+ from a given Yaml | ||
# +config+ String and returns a Hash containing the evaluated content. | ||
# Defaults to returning an empty Hash in case +config+ is nil. | ||
def setup_config(env, config) | ||
hash = YAML.load(ERB.new(config).result)[env] unless config.nil? | ||
hash ||= {} | ||
end | ||
|
||
# Expects the current config +hash+, iterates through the JVM properties, | ||
# adds them to the given +hash+ and returns the merged result. | ||
def setup_jvm(hash) | ||
if jruby? | ||
jvm_properties.each do |key, value| | ||
param = hash_from_property key, value | ||
hash = deep_merge(hash, param) unless hash.nil? | ||
end | ||
end | ||
hash | ||
end | ||
|
||
# Merges a given +hash+ with a +target+ Hash and returns the merged result. | ||
def deep_merge(hash, target) | ||
target.keys.each do |key| | ||
if target[key].is_a? Hash and hash[key].is_a? Hash | ||
hash[key] = deep_merge(hash[key], target[key]) | ||
next | ||
end | ||
hash[key] = target[key] | ||
end | ||
hash | ||
end | ||
|
||
# Expects +key+ and +value+ from a JVM property and returns a Hash that | ||
# complies to the YAML format. | ||
def hash_from_property(key, value) | ||
hash, split = {}, key.split(".") | ||
(split.size-1).downto(0) do |i| | ||
v = i == (split.size-1) ? value : hash | ||
hash = { split[i] => v } | ||
end | ||
hash | ||
end | ||
|
||
# Returns the JVM properties. | ||
def jvm_properties | ||
JavaLang::System.get_properties | ||
end | ||
|
||
end | ||
end | ||
|
||
if Ambience.jruby? | ||
module JavaLang | ||
include_package "java.lang" | ||
end | ||
end | ||
require "ambience/core_ext" | ||
require "ambience/ambience" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
require "yaml" unless defined? YAML | ||
require "erb" unless defined? ERB | ||
|
||
# = Ambience | ||
# | ||
# JRuby on Rails app configuration feat. YAML and JVM properties. Lets you specify a default | ||
# configuration in a YAML file and overwrite its properties via JVM properties for production. | ||
class Ambience | ||
class << self | ||
|
||
# Returns a new config Hash for a given Rails app +environment+ from a given +config_file+. | ||
# Overwrites properties with any JVM properties (in case the application is running on JRuby). | ||
def new(config_file = nil, environment = nil) | ||
hash = parse_config load_config(config_file), environment | ||
hash = merge_jvm_properties_with hash | ||
end | ||
|
||
# Returns if the application's running on JRuby. | ||
def jruby? | ||
RUBY_PLATFORM =~ /java/ | ||
end | ||
|
||
private | ||
|
||
# Loads the given +config_file+. Tries to load "ambience.yml" from the | ||
# application's config folder in case no +config_file+ was given. Returns | ||
# the content from the config file or nil in case no config file was found. | ||
def load_config(config_file) | ||
config_file ||= File.join(RAILS_ROOT, "config", "ambience.yml") | ||
config = File.read config_file if File.exist? config_file | ||
config || nil | ||
end | ||
|
||
# Returns the ERB-interpreted content at the given +env+ from a given YAML | ||
# +config+ String and returns a Hash containing the evaluated content. | ||
# Defaults to returning an empty Hash in case +config+ is nil. | ||
def parse_config(config, environment) | ||
config_hash = YAML.load(ERB.new(config).result) | ||
config_hash = config_hash[environment] if environment | ||
config_hash || {} | ||
end | ||
|
||
# Expects the current config +hash+, iterates through the JVM properties, | ||
# adds them to the given +hash+ and returns the merged result. | ||
def merge_jvm_properties_with(hash) | ||
return hash unless jruby? | ||
|
||
jvm_properties.each do |key, value| | ||
param = hash_from_property key, value | ||
hash = deep_merge hash, param if hash | ||
end | ||
hash | ||
end | ||
|
||
# Expects +key+ and +value+ from a JVM property and returns a Hash that | ||
# complies to the YAML format. | ||
def hash_from_property(key, value) | ||
hash, split = {}, key.split(".") | ||
(split.size-1).downto(0) do |i| | ||
v = i == (split.size-1) ? value : hash | ||
hash = { split[i] => v } | ||
end | ||
hash | ||
end | ||
|
||
# Returns the JVM properties. | ||
def jvm_properties | ||
JavaLang::System.get_properties | ||
end | ||
|
||
end | ||
end | ||
|
||
if Ambience.jruby? | ||
module JavaLang | ||
include_package "java.lang" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class Hash | ||
|
||
# Returns a new Hash with all keys converted to Symbols. | ||
def recursively_symbolize_keys | ||
inject({}) do |hash, (key, value)| | ||
hash[key.to_sym] = case value | ||
when Hash then value.recursively_symbolize_keys | ||
else value | ||
end | ||
hash | ||
end | ||
end | ||
|
||
# Converts this Hash to a new Hash with all keys converted to Symbols. | ||
def recursively_symbolize_keys! | ||
replace recursively_symbolize_keys | ||
end | ||
|
||
# Returns a new hash with self and other_hash merged recursively. | ||
# Implementation from ActiveSupport. | ||
def deep_merge(other_hash) | ||
merge(other_hash) do |key, oldval, newval| | ||
oldval = oldval.to_hash if oldval.respond_to? :to_hash | ||
newval = newval.to_hash if newval.respond_to? :to_hash | ||
oldval.class.to_s == "Hash" && newval.class.to_s == "Hash" ? oldval.deep_merge(newval) : newval | ||
end | ||
end | ||
|
||
# Returns a new hash with self and other_hash merged recursively. Modifies the receiver in place. | ||
# Implementation from ActiveSupport. | ||
def deep_merge!(other_hash) | ||
replace deep_merge(other_hash) | ||
end | ||
|
||
end |