Sha256: 4937854e121a84fb86920729269c802a3721cc79a4854ebf682a6c3f3925c476

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

module Chatterbot
  require 'yaml/store'

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

    # list of vars that shouldn't ever be written
    READ_ONLY_VARIABLES = [:consumer_key, :consumer_secret, :access_token, :access_token_secret, :log_dest]

    # if true, we will never actually update the config file
    attr_accessor :no_update

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

    # delete a key from the config
    def delete(key)
      return if @no_update == true
      @store.transaction do
        @store.delete(key)
      end
    end

    # set/update a key
    def []=(key, value)
      return if @no_update == true
      @store.transaction do
        @store[key] = value
      end
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chatterbot-2.0.0.pre lib/chatterbot/config_manager.rb