Class: Jabbot::Config

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

Overview

Jabbot configuration. Use either Jabbot::CliConfig.new or JabbotFileConfig.new setup a new bot from either command line or file (respectively). Configurations can be chained so they override each other:

config = Jabbot::FileConfig.new
config << Jabbot::CliConfig.new
config.to_hash

The preceding example will create a configuration which is based on a configuration file but have certain values overridden from the command line. This can be used for instance to store everything but the Twitter account password in your configuration file. Then you can just provide the password when running the bot.

Direct Known Subclasses

FileConfig

Constant Summary collapse

DEFAULT =
{
  :log_level => 'info',
  :log_file => nil,
  :login => nil,
  :password => nil,
  :nick => 'jabbot',
  :channel => nil,
  :server => nil,
  :resource => nil,
  :debug => false
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ Config

Returns a new instance of Config.



33
34
35
36
# File 'lib/jabbot/config.rb', line 33

def initialize(settings = {})
  @configs = []
  @settings = settings
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Internal: Maps calls to non existant functions to

configuration values, if they exist.

name, *args and &block as described in the core classes.

Returns the configuration value if any.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jabbot/config.rb', line 55

def method_missing(name, *args, &block)
  regex = /=$/
  attr_name = name.to_s.sub(regex, '').to_sym
  return super if name == attr_name && !@settings.key?(attr_name)

  if name != attr_name
    @settings[attr_name] = args.first
  end

  @settings[attr_name]
end

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings.



19
20
21
# File 'lib/jabbot/config.rb', line 19

def settings
  @settings
end

Class Method Details

.defaultObject



76
77
78
# File 'lib/jabbot/config.rb', line 76

def self.default
  Config.new({}.merge(DEFAULT))
end

Instance Method Details

#add(config) ⇒ Object Also known as: <<

Public: Add a configuration object to override given settings

config -

Returns the class object.



43
44
45
46
# File 'lib/jabbot/config.rb', line 43

def add(config)
  @configs << config
  self
end

#to_hashObject

Public: Merges configurations and returns a hash with all options

Returns a Hash of the configuration.



70
71
72
73
74
# File 'lib/jabbot/config.rb', line 70

def to_hash
  hash = {}.merge(@settings)
  @configs.each { |conf| hash.merge!(conf.to_hash) }
  hash
end