Class: Chatterbot::ConfigManager

Inherits:
Object
  • Object
show all
Defined in:
lib/chatterbot/config_manager.rb

Overview

wrap YAML::Store to maintain config but have a few read-only variables which we will never set/override

Constant Summary collapse

READ_ONLY_VARIABLES =

list of vars that shouldn’t ever be written

[:consumer_key, :consumer_secret, :access_token, :access_token_secret, :log_dest]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dest, read_only = {}, no_update = false) ⇒ ConfigManager

Returns a new instance of ConfigManager.



16
17
18
19
20
# File 'lib/chatterbot/config_manager.rb', line 16

def initialize(dest, read_only={}, no_update=false)
  @read_only = read_only
  @store = YAML::Store.new(dest, true)
  @no_update = no_update
end

Instance Attribute Details

#no_updateObject

if true, we will never actually update the config file



14
15
16
# File 'lib/chatterbot/config_manager.rb', line 14

def no_update
  @no_update
end

Instance Method Details

#[](key) ⇒ Object

retrieve a key



45
46
47
48
49
50
51
52
# File 'lib/chatterbot/config_manager.rb', line 45

def [](key)
  if READ_ONLY_VARIABLES.include?(key) && @read_only[key]
    return @read_only[key]
  end
  @store.transaction do
    @store[key]
  end
end

#[]=(key, value) ⇒ Object

set/update a key



37
38
39
40
41
42
# File 'lib/chatterbot/config_manager.rb', line 37

def []=(key, value)
  return if @no_update == true
  @store.transaction do
    @store[key] = value
  end
end

#delete(key) ⇒ Object

delete a key from the config



23
24
25
26
27
28
# File 'lib/chatterbot/config_manager.rb', line 23

def delete(key)
  return if @no_update == true
  @store.transaction do
    @store.delete(key)
  end
end

#to_hObject



30
31
32
33
34
# File 'lib/chatterbot/config_manager.rb', line 30

def to_h
  @store.transaction do
    Hash[@store.roots.map { |k| [k, @store[k]] }]
  end
end