Class: Rodbot::Config::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/rodbot/config.rb

Instance Method Summary collapse

Constructor Details

#initializeReader

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

Parameters:

  • key (Symbol)

    config key

  • value (Object, nil) (defaults to: nil)

    config value

Yields:

  • optional block containing nested config

Returns:

  • (self)


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

Yields:

  • block to evaluate

Returns:

  • (self)


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

Parameters:

  • strings (String, nil)

    one or more strings to evaluate

Returns:

  • (self)


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_hHash

Config hash

Returns:

  • (Hash)


156
157
158
# File 'lib/rodbot/config.rb', line 156

def to_h
  @hash
end