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
10 11 12 13 |
# File 'rlib/configuration.rb', line 10 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
30 31 32 33 34 35 36 37 |
# File 'rlib/configuration.rb', line 30 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.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'rlib/configuration.rb', line 46 def self.test config = Configuration.new('../conf/config_test.json') $stderr.puts "base_directories is defined? #{config.respond_to?(:base_directory)}" $stderr.puts "not_there is defined? #{config.respond_to?(:not_there)}" $stderr.puts "base_directories is: '#{config.base_directory}'" $stderr.puts "hello is of class: #{config.hello.class}" $stderr.puts "config.hello[0] => #{config.hello[0]}" $stderr.puts "world is of class: #{config.world.class}" $stderr.puts "config.world['1'] => #{config.world['1']}" $stderr.puts "boolean is of class: #{config.boolean.class}" $stderr.puts "config.boolean => #{config.boolean}" $stderr.puts "Dump the config file using to_s" $stderr.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
20 21 22 |
# File 'rlib/configuration.rb', line 20 def respond_to?(symbol, include_private = false) (@pjson[symbol.to_s] != nil) || super(symbol, include_private) end |
- (String) to_s
Returns the configuration
40 41 42 |
# File 'rlib/configuration.rb', line 40 def to_s @pjson.to_s end |