Class: Configuration
- Inherits:
-
Object
- Object
- Configuration
- Defined in:
- rlib/configuration.rb
Overview
Reads json configuration and provides access to the configuration data as method calls.
Class Method Summary (collapse)
-
+ (Object) test
Provides a self test of this class, by reading the test configuration file.
Instance Method Summary (collapse)
-
- (Configuration) initialize(filename = "#{File.dirname(__FILE__)}/../conf/config.json")
constructor
Creates an instance of Configuration from a json file.
-
- (Object) method_missing(symbol, *args, &block)
Default handler to map json configuration names to method names.
-
- (Boolean) respond_to?(symbol, include_private = false)
Provides a test for a method named after a json configuration item exists.
-
- (String) to_s
The configuration.
Constructor Details
- (Configuration) initialize(filename = "#{File.dirname(__FILE__)}/../conf/config.json")
Creates an instance of Configuration from a json file
11 12 13 14 |
# File 'rlib/configuration.rb', line 11 def initialize(filename="#{File.dirname(__FILE__)}/../conf/config.json") json = File.read(filename) @pjson = JSON.parse(json) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(symbol, *args, &block)
Be aware of the possibility of name conflicts between built in class methods an configuration items defined in the json file)
Default handler to map json configuration names to method names
31 32 33 34 35 36 37 38 |
# File 'rlib/configuration.rb', line 31 def method_missing(symbol , *args, &block) s = symbol.to_s if @pjson[s] != nil return @pjson[s] else super end end |
Class Method Details
+ (Object) test
Provides a self test of this class, by reading the test configuration file.
and attempting to access configuration items as methods.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'rlib/configuration.rb', line 47 def self.test config = Configuration.new('../conf/config_test.json') puts "base_directories is defined? #{config.respond_to?(:base_directory)}" puts "not_there is defined? #{config.respond_to?(:not_there)}" puts "base_directories is: '#{config.base_directory}'" puts "hello is of class: #{config.hello.class}" puts "config.hello[0] => #{config.hello[0]}" puts "world is of class: #{config.world.class}" puts "config.world['1'] => #{config.world['1']}" puts "boolean is of class: #{config.boolean.class}" puts "config.boolean => #{config.boolean}" puts "Dump the config file using to_s" puts config end |
Instance Method Details
- (Boolean) respond_to?(symbol, include_private = false)
We need to define respond_to? as well as method_missing to satisfy tests in some libraries.
Provides a test for a method named after a json configuration item exists
21 22 23 |
# File 'rlib/configuration.rb', line 21 def respond_to?(symbol, include_private = false) (@pjson[symbol.to_s] != nil) || super(symbol, include_private) end |
- (String) to_s
Returns the configuration
41 42 43 |
# File 'rlib/configuration.rb', line 41 def to_s @pjson.to_s end |