Class: Patir::Configurator
- Inherits:
-
Object
- Object
- Patir::Configurator
- Defined in:
- lib/patir/configuration.rb
Overview
Configurator is the base class for all the Patir configuration classes.
The idea behind the configurator is that the developer creates a module that contains as methods all the configuration directives. He then derives a class from Configurator and includes the directives module. The Configurator loads the configuration file and evals it with itself as context (variable configuration), so the directives become methods in the configuration file: configuration.directive=“some value” configuration.other_directive=to group values together”,:other_key=>“omg”
The Configurator instance contains all the configuration data. Configurator#configuration method is provided as a post-processing step. It should be overriden to return the configuration data in the desired format and perform any overall validation steps (single element validation steps should be done in the directives module).
Example
module SimpleConfiguration
def name= tool_name
raise Patir::ConfigurationException,"Inappropriate language not allowed" if tool_name=="@#!&@&$}"
@name=tool_name
end
end
class SimpleConfigurator
include SimpleConfiguration
def configuration
return @name
end
end The configuration file would then be configuration.name=“really polite name” To use it you would do cfg=SimpleConfigurator.new(“config.cfg”).configuration
Instance Attribute Summary collapse
-
#config_file ⇒ Object
readonly
Returns the value of attribute config_file.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#wd ⇒ Object
readonly
Returns the value of attribute wd.
Instance Method Summary collapse
-
#configuration ⇒ Object
Returns self.
-
#initialize(config_file, logger = nil) ⇒ Configurator
constructor
A new instance of Configurator.
-
#load_from_file(filename) ⇒ Object
Loads the configuration from a file.
Constructor Details
#initialize(config_file, logger = nil) ⇒ Configurator
Returns a new instance of Configurator.
41 42 43 44 45 46 |
# File 'lib/patir/configuration.rb', line 41 def initialize config_file,logger=nil @logger=logger @logger||=Patir.setup_logger @config_file=config_file load_configuration(@config_file) end |
Instance Attribute Details
#config_file ⇒ Object (readonly)
Returns the value of attribute config_file.
40 41 42 |
# File 'lib/patir/configuration.rb', line 40 def config_file @config_file end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
40 41 42 |
# File 'lib/patir/configuration.rb', line 40 def logger @logger end |
#wd ⇒ Object (readonly)
Returns the value of attribute wd.
40 41 42 |
# File 'lib/patir/configuration.rb', line 40 def wd @wd end |
Instance Method Details
#configuration ⇒ Object
Returns self. This should be overriden in the actual implementations
49 50 51 |
# File 'lib/patir/configuration.rb', line 49 def configuration return self end |
#load_from_file(filename) ⇒ Object
Loads the configuration from a file
Use this to chain configuration files together
Example
Say you have on configuration file “first.cfg” that contains all the generic directives and several others that change only one or two things.
You can ‘include’ the first.cfg file in the other configurations with configuration.load_from_file(“first.cfg”)
61 62 63 64 |
# File 'lib/patir/configuration.rb', line 61 def load_from_file filename fnm = File.exist?(filename) ? filename : File.join(@wd,filename) load_configuration(fnm) end |