Class: WIKK::Configuration
- Inherits:
-
Object
- Object
- WIKK::Configuration
- Defined in:
- lib/wikk_configuration.rb
Overview
Reads json configuration and provides access to the configuration data as method calls.
@attr_accessor pjson [Hash] Raw hash created from reading the json file
Constant Summary collapse
- VERSION =
'0.1.3'
Instance Attribute Summary collapse
-
#pjson ⇒ Object
Returns the value of attribute pjson.
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.
-
#save(filename = nil) ⇒ Object
Write Json config file.
-
#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
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/wikk_configuration.rb', line 16 def initialize(filename="#{File.dirname(__FILE__)}/../conf/config.json") if filename.class == Hash @filename = nil @pjson = filename else @filename = filename json = File.read(filename) @pjson = JSON.parse(json) end 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
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/wikk_configuration.rb', line 42 def method_missing(symbol , *args, &block) s = symbol.to_s if @pjson[s] != nil return @pjson[s] elsif s[-1,1] == "=" @pjson[s[0..-2]] = args[0] else super end end |
Instance Attribute Details
#pjson ⇒ Object
Returns the value of attribute pjson.
11 12 13 |
# File 'lib/wikk_configuration.rb', line 11 def pjson @pjson 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
32 33 34 |
# File 'lib/wikk_configuration.rb', line 32 def respond_to?(symbol, include_private = false) (@pjson[s = symbol.to_s] != nil) || (s[-1,1] == '=' && @pjson[s[0..-2]] != nil) || super(symbol, include_private) end |
#save(filename = nil) ⇒ Object
Write Json config file. Either over the original, or a new file.
55 56 57 58 59 60 61 62 |
# File 'lib/wikk_configuration.rb', line 55 def save(filename = nil) filename ||= @filename if filename != nil File.open(filename , "w+") do |fd| fd.write(@pjson.to_j) end end end |
#to_s ⇒ String
Returns the configuration.
65 66 67 |
# File 'lib/wikk_configuration.rb', line 65 def to_s @pjson.to_s end |