class Fuguta::Configuration
Constants
- DEPRECATED_ERROR_MESSAGE
- DEPRECATED_WARNING_MESSAGE
- SYNTAX_ERROR_SOURCES
- ValidationError
Attributes
config[R]
parent[R]
Public Class Methods
DSL(&blk)
click to toggle source
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
# File lib/fuguta.rb, line 359 def DSL(&blk) self.const_get(:DSL, false).class_eval(&blk) self end
alias_param(alias_name, ref_name)
click to toggle source
# File lib/fuguta.rb, line 267 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)
click to toggle source
# File lib/fuguta.rb, line 256 def deprecated_error_for(old_name, param_name, message=nil) err_msg = message || 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)
click to toggle source
Raise an error if the old parameter is set.
# File lib/fuguta.rb, line 250 def deprecated_error_param(old_name, message=nil) on_param_create_hook do |param_name, opts| self.deprecated_error_for(old_name, param_name, message) end end
deprecated_warn_for(old_name, param_name, message=nil)
click to toggle source
# File lib/fuguta.rb, line 237 def deprecated_warn_for(old_name, param_name, message=nil) warn_msg = message || 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)
click to toggle source
Show warning message if the old parameter is set.
# File lib/fuguta.rb, line 231 def deprecated_warn_param(old_name, message=nil) on_param_create_hook do |param_name, opts| self.deprecated_warn_for(old_name, param_name, message) end end
load(*paths)
click to toggle source
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
# File lib/fuguta.rb, line 332 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
new(parent=nil)
click to toggle source
# File lib/fuguta.rb, line 396 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
on_initialize_hook(&blk)
click to toggle source
# File lib/fuguta.rb, line 219 def on_initialize_hook(&blk) @on_initialize_hooks << blk end
on_initialize_hooks()
click to toggle source
# File lib/fuguta.rb, line 223 def on_initialize_hooks @on_initialize_hooks end
param(name, opts={})
click to toggle source
# File lib/fuguta.rb, line 286 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)
click to toggle source
# File lib/fuguta.rb, line 35 def self.usual_paths(paths = nil) if paths @usual_paths = paths else @usual_paths end end
walk_tree(conf, &blk)
click to toggle source
# File lib/fuguta.rb, line 47 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
Private Class Methods
inherited(klass)
click to toggle source
Calls superclass method
# File lib/fuguta.rb, line 365 def inherited(klass) super klass.const_set(:DSL, Module.new) klass.instance_eval { @on_initialize_hooks = [] @opts = {} @on_param_create_hooks = [] } dsl_mods = [] c = klass while c < Configuration && c.superclass.const_defined?(:DSL, false) parent_dsl = c.superclass.const_get(:DSL, false) if parent_dsl && parent_dsl.class === Module dsl_mods << parent_dsl end c = c.superclass end # including order is ancestor -> descendants dsl_mods.reverse.each { |i| klass.const_get(:DSL, false).__send__(:include, i) } end
on_param_create_hook(&blk)
click to toggle source
# File lib/fuguta.rb, line 389 def on_param_create_hook(&blk) @on_param_create_hooks << blk end
Public Instance Methods
after_initialize()
click to toggle source
# File lib/fuguta.rb, line 419 def after_initialize end
parse_dsl(&blk)
click to toggle source
# File lib/fuguta.rb, line 427 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()
click to toggle source
# File lib/fuguta.rb, line 43 def usual_paths self.class.usual_paths end
validate(errors)
click to toggle source
# File lib/fuguta.rb, line 422 def validate(errors) end
Private Instance Methods
method_missing(m, *args)
click to toggle source
Calls superclass method
# File lib/fuguta.rb, line 445 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