Class: DotConfig::Configuration
- Inherits:
-
Object
- Object
- DotConfig::Configuration
- Defined in:
- lib/dot_config/configuration.rb
Overview
This class gives you access to the configuration with dot syntax.
Instance Attribute Summary collapse
-
#config ⇒ Hash
Stores the configuration like a
Hash
. -
#writing ⇒ Object
readonly
Returns the value of attribute writing.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ self
constructor
Class constructor.
-
#method_missing(name, *arguments) ⇒ Object
Post treatment of call method which have failed.
-
#to_hash ⇒ Hash
Returns the config like a
Hash
.
Constructor Details
#initialize(options = {}) ⇒ self
Class constructor.
30 31 32 33 34 35 |
# File 'lib/dot_config/configuration.rb', line 30 def initialize( = {}) raise ArgumentError, 'Invalid argument' if !.is_a?(Hash) @writing = [:writing] ? true : false self.config = [:config] if [:config].is_a?(Hash) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *arguments) ⇒ Object
Post treatment of call method which have failed.
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/dot_config/configuration.rb', line 60 def method_missing(name, *arguments) if name[-1, 1] == '=' && @config.key?(name[0..-2].to_sym) if @writing @config[name[0..-2].to_sym] = arguments.first else raise 'The configuration is not allowed in writing' end elsif @config.key?(name) @config[name] else super end end |
Instance Attribute Details
#config ⇒ Hash
Returns Stores the configuration like a Hash
.
25 26 27 |
# File 'lib/dot_config/configuration.rb', line 25 def config @config end |
#writing ⇒ Object (readonly)
Returns the value of attribute writing.
25 |
# File 'lib/dot_config/configuration.rb', line 25 attr_reader :config, :writing |
Instance Method Details
#to_hash ⇒ Hash
Returns the config like a Hash
.
77 78 79 80 81 |
# File 'lib/dot_config/configuration.rb', line 77 def to_hash hash = Hash.new @config.each { |k, v| hash[k] = v.is_a?(self.class) ? v.to_hash : v } hash end |