Class: RBarman::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/rbarman/configuration.rb

Overview

A very flexible configuration class, to be used as Singleton

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Configuration

Creates a new instance of RBarman::Configuration

Parameters:

  • (defaults to: {})

    added as configuration parameters



11
12
13
14
15
# File 'lib/rbarman/configuration.rb', line 11

def initialize(data={})
  @data = {}
  update!(data)
  basic_configuration
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object, ...

For catching [NoMethodError] and trying to add a new parameter or returning a parameter value, based on the “missing” method name

Examples:

Configuration.instance[:some_key] = "some_value"
Configuration.instance.some_key #=> "some_value"
Configuration.instance.another_key = { :a => 1 }
Configuration.instance[:another_key] #=> { :a => 1}
Configuration.instance[:missing] #=> nil

Parameters:

  • the method name

  • the arguments passed to the method.

Returns:

  • when method name doesn’t end with an ‘=` operator, a parameter value will be returned (if found, otherwise nil)



55
56
57
58
59
60
61
# File 'lib/rbarman/configuration.rb', line 55

def method_missing(sym, *args)
  if sym.to_s =~ /(.+)=$/
    self[$1] = args.first
  else
    self[sym]
  end
end

Instance Method Details

#[](key) ⇒ Object?

Gives access to parameters

Parameters:

  • the key

Returns:

  • the value



29
30
31
# File 'lib/rbarman/configuration.rb', line 29

def [](key)
  @data[key.to_sym]
end

#[]=(key, value) ⇒ void

This method returns an undefined value.

Adds a new parameter

Parameters:

  • the key

  • the value



37
38
39
40
41
42
43
# File 'lib/rbarman/configuration.rb', line 37

def []=(key, value)
  if value.class == Hash
    @data[key.to_sym] = Config.new(value)
  else
    @data[key.to_sym] = value
  end
end

#basic_configurationvoid

This method returns an undefined value.

adds :binary with path to barman binary as value and :barman_home (default $HOME) with path to barman’s backup base directory as value. If which reports a path for barman, that path will be used, otherwise /usr/bin/barman

Examples:

Configuration.Instance.binary #=> "/usr/bin/barman"
Configuration.Instance.barman_home #=> "/var/lib/barman"


68
69
70
71
72
# File 'lib/rbarman/configuration.rb', line 68

def basic_configuration
  b_path = `which barman`.chomp
  self[:binary] = b_path.empty? ? '/usr/bin/barman' : b_path
  self[:barman_home] = ENV['HOME']
end

#update!(data) ⇒ void

This method returns an undefined value.

Adds parameters

Parameters:

  • parameters to add



20
21
22
23
24
# File 'lib/rbarman/configuration.rb', line 20

def update!(data)
  data.each do |key, value|
    self[key] = value
  end
end