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)


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

Yields:

  • block to evaluate

Returns:

  • (self)


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

Parameters:

  • strings (String, nil)

    one or more strings to evaluate

Returns:

  • (self)


117
118
119
120
# File 'lib/rodbot/config.rb', line 117

def eval_strings(*strings)
  instance_eval(strings.compact.join("\n"))
  self
end

#to_hHash

Config hash

Returns:

  • (Hash)


154
155
156
# File 'lib/rodbot/config.rb', line 154

def to_h
  @hash
end