ruby-conf 2.4.0 → 2.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -6
- data/VERSION +1 -1
- data/lib/ruby-conf.rb +71 -34
- data/ruby-conf.gemspec +4 -1
- data/spec/lib/a_not_conf.rb +3 -0
- data/spec/lib/ruby-conf_spec.rb +60 -14
- data/spec/lib/test_conf.rb.tmpl +12 -0
- data/spec/lib/z_not_loaded.rb +5 -0
- data/spec/spec_helper.rb +1 -0
- metadata +5 -2
data/Gemfile
CHANGED
@@ -1,11 +1,6 @@
|
|
1
1
|
source "http://rubygems.org"
|
2
|
-
# Add dependencies required to use your gem here.
|
3
|
-
# Example:
|
4
|
-
# gem "activesupport", ">= 2.3.5"
|
5
2
|
|
6
|
-
|
7
|
-
# Include everything needed to run rake, tests, features, etc.
|
8
|
-
group :development do
|
3
|
+
group :development, :test do
|
9
4
|
gem "rspec"
|
10
5
|
gem "rr"
|
11
6
|
gem "yard"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.5.0
|
data/lib/ruby-conf.rb
CHANGED
@@ -2,15 +2,47 @@
|
|
2
2
|
#
|
3
3
|
# @author Hollin Wilkins & Curtis Schofield & Mason Bo-bay-son
|
4
4
|
|
5
|
-
|
5
|
+
require 'find'
|
6
|
+
require 'digest/md5'
|
7
|
+
|
8
|
+
module RubyConf
|
9
|
+
|
10
|
+
class Loader < BasicObject
|
11
|
+
@@conf = @@path = @@mtime = @@md5 = nil
|
12
|
+
|
13
|
+
class << self
|
14
|
+
def nil?() @@conf.nil? end
|
15
|
+
def __rc_loaded_conf() { path:@@path, mtime:@@mtime, md5:@@md5 } end
|
16
|
+
def __rc_conf() @@conf end
|
17
|
+
def __rc_set_conf(conf = nil) @@conf, @@path, @@mtime, @@md5 = conf, nil, nil, nil end
|
18
|
+
def __rc_load(path)
|
19
|
+
__rc_set_conf
|
20
|
+
@@path, @@mtime, @@md5 = path, File.mtime(path).to_i, Digest::MD5.hexdigest(File.read(path)) if load(path) && @@conf
|
21
|
+
end
|
22
|
+
def method_missing(name, *args, &block)
|
23
|
+
if @@mtime && @@mtime != File.mtime(@@path).to_i && @@md5 != Digest::MD5.hexdigest(File.read(@@path))
|
24
|
+
__rc_load(@@path)
|
25
|
+
end
|
6
26
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
27
|
+
if @@conf.nil?
|
28
|
+
Find.find('.') do |path|
|
29
|
+
next unless @@conf.nil? && path =~ /\.(?:rb|config|conf)$/
|
30
|
+
if path =~ /\.ruby-conf$/ || File.read(path) =~ /^\s*\#\s*\:\s*ruby-conf\s*$/mi
|
31
|
+
break if __rc_load(path)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
@@conf.__send__(name, *args, &block)
|
36
|
+
end
|
37
|
+
def to_s() @@conf.to_s end
|
38
|
+
def inspect() @@conf.inspect end
|
39
|
+
end
|
40
|
+
end
|
11
41
|
|
12
|
-
|
13
|
-
|
42
|
+
module Magic
|
43
|
+
attr_accessor :__rc_chain
|
44
|
+
def __rc_gather() "#{to_s}#{__rc_chain.nil? ? "" : " #{__rc_chain.__rc_gather}"}" end
|
45
|
+
end
|
14
46
|
|
15
47
|
class Config
|
16
48
|
attr_reader :__rc_attributes, :__rc_parent, :__rc_name, :__rc_chains, :__rc_locked
|
@@ -152,39 +184,44 @@ class RubyConf < BasicObject
|
|
152
184
|
def inspect() __rc_build_inspect end
|
153
185
|
end
|
154
186
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
if const && const.to_s[/^[A-Z]/]
|
161
|
-
const = const.to_sym
|
162
|
-
::Object.const_set(const, config) if !::Object.const_defined?(const) || ::Object.const_get(const).is_a?(Config)
|
163
|
-
end
|
164
|
-
|
165
|
-
if $RUBY_CONF.nil? && (name.nil? || name.to_s =~ /^(?:Rails)?Conf/)
|
166
|
-
$RUBY_CONF = if ::Object.const_defined?(:Rails)
|
167
|
-
cfg = config[:"#{::Rails.env}"] || config[:"#{::Rails.env}_conf"] || config[:"#{::Rails.env}_config"]
|
168
|
-
cfg && cfg.detach || config
|
169
|
-
else
|
170
|
-
config
|
171
|
-
end
|
187
|
+
@@__rc_configs = {}
|
188
|
+
class << self
|
189
|
+
def clear()
|
190
|
+
Loader::__rc_set_conf
|
191
|
+
@@__rc_configs.clear
|
172
192
|
end
|
173
193
|
|
174
|
-
|
175
|
-
|
194
|
+
def [](name) @@__rc_configs[name.to_sym] end
|
195
|
+
def method_missing(name, *args) @@__rc_configs[name.to_sym] end
|
196
|
+
def respond_to?(name) @@__rc_configs.key?(name.to_sym) end
|
176
197
|
|
177
|
-
|
198
|
+
def define(name = nil, options = {}, &block)
|
199
|
+
config = Config.new(name, &block)
|
200
|
+
@@__rc_configs[name.to_sym] = config unless name.nil?
|
178
201
|
|
179
|
-
|
202
|
+
const = options.fetch(:as, name)
|
203
|
+
if const && const.to_s[/^[A-Z]/]
|
204
|
+
const = const.to_sym
|
205
|
+
::Object.const_set(const, config) if !::Object.const_defined?(const) || ::Object.const_get(const).is_a?(Config)
|
206
|
+
end
|
180
207
|
|
181
|
-
|
208
|
+
if Loader::__rc_conf.nil? && (name.nil? || name.to_s =~ /^(?:Rails)?Conf/)
|
209
|
+
default_conf = if ::Object.const_defined?(:Rails)
|
210
|
+
cfg = config[:"#{::Rails.env}"] || config[:"#{::Rails.env}_conf"] || config[:"#{::Rails.env}_config"]
|
211
|
+
cfg && cfg.detach || config
|
212
|
+
else
|
213
|
+
config
|
214
|
+
end
|
215
|
+
Loader::__rc_set_conf(default_conf)
|
216
|
+
end
|
182
217
|
|
183
|
-
|
184
|
-
$RUBY_CONF = nil
|
185
|
-
@@__rc_configs.keys.each do |config|
|
186
|
-
remove_const(config.to_sym) if config.to_s[/^[A-Z]/] && const_defined?(config.to_sym)
|
218
|
+
config
|
187
219
|
end
|
188
|
-
@@__rc_configs.clear
|
189
220
|
end
|
221
|
+
|
190
222
|
end
|
223
|
+
|
224
|
+
RUBY_CONF = RubyConf::Loader
|
225
|
+
|
226
|
+
|
227
|
+
|
data/ruby-conf.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "ruby-conf"
|
8
|
-
s.version = "2.
|
8
|
+
s.version = "2.5.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Curtis Schofield & Hollin Wilkins & Mason"]
|
@@ -28,7 +28,10 @@ Gem::Specification.new do |s|
|
|
28
28
|
"VERSION",
|
29
29
|
"lib/ruby-conf.rb",
|
30
30
|
"ruby-conf.gemspec",
|
31
|
+
"spec/lib/a_not_conf.rb",
|
31
32
|
"spec/lib/ruby-conf_spec.rb",
|
33
|
+
"spec/lib/test_conf.rb.tmpl",
|
34
|
+
"spec/lib/z_not_loaded.rb",
|
32
35
|
"spec/spec_helper.rb"
|
33
36
|
]
|
34
37
|
s.homepage = "http://github.com/blazingpair/ruby-conf"
|
data/spec/lib/ruby-conf_spec.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require 'ruby-conf'
|
3
2
|
|
4
3
|
describe RubyConf do
|
5
4
|
|
@@ -195,8 +194,10 @@ TEXT
|
|
195
194
|
let(:inherited_config) do
|
196
195
|
RubyConf.define "inherited_config" do
|
197
196
|
basic do
|
197
|
+
movie "b"
|
198
198
|
thing do
|
199
199
|
origin "swamp"
|
200
|
+
type "monster"
|
200
201
|
end
|
201
202
|
end
|
202
203
|
|
@@ -209,14 +210,25 @@ TEXT
|
|
209
210
|
city :inherits => basic do
|
210
211
|
thing do
|
211
212
|
origin "ocean"
|
213
|
+
name "bob"
|
212
214
|
end
|
213
215
|
end
|
214
216
|
|
217
|
+
chained :inherits => city do
|
218
|
+
thing do
|
219
|
+
origin "space"
|
220
|
+
end
|
221
|
+
end
|
215
222
|
end
|
216
223
|
end
|
217
224
|
it "pre-loads a config with a existing config" do
|
218
225
|
inherited_config.laboritory.thing.origin.should == inherited_config.basic.thing.origin
|
219
226
|
end
|
227
|
+
it "chains inherited configs" do
|
228
|
+
inherited_config.chained.movie.should == "b"
|
229
|
+
inherited_config.chained.thing.origin.should == "space"
|
230
|
+
inherited_config.chained.thing.name.should == "bob"
|
231
|
+
end
|
220
232
|
it "does not overwrite values" do
|
221
233
|
inherited_config.city.thing.origin.should == "ocean"
|
222
234
|
end
|
@@ -365,13 +377,48 @@ TEXT
|
|
365
377
|
|
366
378
|
describe "Automatically sets the RAILS_CONF variable" do
|
367
379
|
|
380
|
+
after do
|
381
|
+
dir = Dir["./**/test_conf.rb.tmpl"].first[/^(.*?)\/test_conf.rb.tmpl$/, 1]
|
382
|
+
File.delete("#{dir}/test_conf.rb") if File.exists?("#{dir}/test_conf.rb")
|
383
|
+
end
|
384
|
+
|
385
|
+
it "will autoload the first ruby-conf that it can find if none is provided" do
|
386
|
+
dir = Dir["./**/test_conf.rb.tmpl"].first[/^(.*?)\/test_conf.rb.tmpl$/, 1]
|
387
|
+
|
388
|
+
val = Random.rand.to_s
|
389
|
+
File.write("#{dir}/test_conf.rb", File.read("#{dir}/test_conf.rb.tmpl").gsub('{{VALUE}}', val))
|
390
|
+
|
391
|
+
RUBY_CONF.should be_nil
|
392
|
+
RUBY_CONF.ident.should == "FOUND AND LOADED BASIC CONFIG #{val}"
|
393
|
+
loaded = RUBY_CONF.__rc_loaded_conf
|
394
|
+
|
395
|
+
FileUtils.touch(loaded[:path], mtime: 100)
|
396
|
+
File.mtime(loaded[:path]).to_i.should_not == loaded[:mtime]
|
397
|
+
RUBY_CONF.ident.should == "FOUND AND LOADED BASIC CONFIG #{val}"
|
398
|
+
RUBY_CONF.__rc_loaded_conf[:mtime].should == loaded[:mtime]
|
399
|
+
|
400
|
+
val = Random.rand.to_s
|
401
|
+
File.write("#{dir}/test_conf.rb", File.read("#{dir}/test_conf.rb.tmpl").gsub('{{VALUE}}', val))
|
402
|
+
FileUtils.touch(loaded[:path], mtime: 100)
|
403
|
+
RUBY_CONF.ident.should == "FOUND AND LOADED BASIC CONFIG #{val}"
|
404
|
+
RUBY_CONF.__rc_loaded_conf[:mtime].should_not == loaded[:mtime]
|
405
|
+
|
406
|
+
RubyConf.clear
|
407
|
+
module ::Rails
|
408
|
+
def self.env() "foo" end
|
409
|
+
end
|
410
|
+
::Object.const_defined?(:Rails).should be_true
|
411
|
+
RUBY_CONF.should be_nil
|
412
|
+
RUBY_CONF.ident.should == "FOUND AND LOADED RAILS ENV CONFIG #{val}"
|
413
|
+
end
|
414
|
+
|
368
415
|
it "sets the first unnamed config as default" do
|
369
|
-
|
416
|
+
RUBY_CONF.should be_nil
|
370
417
|
first = RubyConf.define { ident "first" }
|
371
|
-
|
418
|
+
RUBY_CONF.should_not be_nil
|
372
419
|
second = RubyConf.define { ident "second" }
|
373
|
-
|
374
|
-
|
420
|
+
RUBY_CONF.__rc_conf.should == first
|
421
|
+
RUBY_CONF.ident.should == "first"
|
375
422
|
end
|
376
423
|
|
377
424
|
it "sets the proper config based on Rails environment and detaches, if it exists" do
|
@@ -380,36 +427,35 @@ TEXT
|
|
380
427
|
end
|
381
428
|
::Object.const_defined?(:Rails).should be_true
|
382
429
|
|
383
|
-
|
430
|
+
RUBY_CONF.should be_nil
|
384
431
|
RubyConf.define do
|
385
432
|
foo { ident "correct" }
|
386
433
|
bar { ident "wrong" }
|
387
434
|
end
|
388
|
-
|
435
|
+
RUBY_CONF.ident.should == "correct"
|
389
436
|
|
390
437
|
RubyConf.clear
|
391
|
-
|
438
|
+
RUBY_CONF.should be_nil
|
392
439
|
RubyConf.define do
|
393
440
|
foo_conf { ident "correct" }
|
394
441
|
bar_conf { ident "wrong" }
|
395
442
|
end
|
396
|
-
|
443
|
+
RUBY_CONF.ident.should == "correct"
|
397
444
|
|
398
445
|
RubyConf.clear
|
399
|
-
|
446
|
+
RUBY_CONF.should be_nil
|
400
447
|
RubyConf.define do
|
401
448
|
foo_config { ident "correct" }
|
402
449
|
bar_config { ident "wrong" }
|
403
450
|
end
|
404
|
-
|
451
|
+
RUBY_CONF.ident.should == "correct"
|
405
452
|
|
406
453
|
RubyConf.clear
|
407
|
-
|
454
|
+
RUBY_CONF.should be_nil
|
408
455
|
RubyConf.define do
|
409
456
|
ident "correct"
|
410
457
|
end
|
411
|
-
|
412
|
-
|
458
|
+
RUBY_CONF.ident.should == "correct"
|
413
459
|
end
|
414
460
|
end
|
415
461
|
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
#this has the ruby-conf comment directive should be auto-loaded when required
|
4
|
+
#:ruby-conf
|
5
|
+
|
6
|
+
RubyConf.define do
|
7
|
+
ident "FOUND AND LOADED BASIC CONFIG {{VALUE}}"
|
8
|
+
foo do
|
9
|
+
ident "FOUND AND LOADED RAILS ENV CONFIG {{VALUE}}"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-conf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.5.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -95,7 +95,10 @@ files:
|
|
95
95
|
- VERSION
|
96
96
|
- lib/ruby-conf.rb
|
97
97
|
- ruby-conf.gemspec
|
98
|
+
- spec/lib/a_not_conf.rb
|
98
99
|
- spec/lib/ruby-conf_spec.rb
|
100
|
+
- spec/lib/test_conf.rb.tmpl
|
101
|
+
- spec/lib/z_not_loaded.rb
|
99
102
|
- spec/spec_helper.rb
|
100
103
|
homepage: http://github.com/blazingpair/ruby-conf
|
101
104
|
licenses:
|
@@ -112,7 +115,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
112
115
|
version: '0'
|
113
116
|
segments:
|
114
117
|
- 0
|
115
|
-
hash:
|
118
|
+
hash: 3183998097160756917
|
116
119
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
120
|
none: false
|
118
121
|
requirements:
|