class Zemu::ConfigObject
Abstract configuration object. All configuration objects should inherit from this.
Public Class Methods
new(&block)
click to toggle source
# File lib/zemu/config.rb, line 19 def initialize(&block) if self.class == Zemu::ConfigObject raise NotImplementedError, "Cannot construct an instance of the abstract class Zemu::ConfigObject." end @initialized = false # Initialize each parameter to nil params.each do |p| if params_init[p].nil? instance_variable_set("@#{p}", nil) else instance_variable_set("@#{p}", params_init[p]) end end # Instance eval the block. instance_eval(&block) # Raise a ConfigError if any of the parameters are unset. params.each do |p| if instance_variable_get("@#{p}").nil? raise ConfigError, "The #{p} parameter of a #{self.class.name} configuration object must be set." end end @initialized = true end
Public Instance Methods
method_missing(m, *args, &block)
click to toggle source
This allows some metaprogramming magic to allow the user to set instance variables (config parameters) while initializing the configuration object, but ensures that these parameters are readonly once the object is initialized.
Calls superclass method
# File lib/zemu/config.rb, line 51 def method_missing(m, *args, &block) params.each do |v| # We don't allow the setting of instance variables if the object # has been initialized. if m == "#{v}".to_sym if args.size == 1 && !@initialized instance_variable_set("@#{v}", args[0]) return elsif args.size == 0 return instance_variable_get("@#{v}") end end end # Otherwise just call super's method_missing super end
Protected Instance Methods
params()
click to toggle source
Defines the parameters of this configuration object.
# File lib/zemu/config.rb, line 6 def params raise NotImplementedError, "params must be overridden by inheriting class." end
params_init()
click to toggle source
Defines the initial values of parameters, if any.
# File lib/zemu/config.rb, line 13 def params_init return {} end