Class: Zemu::ConfigObject
- Inherits:
-
Object
- Object
- Zemu::ConfigObject
- Defined in:
- lib/zemu/config.rb
Overview
Abstract configuration object. All configuration objects should inherit from this.
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(&block) ⇒ ConfigObject
constructor
A new instance of ConfigObject.
-
#method_missing(m, *args, &block) ⇒ Object
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.
Constructor Details
#initialize(&block) ⇒ ConfigObject
Returns a new instance of ConfigObject.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/zemu/config.rb', line 21 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
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.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/zemu/config.rb', line 53 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 |