Class: Fuguta::Configuration
- Inherits:
-
Object
- Object
- Fuguta::Configuration
- Defined in:
- lib/fuguta.rb
Defined Under Namespace
Modules: ConfigurationMethods Classes: DSLProxy, Loader
Constant Summary collapse
- ValidationError =
Fuguta::ValidationError
- DEPRECATED_WARNING_MESSAGE =
"WARN: Deprecated parameter: %1$s. Please use '%2$s'".freeze
- DEPRECATED_ERROR_MESSAGE =
"ERROR: Parameter is no longer supported: %1$s. Please use '%2$s'".freeze
- SYNTAX_ERROR_SOURCES =
[ScriptError, NameError].freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Class Method Summary collapse
- .alias_param(alias_name, ref_name) ⇒ Object
- .deprecated_error_for(old_name, param_name, message = nil) ⇒ Object
-
.deprecated_error_param(old_name, message = nil) ⇒ Object
Raise an error if the old parameter is set.
- .deprecated_warn_for(old_name, param_name, message = nil) ⇒ Object
-
.deprecated_warn_param(old_name, message = nil) ⇒ Object
Show warning message if the old parameter is set.
-
.DSL(&blk) ⇒ Object
Helper method defines “module DSL” under the current conf class.
-
.load(*paths) ⇒ Object
Load configuration file.
- .on_initialize_hook(&blk) ⇒ Object
- .on_initialize_hooks ⇒ Object
- .param(name, opts = {}) ⇒ Object
- .usual_paths(paths = nil) ⇒ Object
- .walk_tree(conf, &blk) ⇒ Object
Instance Method Summary collapse
- #after_initialize ⇒ Object
-
#initialize(parent = nil) ⇒ Configuration
constructor
A new instance of Configuration.
- #parse_dsl(&blk) ⇒ Object
- #usual_paths ⇒ Object
- #validate(errors) ⇒ Object
Constructor Details
#initialize(parent = nil) ⇒ Configuration
Returns a new instance of Configuration.
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/fuguta.rb', line 397 def initialize(parent=nil) unless parent.nil? raise ArgumentError, "parent must be a 'Fuguta::Configuration'. Got '#{parent.class}'." unless parent.is_a?(Fuguta::Configuration) end @config = {} @parent = parent hook_lst = [] c = self.class while c < Configuration hook_lst << c.instance_variable_get(:@on_initialize_hooks) c = c.superclass end hook_lst.reverse.each { |l| l.each { |c| self.instance_eval(&c) } } after_initialize end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object (private)
446 447 448 449 450 451 452 453 454 |
# File 'lib/fuguta.rb', line 446 def method_missing(m, *args) if @config.has_key?(m.to_sym) @config[m.to_sym] elsif @config.has_key?(m.to_s) @config[m.to_s] else super end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
395 396 397 |
# File 'lib/fuguta.rb', line 395 def config @config end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
395 396 397 |
# File 'lib/fuguta.rb', line 395 def parent @parent end |
Class Method Details
.alias_param(alias_name, ref_name) ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/fuguta.rb', line 268 def alias_param (alias_name, ref_name) # getter self.class_eval %Q{ # Ruby alias show error if the method to be defined later is # set. So create method to call the reference method. def #{alias_name.to_s}() #{ref_name}() end } # DSL setter self.const_get(:DSL, false).class_eval %Q{ def #{alias_name}(v) #{ref_name.to_s}(v) end alias_method :#{alias_name.to_s}=, :#{alias_name.to_s} } end |
.deprecated_error_for(old_name, param_name, message = nil) ⇒ Object
257 258 259 260 261 262 263 264 265 266 |
# File 'lib/fuguta.rb', line 257 def deprecated_error_for(old_name, param_name, =nil) err_msg = || DEPRECATED_ERROR_MESSAGE alias_param old_name, param_name self.const_get(:DSL, false).class_eval %Q{ def #{old_name}(v) raise ("#{err_msg}" % ["#{old_name}", "#{param_name}"]) end } end |
.deprecated_error_param(old_name, message = nil) ⇒ Object
Raise an error if the old parameter is set.
251 252 253 254 255 |
# File 'lib/fuguta.rb', line 251 def deprecated_error_param(old_name, =nil) on_param_create_hook do |param_name, opts| self.deprecated_error_for(old_name, param_name, ) end end |
.deprecated_warn_for(old_name, param_name, message = nil) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/fuguta.rb', line 238 def deprecated_warn_for(old_name, param_name, =nil) warn_msg = || DEPRECATED_WARNING_MESSAGE alias_param old_name, param_name self.const_get(:DSL, false).class_eval %Q{ def #{old_name}(v) STDERR.puts("#{warn_msg}" % ["#{old_name}", "#{param_name}"]) #{param_name.to_s}(v) end } end |
.deprecated_warn_param(old_name, message = nil) ⇒ Object
Show warning message if the old parameter is set.
232 233 234 235 236 |
# File 'lib/fuguta.rb', line 232 def deprecated_warn_param(old_name, =nil) on_param_create_hook do |param_name, opts| self.deprecated_warn_for(old_name, param_name, ) end end |
.DSL(&blk) ⇒ Object
Helper method defines “module DSL” under the current conf class.
This does mostly same things as “module DSL” but using “module” statement get the “DSL” constant defined in unexpected location if you combind to use with other Ruby DSL syntax.
Usage: class Conf1 < Configuration
DSL do
end
end
360 361 362 363 |
# File 'lib/fuguta.rb', line 360 def DSL(&blk) self.const_get(:DSL, false).class_eval(&blk) self end |
.load(*paths) ⇒ Object
Load configuration file
-
Simply loads single configuration file.
conf = ConfigurationClass.load('1.conf')
-
Loads multiple files and merge.
file1.conf:
config.param1 = 1
file2.conf:
config.param1 = 2
conf = ConfigurationClass.load('file1.conf', 'file2.conf')
conf.param1 == 2
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/fuguta.rb', line 333 def load(*paths) c = self.new l = Loader.new(c) if paths.empty? l.load else paths.each { |path| l.load(path) } end l.validate c end |
.on_initialize_hook(&blk) ⇒ Object
220 221 222 |
# File 'lib/fuguta.rb', line 220 def on_initialize_hook(&blk) @on_initialize_hooks << blk end |
.on_initialize_hooks ⇒ Object
224 225 226 |
# File 'lib/fuguta.rb', line 224 def on_initialize_hooks @on_initialize_hooks end |
.param(name, opts = {}) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/fuguta.rb', line 287 def param(name, opts={}) opts = opts.merge(@opts) case opts[:default] when Proc # create getter method if proc is set as default value self.class_exec { define_method(name.to_s.to_sym) do @config[name.to_s.to_sym] || self.instance_exec(&opts[:default]) end } else on_initialize_hook do |c| @config[name.to_s.to_sym] = opts[:default] end end @on_param_create_hooks.each { |blk| blk.call(name.to_s.to_sym, opts) } self.const_get(:DSL, false).class_eval %Q{ def #{name}(v) @config["#{name.to_s}".to_sym] = v end alias_method :#{name.to_s}=, :#{name.to_s} } @opts.clear @on_param_create_hooks.clear end |
.usual_paths(paths = nil) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/fuguta.rb', line 36 def self.usual_paths(paths = nil) if paths @usual_paths = paths else @usual_paths end end |
.walk_tree(conf, &blk) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/fuguta.rb', line 48 def self.walk_tree(conf, &blk) raise ArgumentError, "conf must be a 'Configuration'. Got '#{conf.class}'." unless conf.is_a?(Configuration) blk.call(conf) conf.config.values.each { |c| case c when Configuration walk_tree(c, &blk) when Hash c.values.each { |c1| walk_tree(c1, &blk) if c1.is_a?(Configuration) } when Array c.each { |c1| walk_tree(c1, &blk) if c1.is_a?(Configuration) } end } end |
Instance Method Details
#after_initialize ⇒ Object
420 421 |
# File 'lib/fuguta.rb', line 420 def after_initialize end |
#parse_dsl(&blk) ⇒ Object
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/fuguta.rb', line 428 def parse_dsl(&blk) dsl = self.class.const_get(:DSL, false) raise "DSL module was not found" unless dsl && dsl.is_a?(Module) cp_class = DSLProxy.dup cp_class.__send__(:include, dsl) cp = cp_class.new(self) begin cp.instance_eval(&blk) rescue *SYNTAX_ERROR_SOURCES => e raise Fuguta::SyntaxError.new(e, cp.instance_variable_get(:@loading_path)) end self end |
#usual_paths ⇒ Object
44 45 46 |
# File 'lib/fuguta.rb', line 44 def usual_paths self.class.usual_paths end |
#validate(errors) ⇒ Object
423 424 |
# File 'lib/fuguta.rb', line 423 def validate(errors) end |