Class: Configuration
- Inherits:
-
Object
- Object
- Configuration
- Defined in:
- lib/wikk_configuration.rb
Overview
Reads json configuration and provides access to the configuration data as method calls.
Constant Summary collapse
- VERSION =
'0.1.0'
Instance Method Summary collapse
-
#initialize(filename = "#{File.dirname(__FILE__)}/../conf/config.json") ⇒ Configuration
constructor
Creates an instance of Configuration from a json file.
-
#method_missing(symbol, *args, &block) ⇒ Object
Default handler to map json configuration names to method names.
-
#respond_to?(symbol, include_private = false) ⇒ Boolean
Provides a test for a method named after a json configuration item exists.
-
#to_s ⇒ String
The configuration.
Constructor Details
#initialize(filename = "#{File.dirname(__FILE__)}/../conf/config.json") ⇒ Configuration
Creates an instance of Configuration from a json file
11 12 13 14 |
# File 'lib/wikk_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
#method_missing(symbol, *args, &block) ⇒ Object
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 'lib/wikk_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 |
Instance Method Details
#respond_to?(symbol, include_private = false) ⇒ Boolean
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 'lib/wikk_configuration.rb', line 21 def respond_to?(symbol, include_private = false) (@pjson[symbol.to_s] != nil) || super(symbol, include_private) end |
#to_s ⇒ String
Returns the configuration.
41 42 43 |
# File 'lib/wikk_configuration.rb', line 41 def to_s @pjson.to_s end |