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
139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/rodbot/config.rb', line 139 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
128 129 130 131 |
# File 'lib/rodbot/config.rb', line 128 def eval_block(&block) instance_eval(&block) if block self end |
#eval_strings(*strings) ⇒ self
Eval configuration from strings
117 118 119 120 121 122 |
# File 'lib/rodbot/config.rb', line 117 def eval_strings(*strings) # TODO: filter magic comment until they are no longer requred to prevent warning strings = strings.map { _1&.sub(/# frozen_string_literal: true/, '') } instance_eval(strings.compact.join("\n")) self end |
#to_h ⇒ Hash
Config hash
156 157 158 |
# File 'lib/rodbot/config.rb', line 156 def to_h @hash end |