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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f49c05d8c2034aa1970d1698e22da05f9087cf6
4
- data.tar.gz: 31165aaac2b92557f540d524ad9d4ea947662394
3
+ metadata.gz: e82f1410ca956af64f001d97b24d17d4c58bc63c
4
+ data.tar.gz: 81be06542d7659ecee8be0a8766431189eaa45f6
5
5
  SHA512:
6
- metadata.gz: 0d213e98b6b0fc79bff17f96c218f7d9dce072b2e6c7c4f1d19326ca72fc95bd3ec97bdeb7a58e43a8b931af1c36c5e8bafa8d05971a18a03bc53837463c0089
7
- data.tar.gz: 4e2763f923be989f93803396b31dbe860a92f99f2f284bf951a3636efb5d23dd2df9a35c964bbf787116e36683be38884b01e7e5930bc52e9dbc8ebacf3d5b6b
6
+ metadata.gz: 03990c55d7f54f5658b98d0ed8dfa5bc13d79c0e93887dfad1e52c64e63fd1b6865ee0dea28a4a7a689c2f9e85c5a9cd9b2b436d664bf539642a659264394675
7
+ data.tar.gz: 3b613f2d55b7fd741623df6bca0ffba8fd056a9188dca94de02ac736e5e6d955df4c77e4a05e44d9ad62e48785b655a0dda5433676a81a4214f8da368b800d3e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## v0.1.4
2
+ * Delegate more methods from config instance to underlying data instance
3
+ * Add initial support for auto reloading configuration
4
+
1
5
  ## v0.1.2
2
6
  * Update `Bogo::Config` to behave more like `Smash`
3
7
  * Make XML usage more consistent and apply type formats
@@ -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
- attr_accessor :path
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
- if(path_or_hash.is_a?(String))
25
- @path = path_or_hash
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 = path_or_hash
115
+ hash = initial
29
116
  end
30
117
  if(hash)
31
- load_data(hash)
32
- data.replace(hash.to_smash.deep_merge(data))
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(false))
127
+ def_delegators *([:data, :[]] + (Smash.public_instance_methods - Object.public_instance_methods))
38
128
 
39
129
  # Load configuration from file(s)
40
130
  #
@@ -1,6 +1,6 @@
1
1
  module Bogo
2
2
  class Config
3
3
  # Current library version
4
- VERSION = Gem::Version.new('0.1.2')
4
+ VERSION = Gem::Version.new('0.1.4')
5
5
  end
6
6
  end
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.2
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: 2014-12-31 00:00:00.000000000 Z
11
+ date: 2015-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bogo