greased-rails 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +15 -2
- data/lib/greased/applicator.rb +9 -79
- data/lib/greased/loggable.rb +72 -0
- data/lib/greased/method_caller.rb +43 -0
- data/lib/greased/options.rb +106 -0
- data/lib/greased/rails/engine.rb +51 -32
- data/lib/greased/rails/version.rb +1 -1
- data/lib/greased/setting.rb +8 -12
- data/lib/greased/settings.rb +6 -11
- data/lib/greased-rails.rb +1 -4
- data/lib/greased.rb +40 -0
- data/tasks/greased.rake +15 -15
- data/templates/greased.yml +19 -0
- metadata +7 -2
data/README.md
CHANGED
@@ -20,12 +20,25 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
There are
|
23
|
+
There are four OPTIONAL configuration files you can create:
|
24
24
|
|
25
|
+
* greased.yml (options for applying settings to environment)
|
25
26
|
* greased_settings.yml (serialized application settings)
|
27
|
+
* greased_partial.yml (serialized application settings that override greased_settings.yml)
|
26
28
|
* greased_variables.yml (serialized ENV variables)
|
27
29
|
|
28
|
-
You can see what they look like in the "templates" folder.
|
30
|
+
All YAML files support ERB. You can see what they look like in the "templates" folder.
|
31
|
+
|
32
|
+
### Options - greased.yml
|
33
|
+
|
34
|
+
This is a YAML serialization of options defining environment and where files are located.
|
35
|
+
|
36
|
+
If you don't create your own file, Greased will use the file in the "templates" folder of this gem. To specify your own settings, save your file to one of the following locations:
|
37
|
+
|
38
|
+
* greased.yml (in the root of your Rails application)
|
39
|
+
* greased/greased.yml
|
40
|
+
* config/greased.yml
|
41
|
+
* config/greased/greased.yml
|
29
42
|
|
30
43
|
### Application Settings - greased_settings.yml
|
31
44
|
|
data/lib/greased/applicator.rb
CHANGED
@@ -5,68 +5,11 @@ require 'active_support/core_ext/hash/deep_merge'
|
|
5
5
|
module Greased
|
6
6
|
class Applicator
|
7
7
|
|
8
|
-
# settings applied to application environment
|
9
|
-
APP_SETTINGS_FILENAME_BASE = "settings.yml"
|
10
|
-
APP_SETTINGS_FILENAME = "greased_#{APP_SETTINGS_FILENAME_BASE}"
|
11
|
-
# additional settings applied to application environment
|
12
|
-
# after default Greased (template) settings are applied
|
13
|
-
# or custom settings file is applied.
|
14
|
-
APP_PATCH_FILENAME_BASE = "partial.yml"
|
15
|
-
APP_PATCH_FILENAME = "greased_#{APP_PATCH_FILENAME_BASE}"
|
16
|
-
# environment variables to load into ENV
|
17
|
-
ENV_VARS_FILENAME_BASE = "variables.yml"
|
18
|
-
ENV_VARS_FILENAME = "greased_#{ENV_VARS_FILENAME_BASE}"
|
19
|
-
# default Greased template with application settings
|
20
|
-
DEFAULT_SETTINGS_FILE = Pathname.new(File.join(File.dirname(__FILE__), '..', '..', 'templates', APP_SETTINGS_FILENAME)).realpath
|
21
|
-
DEFAULT_ENV = "development"
|
22
|
-
|
23
|
-
def self.default_options
|
24
|
-
{
|
25
|
-
:app => nil,
|
26
|
-
:env => ENV['RAILS_ENV'] || ENV['RACK_ENV'] || DEFAULT_ENV,
|
27
|
-
:groups => ["application"],
|
28
|
-
:app_filename => DEFAULT_SETTINGS_FILE,
|
29
|
-
#:env_filename => [],
|
30
|
-
:skip_erb => false
|
31
|
-
}
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.rails_options
|
35
|
-
if defined? ::Rails
|
36
|
-
default_options.merge(
|
37
|
-
:app => ::Rails.application,
|
38
|
-
#:env => ::Rails.env || DEFAULT_ENV,
|
39
|
-
#:groups => ["application"],
|
40
|
-
:app_filename => [
|
41
|
-
File.join(::Rails.root, APP_SETTINGS_FILENAME),
|
42
|
-
File.join(::Rails.root, "greased", APP_SETTINGS_FILENAME_BASE),
|
43
|
-
File.join(::Rails.root, "config", APP_SETTINGS_FILENAME),
|
44
|
-
File.join(::Rails.root, "config", "greased", APP_SETTINGS_FILENAME_BASE),
|
45
|
-
DEFAULT_SETTINGS_FILE
|
46
|
-
],
|
47
|
-
:partial_filename => [
|
48
|
-
File.join(::Rails.root, APP_PATCH_FILENAME),
|
49
|
-
File.join(::Rails.root, "greased", APP_PATCH_FILENAME_BASE),
|
50
|
-
File.join(::Rails.root, "config", APP_PATCH_FILENAME),
|
51
|
-
File.join(::Rails.root, "config", "greased", APP_PATCH_FILENAME_BASE)
|
52
|
-
],
|
53
|
-
:env_filename => [
|
54
|
-
File.join(::Rails.root, ENV_VARS_FILENAME),
|
55
|
-
File.join(::Rails.root, "greased", ENV_VARS_FILENAME_BASE),
|
56
|
-
File.join(::Rails.root, "config", ENV_VARS_FILENAME),
|
57
|
-
File.join(::Rails.root, "config", "greased", ENV_VARS_FILENAME_BASE)
|
58
|
-
]
|
59
|
-
)
|
60
|
-
else
|
61
|
-
default_options
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
8
|
attr_accessor :app, :env, :groups
|
66
9
|
|
67
|
-
def initialize(options)
|
68
|
-
@options =
|
69
|
-
@app =
|
10
|
+
def initialize(application, options = {})
|
11
|
+
@options = Options.defaults.merge(options)
|
12
|
+
@app = application#@options[:app]
|
70
13
|
@env = @options[:env]
|
71
14
|
@groups = @options[:groups]
|
72
15
|
end
|
@@ -82,16 +25,12 @@ module Greased
|
|
82
25
|
self
|
83
26
|
end
|
84
27
|
|
85
|
-
def puts_env(options = {})
|
86
|
-
puts(*list_env(options))
|
87
|
-
end
|
88
|
-
|
89
28
|
def list_env(options = {})
|
90
29
|
variables(options).collect{|key, value| "#{key}=#{value}"}
|
91
30
|
end
|
92
31
|
|
93
|
-
def save_env_file(environment, filename = nil)
|
94
|
-
filename ||=
|
32
|
+
def save_env_file(path, environment, filename = nil)
|
33
|
+
filename ||= Greased.file_path(path, "#{environment}.env")
|
95
34
|
|
96
35
|
File.open(filename, 'w') do |file|
|
97
36
|
list_env(:env => environment).each{|line| file.puts line }
|
@@ -123,21 +62,12 @@ module Greased
|
|
123
62
|
protected
|
124
63
|
|
125
64
|
def load_settings(filename, options = {})
|
126
|
-
|
127
|
-
|
65
|
+
all = Greased.load_yaml(filename, options)
|
66
|
+
defaults = all.delete('defaults') || {}
|
128
67
|
|
129
|
-
|
130
|
-
|
131
|
-
source = ERB.new(source).result unless options[:skip_erb]
|
132
|
-
all = YAML.load(source)
|
133
|
-
defaults = all.delete('defaults') || {}
|
134
|
-
|
135
|
-
all.each do |environment, settings|
|
136
|
-
all[environment] = defaults.deep_merge(settings)
|
137
|
-
end
|
68
|
+
all.each do |environment, settings|
|
69
|
+
all[environment] = defaults.deep_merge(settings)
|
138
70
|
end
|
139
|
-
|
140
|
-
all || {}
|
141
71
|
end
|
142
72
|
|
143
73
|
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
#
|
2
|
+
# Taken from Mongoid 4/2/2013
|
3
|
+
# https://github.com/mongoid/mongoid/blob/master/lib/mongoid/loggable.rb
|
4
|
+
#
|
5
|
+
|
6
|
+
module Greased
|
7
|
+
# Contains logging behaviour.
|
8
|
+
module Loggable
|
9
|
+
|
10
|
+
# Get the logger.
|
11
|
+
#
|
12
|
+
# @note Will try to grab Rails' logger first before creating a new logger
|
13
|
+
# with stdout.
|
14
|
+
#
|
15
|
+
# @example Get the logger.
|
16
|
+
# Loggable.logger
|
17
|
+
#
|
18
|
+
# @return [ Logger ] The logger.
|
19
|
+
#
|
20
|
+
# @since 3.0.0
|
21
|
+
def logger
|
22
|
+
return @logger if defined?(@logger)
|
23
|
+
@logger = rails_logger || default_logger
|
24
|
+
end
|
25
|
+
|
26
|
+
# Set the logger.
|
27
|
+
#
|
28
|
+
# @example Set the logger.
|
29
|
+
# Loggable.logger = Logger.new($stdout)
|
30
|
+
#
|
31
|
+
# @param [ Logger ] The logger to set.
|
32
|
+
#
|
33
|
+
# @return [ Logger ] The new logger.
|
34
|
+
#
|
35
|
+
# @since 3.0.0
|
36
|
+
def logger=(logger)
|
37
|
+
@logger = logger
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
# Gets the default Mongoid logger - stdout.
|
43
|
+
#
|
44
|
+
# @api private
|
45
|
+
#
|
46
|
+
# @example Get the default logger.
|
47
|
+
# Loggable.default_logger
|
48
|
+
#
|
49
|
+
# @return [ Logger ] The default logger.
|
50
|
+
#
|
51
|
+
# @since 3.0.0
|
52
|
+
def default_logger
|
53
|
+
logger = Logger.new($stdout)
|
54
|
+
logger.level = Logger::INFO
|
55
|
+
logger
|
56
|
+
end
|
57
|
+
|
58
|
+
# Get the Rails logger if it's defined.
|
59
|
+
#
|
60
|
+
# @api private
|
61
|
+
#
|
62
|
+
# @example Get Rails' logger.
|
63
|
+
# Loggable.rails_logger
|
64
|
+
#
|
65
|
+
# @return [ Logger ] The Rails logger.
|
66
|
+
#
|
67
|
+
# @since 3.0.0
|
68
|
+
def rails_logger
|
69
|
+
defined?(::Rails) && ::Rails.respond_to?(:logger) && ::Rails.logger
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Greased
|
2
|
+
class MethodCaller
|
3
|
+
|
4
|
+
def initialize(operator = nil, options = {})
|
5
|
+
@operator = operator
|
6
|
+
@evaluations = []
|
7
|
+
|
8
|
+
if @operator.nil?
|
9
|
+
@evaluations << :default
|
10
|
+
elsif @operator == "="
|
11
|
+
@evaluations << :assign
|
12
|
+
else
|
13
|
+
@evaluations << :evaluate
|
14
|
+
end
|
15
|
+
|
16
|
+
if options[:reassign]
|
17
|
+
@evaluations << :assign
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def call(target, name, value)
|
22
|
+
@evaluations.inject(value) do |result, evaluator|
|
23
|
+
send(evaluator, target, name, result)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
|
29
|
+
def evaluate(target, name, value)
|
30
|
+
target.send(name.to_sym).send(@operator.to_sym, value)
|
31
|
+
end
|
32
|
+
|
33
|
+
def assign(target, name, value)
|
34
|
+
target.send(:"#{name}=", value)
|
35
|
+
end
|
36
|
+
|
37
|
+
def default(target, name, value)
|
38
|
+
value = [value] unless value.is_a? Array
|
39
|
+
target.send(name.to_sym, *value)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
require 'pathname'
|
3
|
+
require 'active_support/core_ext/hash/deep_merge'
|
4
|
+
require 'active_support/core_ext/hash/keys'
|
5
|
+
|
6
|
+
module Greased
|
7
|
+
class Options
|
8
|
+
|
9
|
+
# settings applied to application environment
|
10
|
+
APP_SETTINGS_FILENAME_BASE = "settings.yml"
|
11
|
+
APP_SETTINGS_FILENAME = "greased_#{APP_SETTINGS_FILENAME_BASE}"
|
12
|
+
# additional settings applied to application environment
|
13
|
+
# after default Greased (template) settings are applied
|
14
|
+
# or custom settings file is applied.
|
15
|
+
APP_PATCH_FILENAME_BASE = "partial.yml"
|
16
|
+
APP_PATCH_FILENAME = "greased_#{APP_PATCH_FILENAME_BASE}"
|
17
|
+
# environment variables to load into ENV
|
18
|
+
ENV_VARS_FILENAME_BASE = "variables.yml"
|
19
|
+
ENV_VARS_FILENAME = "greased_#{ENV_VARS_FILENAME_BASE}"
|
20
|
+
# options to load from YAML
|
21
|
+
OPTIONS_FILENAME = "greased.yml"
|
22
|
+
DEFAULT_OPTIONS_FILE = Greased.file_path(File.dirname(__FILE__), '..', '..', 'templates', OPTIONS_FILENAME)
|
23
|
+
# default Greased template with application settings
|
24
|
+
DEFAULT_SETTINGS_FILE = Greased.file_path(File.dirname(__FILE__), '..', '..', 'templates', APP_SETTINGS_FILENAME)
|
25
|
+
DEFAULT_ENV = "development"
|
26
|
+
|
27
|
+
class << self
|
28
|
+
|
29
|
+
def defaults
|
30
|
+
load_options(DEFAULT_OPTIONS_FILE).deep_merge!(:app_filename => DEFAULT_SETTINGS_FILE)
|
31
|
+
end
|
32
|
+
|
33
|
+
def find(path)
|
34
|
+
#Greased.logger.debug "Find application options in: #{path}"
|
35
|
+
|
36
|
+
options = defaults
|
37
|
+
|
38
|
+
if Dir.exists? path
|
39
|
+
# load rails defaults
|
40
|
+
options = merge_options options, {
|
41
|
+
#:env => ::Rails.env || DEFAULT_ENV,
|
42
|
+
#:groups => ["application"],
|
43
|
+
:app_filename => [
|
44
|
+
Greased.file_path(path, APP_SETTINGS_FILENAME),
|
45
|
+
Greased.file_path(path, "greased", APP_SETTINGS_FILENAME_BASE),
|
46
|
+
Greased.file_path(path, "config", APP_SETTINGS_FILENAME),
|
47
|
+
Greased.file_path(path, "config", "greased", APP_SETTINGS_FILENAME_BASE),
|
48
|
+
DEFAULT_SETTINGS_FILE
|
49
|
+
],
|
50
|
+
:partial_filename => [
|
51
|
+
Greased.file_path(path, APP_PATCH_FILENAME),
|
52
|
+
Greased.file_path(path, "greased", APP_PATCH_FILENAME_BASE),
|
53
|
+
Greased.file_path(path, "config", APP_PATCH_FILENAME),
|
54
|
+
Greased.file_path(path, "config", "greased", APP_PATCH_FILENAME_BASE)
|
55
|
+
],
|
56
|
+
:env_filename => [
|
57
|
+
Greased.file_path(path, ENV_VARS_FILENAME),
|
58
|
+
Greased.file_path(path, "greased", ENV_VARS_FILENAME_BASE),
|
59
|
+
Greased.file_path(path, "config", ENV_VARS_FILENAME),
|
60
|
+
Greased.file_path(path, "config", "greased", ENV_VARS_FILENAME_BASE)
|
61
|
+
]
|
62
|
+
},
|
63
|
+
# load custom options
|
64
|
+
load_options([
|
65
|
+
Greased.file_path(path, OPTIONS_FILENAME),
|
66
|
+
Greased.file_path(path, "greased", OPTIONS_FILENAME),
|
67
|
+
Greased.file_path(path, "config", OPTIONS_FILENAME),
|
68
|
+
Greased.file_path(path, "config", "greased", OPTIONS_FILENAME)
|
69
|
+
])
|
70
|
+
end
|
71
|
+
|
72
|
+
options
|
73
|
+
end
|
74
|
+
|
75
|
+
protected
|
76
|
+
|
77
|
+
def load_options(filename)
|
78
|
+
#Greased.logger.debug "Loading options from: #{filename}"
|
79
|
+
|
80
|
+
merge_options Greased.load_yaml(filename)
|
81
|
+
end
|
82
|
+
|
83
|
+
def merge_options(*options)
|
84
|
+
options.inject({}) do |all, one|
|
85
|
+
all.deep_merge deep_symbolize_keys(one)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def deep_symbolize_keys(object)
|
92
|
+
case object
|
93
|
+
when Hash
|
94
|
+
object.each_with_object({}) do |(key, value), result|
|
95
|
+
result[key.to_sym] = deep_symbolize_keys(value)
|
96
|
+
end
|
97
|
+
when Array
|
98
|
+
object.map {|e| deep_symbolize_keys(e) }
|
99
|
+
else
|
100
|
+
object
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
data/lib/greased/rails/engine.rb
CHANGED
@@ -5,6 +5,8 @@ module Greased
|
|
5
5
|
module Rails
|
6
6
|
class Engine < ::Rails::Engine
|
7
7
|
|
8
|
+
Greased.logger.level = Logger::DEBUG if ::Rails.env.development?
|
9
|
+
|
8
10
|
rake_tasks do
|
9
11
|
path = Pathname.new(File.join(File.dirname(__FILE__), '../../../tasks/')).realpath
|
10
12
|
|
@@ -24,8 +26,8 @@ module Greased
|
|
24
26
|
#:load_environment_hook
|
25
27
|
config.before_configuration do |application|
|
26
28
|
|
27
|
-
options =
|
28
|
-
applicator = Applicator.new(options)
|
29
|
+
options = Options.find(::Rails.root)
|
30
|
+
applicator = Applicator.new(application, options)
|
29
31
|
variables = applicator.variables.except("RACK_ENV", "RAILS_ENV")
|
30
32
|
|
31
33
|
variables.each do |key, value|
|
@@ -33,34 +35,51 @@ module Greased
|
|
33
35
|
end
|
34
36
|
|
35
37
|
if ::Rails.env.development?
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
38
|
+
|
39
|
+
Greased.logger.debug " "
|
40
|
+
Greased.logger.debug "## GREASED [#{applicator.env.upcase}] #{'#' * (55 - applicator.env.size)}"
|
41
|
+
Greased.logger.debug "# #"
|
42
|
+
Greased.logger.debug "# ... loaded options ... #"
|
43
|
+
Greased.logger.debug "# #"
|
44
|
+
Greased.logger.debug "#####################################################################"
|
45
|
+
Greased.logger.debug " "
|
46
|
+
|
47
|
+
pp options
|
48
|
+
|
49
|
+
Greased.logger.debug " "
|
50
|
+
Greased.logger.debug "#####################################################################"
|
51
|
+
Greased.logger.debug " "
|
52
|
+
|
53
|
+
##########################
|
54
|
+
|
55
|
+
Greased.logger.debug " "
|
56
|
+
Greased.logger.debug "## GREASED [#{applicator.env.upcase}] #{'#' * (55 - applicator.env.size)}"
|
57
|
+
Greased.logger.debug "# #"
|
58
|
+
Greased.logger.debug "# ... loading environment variables ... #"
|
59
|
+
Greased.logger.debug "# #"
|
60
|
+
Greased.logger.debug "#####################################################################"
|
61
|
+
Greased.logger.debug " "
|
43
62
|
|
44
63
|
applicator.list_env.map.collect do |var|
|
45
|
-
|
64
|
+
Greased.logger.debug " #{var}"
|
46
65
|
end
|
47
66
|
|
48
|
-
|
49
|
-
|
50
|
-
|
67
|
+
Greased.logger.debug " "
|
68
|
+
Greased.logger.debug "#####################################################################"
|
69
|
+
Greased.logger.debug " "
|
51
70
|
end
|
52
71
|
|
53
72
|
end
|
54
73
|
|
55
|
-
# config.before_configuration {|app|
|
74
|
+
# config.before_configuration {|app| Greased.logger.debug "BEFORE CONFIGURATION"}
|
56
75
|
#
|
57
|
-
# config.before_initialize {|app|
|
76
|
+
# config.before_initialize {|app| Greased.logger.debug "BEFORE INITIALIZE"}
|
58
77
|
#
|
59
|
-
# config.to_prepare {|app|
|
78
|
+
# config.to_prepare {|app| Greased.logger.debug "TO PREPARE"}
|
60
79
|
#
|
61
|
-
# config.before_eager_load {|app|
|
80
|
+
# config.before_eager_load {|app| Greased.logger.debug "BEFORE EAGER LOAD"}
|
62
81
|
#
|
63
|
-
# config.after_initialize {|app|
|
82
|
+
# config.after_initialize {|app| Greased.logger.debug "AFTER INITIALIZE"}
|
64
83
|
#
|
65
84
|
# hooks = [
|
66
85
|
# :load_environment_hook, :load_active_support, :preload_frameworks, :initialize_logger, :initialize_cache, :set_clear_dependencies_hook,
|
@@ -71,34 +90,34 @@ module Greased
|
|
71
90
|
# ]
|
72
91
|
#
|
73
92
|
# hooks.each do |hook_name|
|
74
|
-
# initializer("greased.before.#{hook_name}", :before => hook_name, :group => :all) {|a|
|
93
|
+
# initializer("greased.before.#{hook_name}", :before => hook_name, :group => :all) {|a| Greased.logger.debug "BEFORE #{hook_name}".upcase}
|
75
94
|
# end
|
76
95
|
|
77
96
|
# RUNS BEFORE ENVIRONMENT CONFIGS ARE LOADED!
|
78
97
|
#app.config.before_initialize
|
79
98
|
config.before_configuration do |application|
|
80
99
|
|
81
|
-
options =
|
82
|
-
applicator = Applicator.new(options)
|
100
|
+
options = Options.find(::Rails.root)
|
101
|
+
applicator = Applicator.new(application, options)
|
83
102
|
|
84
103
|
applicator.settings.apply!
|
85
104
|
|
86
105
|
if ::Rails.env.development?
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
106
|
+
Greased.logger.debug " "
|
107
|
+
Greased.logger.debug "## GREASED [#{applicator.env.upcase}] #{'#' * (55 - applicator.env.size)}"
|
108
|
+
Greased.logger.debug "# #"
|
109
|
+
Greased.logger.debug "# ... loading application settings ... #"
|
110
|
+
Greased.logger.debug "# #"
|
111
|
+
Greased.logger.debug "#####################################################################"
|
112
|
+
Greased.logger.debug " "
|
94
113
|
|
95
114
|
applicator.settings.list.map(&:strip).map{|setting| setting.split("\n")}.flatten.each do |line|
|
96
|
-
|
115
|
+
Greased.logger.debug " #{line}"
|
97
116
|
end
|
98
117
|
|
99
|
-
|
100
|
-
|
101
|
-
|
118
|
+
Greased.logger.debug " "
|
119
|
+
Greased.logger.debug "#####################################################################"
|
120
|
+
Greased.logger.debug " "
|
102
121
|
end
|
103
122
|
|
104
123
|
end
|
data/lib/greased/setting.rb
CHANGED
@@ -1,18 +1,15 @@
|
|
1
1
|
|
2
2
|
module Greased
|
3
3
|
class Setting
|
4
|
-
|
5
4
|
DEFAULT_OPERATOR = "="
|
6
5
|
OPERATIONS = {
|
7
|
-
"<<" =>
|
8
|
-
"+=" =>
|
9
|
-
"-=" =>
|
10
|
-
"*=" =>
|
11
|
-
"=" =>
|
12
|
-
"
|
13
|
-
|
14
|
-
OPERATION_VALIDATORS = {
|
15
|
-
"send" => lambda{|target, name, value| raise "Value for the 'send' operator must be an array of method parameters" unless value.is_a? Array }
|
6
|
+
"<<" => MethodCaller.new("<<"),
|
7
|
+
"+=" => MethodCaller.new("+", :reassign => true),
|
8
|
+
"-=" => MethodCaller.new("-", :reassign => true),
|
9
|
+
"*=" => MethodCaller.new("*", :reassign => true),
|
10
|
+
"=" => MethodCaller.new("="),
|
11
|
+
"call" => MethodCaller.new,
|
12
|
+
"send" => MethodCaller.new
|
16
13
|
}
|
17
14
|
|
18
15
|
attr_reader :name, :value, :operator, :app, :env
|
@@ -33,8 +30,7 @@ module Greased
|
|
33
30
|
end
|
34
31
|
|
35
32
|
def apply!
|
36
|
-
raise "Unknown operator (#{operator}) for setting option: #{name}"
|
37
|
-
OPERATION_VALIDATORS[operator].call(@target, name, value) if OPERATION_VALIDATORS.keys.include? operator
|
33
|
+
raise "Unknown operator (#{operator}) for setting option: #{name}" unless OPERATIONS.keys.include? operator
|
38
34
|
OPERATIONS[operator].call(@target, name, value)
|
39
35
|
|
40
36
|
self
|
data/lib/greased/settings.rb
CHANGED
@@ -12,21 +12,16 @@ module Greased
|
|
12
12
|
refresh_settings!
|
13
13
|
end
|
14
14
|
|
15
|
-
def serialize
|
16
|
-
@settings.collect(&:serialize).join("\n")
|
17
|
-
end
|
18
|
-
|
19
15
|
def apply!
|
20
|
-
|
21
|
-
self
|
16
|
+
each(&:apply!)
|
22
17
|
end
|
23
18
|
|
24
|
-
def
|
25
|
-
|
19
|
+
def list
|
20
|
+
@settings.map(&:serialize)
|
26
21
|
end
|
27
22
|
|
28
|
-
def
|
29
|
-
|
23
|
+
def serialize
|
24
|
+
list.join("\n")
|
30
25
|
end
|
31
26
|
|
32
27
|
def each(&block)
|
@@ -37,7 +32,7 @@ module Greased
|
|
37
32
|
protected
|
38
33
|
|
39
34
|
def refresh_settings!
|
40
|
-
@settings = @config.
|
35
|
+
@settings = @config.map{|key, value| Setting.from_config(@application, key, value, @environment)}.flatten
|
41
36
|
end
|
42
37
|
end
|
43
38
|
end
|
data/lib/greased-rails.rb
CHANGED
data/lib/greased.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
require_relative "greased/loggable"
|
3
|
+
|
4
|
+
module Greased
|
5
|
+
extend Loggable
|
6
|
+
|
7
|
+
def self.load_yaml(filename, options = {})
|
8
|
+
# allow multiple filenames and use the first one that exists
|
9
|
+
filename = Array.wrap(filename).find do |path|
|
10
|
+
File.exists?(path).tap do |found|
|
11
|
+
#logger.debug "#{self.class}.#{__method__} exists [#{found}]: #{path}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
unless filename.nil?
|
16
|
+
source = File.read(filename)
|
17
|
+
source = ERB.new(source).result unless options[:skip_erb]
|
18
|
+
all = YAML.load(source)
|
19
|
+
end
|
20
|
+
|
21
|
+
all || {}
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.file_path(*args)
|
25
|
+
path = File.expand_path File.join(*args)
|
26
|
+
path = File.realpath(path) if File.exists?(path)
|
27
|
+
|
28
|
+
#logger.debug "#{self.class}.#{__method__} path: #{path}"
|
29
|
+
|
30
|
+
path
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
require_relative "greased/applicator"
|
35
|
+
require_relative "greased/options"
|
36
|
+
require_relative "greased/method_caller"
|
37
|
+
require_relative "greased/setting"
|
38
|
+
require_relative "greased/settings"
|
39
|
+
|
40
|
+
require_relative "greased/rails" if defined? ::Rails
|
data/tasks/greased.rake
CHANGED
@@ -3,30 +3,30 @@ require 'greased-rails'
|
|
3
3
|
namespace :greased do
|
4
4
|
namespace :env do
|
5
5
|
task :dump => :environment do
|
6
|
-
options = Greased::
|
7
|
-
|
6
|
+
options = Greased::Options.find(Rails.root)
|
7
|
+
applicator = Greased::Applicator.new(Rails.application, options)
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
9
|
+
Greased.logger.debug ""
|
10
|
+
Greased.logger.debug "## GREASED [#{applicator.env.upcase}] #{'#' * (55 - applicator.env.size)}"
|
11
|
+
Greased.logger.debug "# #"
|
12
|
+
Greased.logger.debug "# ... dumping variables to *.env files ... #"
|
13
|
+
Greased.logger.debug "# #"
|
14
|
+
Greased.logger.debug "#####################################################################"
|
15
|
+
Greased.logger.debug ""
|
16
16
|
|
17
17
|
environments = ["development", "staging", "production"]
|
18
18
|
longest = environments.map(&:size).max
|
19
19
|
|
20
20
|
environments.each do |env|
|
21
|
-
path =
|
22
|
-
filename =
|
21
|
+
path = applicator.save_env_file(Rails.root, env)
|
22
|
+
filename = File.basename(path)
|
23
23
|
|
24
|
-
|
24
|
+
Greased.logger.debug " [#{env}]#{' ' * (longest - env.size)} filename: #{path}"
|
25
25
|
end
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
Greased.logger.debug ""
|
28
|
+
Greased.logger.debug "#####################################################################"
|
29
|
+
Greased.logger.debug ""
|
30
30
|
|
31
31
|
end
|
32
32
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
|
2
|
+
# Where to look for the environment settings file(s).
|
3
|
+
# By default, Greased will try several file path permuations to find the file dynamically.
|
4
|
+
# app_filename: <%= File.join(Rails.root, 'greased_settings.yml') %>
|
5
|
+
|
6
|
+
# Where to look for the environment settings override file(s).
|
7
|
+
# By default, Greased will try several file path permuations to find the file dynamically.
|
8
|
+
# partial_filename: <%= File.join(Rails.root, 'greased_partial.yml') %>
|
9
|
+
|
10
|
+
# Where to look for the environment variable file(s).
|
11
|
+
# By default, Greased will try several file path permuations to find the file dynamically.
|
12
|
+
# env_filename: <%= File.join(Rails.root, 'greased_variables.yml') %>
|
13
|
+
|
14
|
+
# Envirionment (e.g. development, staging, production)
|
15
|
+
env: <%= ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development" %>
|
16
|
+
|
17
|
+
# Groups within the environment variables to load up.
|
18
|
+
groups: ["application"]
|
19
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: greased-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -58,8 +58,12 @@ files:
|
|
58
58
|
- Rakefile
|
59
59
|
- greased-rails.gemspec
|
60
60
|
- lib/greased-rails.rb
|
61
|
+
- lib/greased.rb
|
61
62
|
- lib/greased/app_env.rb
|
62
63
|
- lib/greased/applicator.rb
|
64
|
+
- lib/greased/loggable.rb
|
65
|
+
- lib/greased/method_caller.rb
|
66
|
+
- lib/greased/options.rb
|
63
67
|
- lib/greased/rails.rb
|
64
68
|
- lib/greased/rails/engine.rb
|
65
69
|
- lib/greased/rails/version.rb
|
@@ -67,6 +71,7 @@ files:
|
|
67
71
|
- lib/greased/settings.rb
|
68
72
|
- release.sh
|
69
73
|
- tasks/greased.rake
|
74
|
+
- templates/greased.yml
|
70
75
|
- templates/greased_partial.yml
|
71
76
|
- templates/greased_settings.yml
|
72
77
|
- templates/greased_variables.yml
|