Class: Codebot::Config
- Inherits:
-
Object
- Object
- Codebot::Config
- Defined in:
- lib/codebot/config.rb
Overview
This class manages a Codebot configuration file.
Instance Attribute Summary collapse
-
#core ⇒ Core
readonly
The bot this configuration belongs to.
-
#file ⇒ String
readonly
The path to the managed configuration file.
Class Method Summary collapse
-
.default_file ⇒ String
Returns the path to the default configuration file for the current user.
Instance Method Summary collapse
-
#initialize(core, file = nil) ⇒ Config
constructor
Creates a new instance of the class and loads the configuration file.
-
#integrations ⇒ Array
The integrations contained in this configuration.
-
#load! ⇒ Object
Loads the configuration from the associated file.
-
#networks ⇒ Array
The networks contained in this configuration.
-
#transaction { ... } ⇒ true
A thread-safe method for making changes to the configuration.
Constructor Details
#initialize(core, file = nil) ⇒ Config
Creates a new instance of the class and loads the configuration file.
23 24 25 26 27 28 |
# File 'lib/codebot/config.rb', line 23 def initialize(core, file = nil) @core = core @file = file || self.class.default_file @semaphore = Mutex.new unsafe_load end |
Instance Attribute Details
#core ⇒ Core (readonly)
Returns the bot this configuration belongs to.
13 14 15 |
# File 'lib/codebot/config.rb', line 13 def core @core end |
#file ⇒ String (readonly)
Returns the path to the managed configuration file.
16 17 18 |
# File 'lib/codebot/config.rb', line 16 def file @file end |
Class Method Details
.default_file ⇒ String
Returns the path to the default configuration file for the current user.
69 70 71 |
# File 'lib/codebot/config.rb', line 69 def self.default_file File.join Dir.home, '.codebot.yml' end |
Instance Method Details
#integrations ⇒ Array
Returns the integrations contained in this configuration.
57 58 59 |
# File 'lib/codebot/config.rb', line 57 def integrations @conf[:integrations] end |
#load! ⇒ Object
Loads the configuration from the associated file. If the file does not exist, it is created.
32 33 34 |
# File 'lib/codebot/config.rb', line 32 def load! transaction { unsafe_load } end |
#networks ⇒ Array
Returns the networks contained in this configuration.
62 63 64 |
# File 'lib/codebot/config.rb', line 62 def networks @conf[:networks] end |
#transaction { ... } ⇒ true
A thread-safe method for making changes to the configuration. If another transaction is active, the calling thread waits for it to complete. If a StandardError
occurs during the transaction, the configuration is rolled back to the previous state.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/codebot/config.rb', line 44 def transaction @semaphore.synchronize do state = @conf begin run_transaction(&Proc.new) rescue StandardError @conf = state raise end end end |