Class: Syndi::Config

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

Overview

A class which provides a functional, simple configuration interface. It uses YAML by means of Psych.

Author:

  • noxgirl

  • swarley

Since:

  • 4.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath) ⇒ Config

Produce a new instance, and attempt to parse.

Parameters:

  • filepath (String)

    Path to configuration file.

Since:

  • 4.0.0



30
31
32
33
34
35
# File 'lib/syndi/config.rb', line 30

def initialize filepath
  $m.verbose("Trying to initialize configuration from '#{filepath}'...", VSIMPLE) do
    @path = filepath
    parse
  end # verbose
end

Instance Attribute Details

#confHash{}

Returns This is the hash which contains the data parsed from the configuration file.

Returns:

  • (Hash{})

    This is the hash which contains the data parsed from the configuration file.

See Also:

Since:

  • 4.0.0



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/syndi/config.rb', line 23

class Config
  
  attr_reader :conf, :type

  # Produce a new instance, and attempt to parse.
  #
  # @param [String] filepath Path to configuration file.
  def initialize filepath
    $m.verbose("Trying to initialize configuration from '#{filepath}'...", VSIMPLE) do
      @path = filepath
      parse
    end # verbose
  end

  # Rehash the configuration.
  #
  # If an error occurs, it will revert the configuration to its prior state
  # so that everything can continue to function.
  def rehash!
    
    $m.debug("Configuration file is rehashing.")

    # Keep the old configuration in case of issues.
    oldconf = @conf
    @conf   = {}

    # Rehash
    parse!

    # Ensure it really succeeded.
    if @conf.empty? or !@conf.instance_of? Hash
      # Nope. Restore old configuration.
      @conf = oldconf
      $m.error 'Failed to rehash the configuration file (parser produced empty config)! Reverting to old configuration.'
      return 0
    end
    
    $m.events.call :rehash

  # This rescue is applicable to anything that happens in here, since if it is reached, there really was an error in general.
  rescue => e 
    $m.error 'Failed to rehash configuration file! Reverting to old configuration.', false, e.backtrace
    @conf = oldconf
    return 0
  end

  # Return value of @conf[key].
  #
  # @return [Object] Value of @conf[key].
  # @see @conf
  def [] key
    @conf[key]
  end

  #######
  private
  #######

  # Parse the configuration file, and output the data to {#x}.
  #
  # @raise [ConfigError] If the file does not exist.
  # @raise [ConfigError] If the file cannot be processed.
  def parse

    # Ensure foremost that the configuration file exists.
    unless File.exists? @path
      raise ConfigError, "Configuration file '#@path' does not exist!"
    end

    # Get the data from the file.
    f    = File.open(@path)
    data = f.read
    f.close

    conf = {}
    # Process the YAML.
    begin
      conf = Psych.load data
    rescue => e
      raise ConfigError, "Failed to process the YAML in '#@path'", e.backtrace
    end

    @conf = conf

  end # def parse

end

#typeObject (readonly)

Since:

  • 4.0.0



25
26
27
# File 'lib/syndi/config.rb', line 25

def type
  @type
end

Instance Method Details

#[](key) ⇒ Object

Return value of @conf[key].

Returns:

  • (Object)

    Value of @conf[key].

See Also:

  • @conf

Since:

  • 4.0.0



73
74
75
# File 'lib/syndi/config.rb', line 73

def [] key
  @conf[key]
end

#parseObject (private)

Parse the configuration file, and output the data to #x.

Raises:

Since:

  • 4.0.0



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/syndi/config.rb', line 85

def parse

  # Ensure foremost that the configuration file exists.
  unless File.exists? @path
    raise ConfigError, "Configuration file '#@path' does not exist!"
  end

  # Get the data from the file.
  f    = File.open(@path)
  data = f.read
  f.close

  conf = {}
  # Process the YAML.
  begin
    conf = Psych.load data
  rescue => e
    raise ConfigError, "Failed to process the YAML in '#@path'", e.backtrace
  end

  @conf = conf

end

#rehash!Object

Rehash the configuration.

If an error occurs, it will revert the configuration to its prior state so that everything can continue to function.

Since:

  • 4.0.0



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/syndi/config.rb', line 41

def rehash!
  
  $m.debug("Configuration file is rehashing.")

  # Keep the old configuration in case of issues.
  oldconf = @conf
  @conf   = {}

  # Rehash
  parse!

  # Ensure it really succeeded.
  if @conf.empty? or !@conf.instance_of? Hash
    # Nope. Restore old configuration.
    @conf = oldconf
    $m.error 'Failed to rehash the configuration file (parser produced empty config)! Reverting to old configuration.'
    return 0
  end
  
  $m.events.call :rehash

# This rescue is applicable to anything that happens in here, since if it is reached, there really was an error in general.
rescue => e 
  $m.error 'Failed to rehash configuration file! Reverting to old configuration.', false, e.backtrace
  @conf = oldconf
  return 0
end