bogo-config 0.1.2 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/bogo-config/config.rb +97 -7
- data/lib/bogo-config/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e82f1410ca956af64f001d97b24d17d4c58bc63c
|
4
|
+
data.tar.gz: 81be06542d7659ecee8be0a8766431189eaa45f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 03990c55d7f54f5658b98d0ed8dfa5bc13d79c0e93887dfad1e52c64e63fd1b6865ee0dea28a4a7a689c2f9e85c5a9cd9b2b436d664bf539642a659264394675
|
7
|
+
data.tar.gz: 3b613f2d55b7fd741623df6bca0ffba8fd056a9188dca94de02ac736e5e6d955df4c77e4a05e44d9ad62e48785b655a0dda5433676a81a4214f8da368b800d3e
|
data/CHANGELOG.md
CHANGED
data/lib/bogo-config/config.rb
CHANGED
@@ -10,31 +10,121 @@ module Bogo
|
|
10
10
|
|
11
11
|
class Config
|
12
12
|
|
13
|
+
class << self
|
14
|
+
|
15
|
+
include Bogo::Memoization
|
16
|
+
|
17
|
+
# Reload any registered `Bogo::Config` instances
|
18
|
+
#
|
19
|
+
# @return [TrueClass]
|
20
|
+
def reload!
|
21
|
+
obj_ids = memoize(:bogo_reloadable_configs, :global)
|
22
|
+
objects = Thread.exclusive do
|
23
|
+
ObjectSpace.each_object.find_all do |obj|
|
24
|
+
obj_ids.include?(obj.object_id)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
objects.map(&:init!)
|
28
|
+
memoize(:bogo_reloadable_configs, :global).delete_if do |oid|
|
29
|
+
!obj_ids.include?(oid)
|
30
|
+
end
|
31
|
+
true
|
32
|
+
end
|
33
|
+
|
34
|
+
# Register config instance to auto reload on HUP
|
35
|
+
#
|
36
|
+
# @param config [Bogo::Config]
|
37
|
+
# @return [TrueClass]
|
38
|
+
def reloadable(config)
|
39
|
+
if(config.is_a?(Bogo::Config))
|
40
|
+
reloader
|
41
|
+
memoize(:bogo_reloadable_configs, :global){ [] }.push(config.object_id).uniq!
|
42
|
+
else
|
43
|
+
raise TypeError.new "Expecting type `Bogo::Config`. Received: `#{config.class}`"
|
44
|
+
end
|
45
|
+
true
|
46
|
+
end
|
47
|
+
|
48
|
+
# Internal reloader
|
49
|
+
#
|
50
|
+
# @return [Thread]
|
51
|
+
def reloader
|
52
|
+
memoize(:bogo_config_reloader, :global) do
|
53
|
+
Thread.new do
|
54
|
+
begin
|
55
|
+
loop do
|
56
|
+
begin
|
57
|
+
sleep
|
58
|
+
rescue SignalException => e
|
59
|
+
if(e.signm == 'SIGHUP')
|
60
|
+
if(ENV['DEBUG'])
|
61
|
+
$stdout.puts 'SIGHUP encountered. Reloading `Bogo::Config` instances.'
|
62
|
+
end
|
63
|
+
Bogo::Config.reload!
|
64
|
+
else
|
65
|
+
raise
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
rescue => e
|
70
|
+
if(ENV['DEBUG'])
|
71
|
+
$stderr.puts "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
|
72
|
+
end
|
73
|
+
retry
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
13
81
|
include Bogo::Lazy
|
14
82
|
extend Forwardable
|
15
83
|
|
16
84
|
# @return [String] configuration path
|
17
|
-
|
85
|
+
attr_reader :path
|
86
|
+
# @return [String, Hash]
|
87
|
+
attr_reader :initial
|
18
88
|
|
19
89
|
# Create new instance
|
20
90
|
#
|
21
91
|
# @param path_or_hash [String, Hash] file/directory path or base Hash
|
22
92
|
# @return [self]
|
23
93
|
def initialize(path_or_hash=nil)
|
24
|
-
|
25
|
-
|
94
|
+
@initial = path_or_hash
|
95
|
+
@data = Smash.new
|
96
|
+
init!
|
97
|
+
end
|
98
|
+
|
99
|
+
# Enables automatic reloading on SIGHUP
|
100
|
+
#
|
101
|
+
# @return [TrueClass]
|
102
|
+
def reloadable!
|
103
|
+
Bogo::Config.reloadable(self)
|
104
|
+
true
|
105
|
+
end
|
106
|
+
|
107
|
+
# Intialize the configuration
|
108
|
+
#
|
109
|
+
# @return [self]
|
110
|
+
def init!
|
111
|
+
if(initial.is_a?(String))
|
112
|
+
@path = initial.dup
|
26
113
|
hash = load!
|
27
114
|
else
|
28
|
-
hash =
|
115
|
+
hash = initial
|
29
116
|
end
|
30
117
|
if(hash)
|
31
|
-
|
32
|
-
|
118
|
+
Thread.exclusive do
|
119
|
+
load_data(hash)
|
120
|
+
data.replace(hash.to_smash.deep_merge(data))
|
121
|
+
end
|
33
122
|
end
|
123
|
+
self
|
34
124
|
end
|
35
125
|
|
36
126
|
# Allow Smash like behavior
|
37
|
-
def_delegators *([:data, :[]] + Smash.public_instance_methods
|
127
|
+
def_delegators *([:data, :[]] + (Smash.public_instance_methods - Object.public_instance_methods))
|
38
128
|
|
39
129
|
# Load configuration from file(s)
|
40
130
|
#
|
data/lib/bogo-config/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bogo-config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Roberts
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bogo
|