Class: Rodbot::Config::Reader
- Inherits:
-
Object
- Object
- Rodbot::Config::Reader
- Defined in:
- lib/rodbot/config.rb
Instance Method Summary collapse
-
#eval_block { ... } ⇒ self
Eval configuration from block.
-
#eval_strings(*strings) ⇒ self
Eval configuration from strings.
-
#initialize ⇒ Reader
constructor
A new instance of Reader.
-
#method_missing(key, value = nil) { ... } ⇒ self
Set an config value.
-
#to_h ⇒ Hash
Config hash.
Constructor Details
#initialize ⇒ Reader
Returns a new instance of Reader.
109 110 111 |
# File 'lib/rodbot/config.rb', line 109 def initialize @hash = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(key, value = nil) { ... } ⇒ self
Set an config value
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/rodbot/config.rb', line 137 def method_missing(key, value=nil, *, &block) case when block && value.nil? @hash[key] ||= {} @hash[key].merge! self.class.new.eval_block(&block).to_h when block || KEYS_WITH_IMPLICIT_BLOCK.include?(key) @hash[key] ||= {} @hash[key][value] = self.class.new.eval_block(&block).to_h else @hash[key] = value end self end |
Instance Method Details
#eval_block { ... } ⇒ self
Eval configuration from block
126 127 128 129 |
# File 'lib/rodbot/config.rb', line 126 def eval_block(&block) instance_eval(&block) if block self end |
#eval_strings(*strings) ⇒ self
Eval configuration from strings
117 118 119 120 |
# File 'lib/rodbot/config.rb', line 117 def eval_strings(*strings) instance_eval(strings.compact.join("\n")) self end |
#to_h ⇒ Hash
Config hash
154 155 156 |
# File 'lib/rodbot/config.rb', line 154 def to_h @hash end |