Class: Codebot::Config

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

Overview

This class manages a Codebot configuration file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(core, file = nil) ⇒ Config

Creates a new instance of the class and loads the configuration file.

Parameters:

  • core (Core)

    the bot this configuration belongs to

  • file (String) (defaults to: nil)

    the path to the configuration file, or nil to use the default configuration file for this user



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

#coreCore (readonly)

Returns the bot this configuration belongs to.

Returns:

  • (Core)

    the bot this configuration belongs to



13
14
15
# File 'lib/codebot/config.rb', line 13

def core
  @core
end

#fileString (readonly)

Returns the path to the managed configuration file.

Returns:

  • (String)

    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_fileString

Returns the path to the default configuration file for the current user.

Returns:

  • (String)

    the path to the configuration file



69
70
71
# File 'lib/codebot/config.rb', line 69

def self.default_file
  File.join Dir.home, '.codebot.yml'
end

Instance Method Details

#integrationsArray

Returns the integrations contained in this configuration.

Returns:

  • (Array)

    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

#networksArray

Returns the networks contained in this configuration.

Returns:

  • (Array)

    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.

Yields:

  • invokes the given block during the transaction

Returns:

  • (true)

    if the transaction completes successfully

Raises:

  • (StandardError)

    the error that occurred during modification



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