Class: Configuration

Inherits:
Object
  • Object
show all
Defined in:
rlib/configuration.rb

Overview

Reads json configuration and provides access to the configuration data as method calls.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Configuration) initialize(filename = "#{File.dirname(__FILE__)}/../conf/config.json")

Creates an instance of Configuration from a json file

Parameters:

  • filename (String) (defaults to: "#{File.dirname(__FILE__)}/../conf/config.json")

    The 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)

Note:

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

Parameters:

  • symbol (symbol, String)

    The method name that maps to a json configuration item

  • args (Array)

    Not used, but would hold arguments to the method call. Should be zero length for our methods.

  • block (Block)

    Not used, but would be a code block supplied to the method.

Returns:

  • (Object)

    the data associated with the json name, (hence method name) in the configuration file.



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)

Note:

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

Parameters:

  • symbol (Symbol, String)

    The method name we need to test exists

  • include_private (Boolean) (defaults to: false)

    Extend the test to private methods

Returns:

  • (Boolean)

    true if the method 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

Returns:

  • (String)

    the configuration



40
41
42
# File 'rlib/configuration.rb', line 40

def to_s
  @pjson.to_s
end