Class: Twibot::Config
- Inherits:
-
Object
- Object
- Twibot::Config
- Defined in:
- lib/twibot/config.rb
Overview
Twibot configuration. Use either Twibot::CliConfig.new or TwibotFileConfig.new setup a new bot from either command line or file (respectively). Configurations can be chained so they override each other:
config = Twibot::FileConfig.new
config << Twibot::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
Constant Summary collapse
- DEFAULT =
{ :host => "twitter.com", :min_interval => 30, :max_interval => 300, :interval_step => 10, :log_level => "info", :log_file => nil, :login => nil, :password => nil, :process => :new, :prompt => false, :daemonize => false, :include_friends => false, :timeline_for => :public }
Instance Attribute Summary collapse
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
Class Method Summary collapse
Instance Method Summary collapse
-
#add(config) ⇒ Object
(also: #<<)
Add a configuration object to override given settings.
-
#initialize(settings = {}) ⇒ Config
constructor
A new instance of Config.
-
#method_missing(name, *args, &block) ⇒ Object
Makes it possible to access configuration settings as attributes.
-
#to_hash ⇒ Object
Merges configurations and returns a hash with all options.
Constructor Details
#initialize(settings = {}) ⇒ Config
Returns a new instance of Config.
38 39 40 41 |
# File 'lib/twibot/config.rb', line 38 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
Makes it possible to access configuration settings as attributes
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/twibot/config.rb', line 56 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
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
20 21 22 |
# File 'lib/twibot/config.rb', line 20 def settings @settings end |
Class Method Details
Instance Method Details
#add(config) ⇒ Object Also known as: <<
Add a configuration object to override given settings
46 47 48 49 |
# File 'lib/twibot/config.rb', line 46 def add(config) @configs << config self end |
#to_hash ⇒ Object
Merges configurations and returns a hash with all options
71 72 73 74 75 |
# File 'lib/twibot/config.rb', line 71 def to_hash hash = {}.merge(@settings) @configs.each { |conf| hash.merge!(conf.to_hash) } hash end |