konfa 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/konfa.rb +30 -57
  2. metadata +2 -2
@@ -1,29 +1,31 @@
1
- # -*- coding: utf-8 -*-
2
- require 'yaml'
1
+ require_relative File.join(File.dirname(__FILE__), 'konfa', 'initializer')
3
2
 
4
3
  module Konfa
5
4
  class Base
5
+ include Konfa::Initializer
6
+
6
7
  class << self
7
8
  protected
8
9
 
9
10
  #
10
- # The following methods are not a part of the public API. You may sublcass
11
+ # The following methods are not a part of the public API. You may subclass
11
12
  # them, but remember that unless you scope them as protected or private,
12
13
  # they will then be public
13
14
  #
14
15
 
15
- attr_writer :configuration, :deferred, :initialized_deferred
16
+ attr_writer :configuration, :initializer, :initialized
16
17
 
17
- def initialized_deferred
18
- @initialized_deferred ||= false
18
+ def initialized
19
+ @initialized ||= false
19
20
  end
20
21
 
21
22
  def configuration
23
+ self.init
22
24
  @configuration ||= self.allowed_variables
23
25
  end
24
26
 
25
- def deferred
26
- @deferred ||= nil
27
+ def initializer
28
+ @initializer ||= nil
27
29
  end
28
30
 
29
31
  def truthy?(value)
@@ -39,11 +41,6 @@ module Konfa
39
41
  end
40
42
  end
41
43
 
42
- def do_deferred_initialization
43
- self.send(self.deferred.first, *self.deferred[1..-1])
44
- self.initialized_deferred = true
45
- end
46
-
47
44
  public
48
45
 
49
46
  #
@@ -63,16 +60,11 @@ module Konfa
63
60
  raise UnsupportedVariableError.new(key)
64
61
  end
65
62
 
66
- def do_deferred_initialization?
67
- !self.initialized_deferred && !self.deferred.nil?
68
- end
69
-
70
63
  #
71
64
  # The following methods provides the interface to this class
72
65
  #
73
66
 
74
67
  def get(variable)
75
- self.do_deferred_initialization if self.do_deferred_initialization?
76
68
  raise UnsupportedVariableError.new(variable) unless self.configuration.has_key? variable
77
69
  self.configuration[variable]
78
70
  end
@@ -93,8 +85,26 @@ module Konfa
93
85
  self.configuration.dup
94
86
  end
95
87
 
96
- def initialize_deferred(method, *args)
97
- self.deferred = [method, *args]
88
+ def init?
89
+ !self.initialized && !self.initializer.nil?
90
+ end
91
+
92
+ def init
93
+ return unless self.init?
94
+ # Set to true before calling to prevent recursion if
95
+ # an initializer is accessing the configuration
96
+ self.initialized = true
97
+ self.send(self.initializer.first, *self.initializer[1..-1])
98
+ self.after_initialize
99
+ end
100
+
101
+ def init_with(suffix, *args)
102
+ self.initializer = [:"init_with_#{suffix}", *args]
103
+ self
104
+ end
105
+
106
+ def reinit
107
+ self.initialized = false
98
108
  end
99
109
 
100
110
  def after_initialize
@@ -113,46 +123,9 @@ module Konfa
113
123
  result
114
124
  end
115
125
 
116
- # FIXME: Move out to external package
117
- def initialize_from_yaml(path)
118
- # FIXME: It would be a lot cleaner if the YAML library would raise an
119
- # exception if it fails to read the file. We'll handle it like this for now
120
- # load_file just returns "false" if it fails
121
- yaml_data = YAML.load_file(path)
122
-
123
- unless yaml_data.nil?
124
- raise InitializationError.new("Bad YAML format, key/value pairs expected") unless yaml_data.kind_of?(Hash)
125
-
126
- yaml_data.each do |variable, value|
127
- self.store(variable, value)
128
- end
129
- end
130
-
131
- after_initialize
132
- dump
133
- end
134
-
135
- # FIXME: Move out to external package
136
- def initialize_from_env
137
- conf_prefix = self.env_variable_prefix.upcase
138
-
139
- ENV.keys.reject { |key|
140
- key !~ /^#{conf_prefix}/ # Ignore everything that doesn't match the prefix
141
- }.each { |key|
142
- variable = key[conf_prefix.size..-1].downcase
143
-
144
- self.store(variable, ENV[key])
145
- }
146
-
147
- after_initialize
148
- dump
149
- end
150
126
  end
151
127
  end
152
128
 
153
- class InitializationError < StandardError
154
- end
155
-
156
129
  class UnsupportedVariableError < StandardError
157
130
  end
158
131
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: konfa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -40,7 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
40
  version: '0'
41
41
  requirements: []
42
42
  rubyforge_project:
43
- rubygems_version: 1.8.23
43
+ rubygems_version: 1.8.24
44
44
  signing_key:
45
45
  specification_version: 3
46
46
  summary: Application configuration