Class: Chook::Configuration

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

Overview

The configuration object

Constant Summary collapse

DEFAULT_CONF_FILE =

The location of the default config file

Pathname.new '/etc/chook.conf'
CONF_KEYS =

The attribute keys we maintain, and the type they should be stored as

{
  server_port: :to_i,
  server_engine:  :to_sym,
  handler_dir: nil,
  use_ssl: Chook::Procs::STRING_TO_BOOLEAN,
  ssl_private_key_path: Chook::Procs::STRING_TO_PATHNAME,
  ssl_private_key_pw_path: nil,
  ssl_cert_path: Chook::Procs::STRING_TO_PATHNAME,
  ssl_cert_name: nil
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Initialize!


67
68
69
# File 'lib/chook/configuration.rb', line 67

def initialize
  read_global
end

Instance Method Details

#clear_allvoid

This method returns an undefined value.

Clear all values


78
79
80
# File 'lib/chook/configuration.rb', line 78

def clear_all
  CONF_KEYS.keys.each { |k| send "#{k}=".to_sym, nil }
end

This method returns an undefined value.

Print out the current settings to stdout


147
148
149
# File 'lib/chook/configuration.rb', line 147

def print
  CONF_KEYS.keys.sort.each { |k| puts "#{k}: #{send k}" }
end

#read_globalBoolean

(Re)read the global prefs, if it exists.


86
87
88
89
# File 'lib/chook/configuration.rb', line 86

def read_global
  return false unless DEFAULT_CONF_FILE.file? && DEFAULT_CONF_FILE.readable?
  read DEFAULT_CONF_FILE
end

#reload(file = DEFAULT_CONF_FILE) ⇒ Boolean

Clear the settings and reload the prefs file, or another file if provided


97
98
99
100
101
102
# File 'lib/chook/configuration.rb', line 97

def reload(file = DEFAULT_CONF_FILE)
  file = Pathname.new file
  return false unless file.file? && file.readable?
  clear_all
  read file
end

#save(file) ⇒ void

This method returns an undefined value.

Save the prefs into a file


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chook/configuration.rb', line 110

def save(file)
  path = Pathname.new(file)

  # file already exists? read it in and update the values.
  # Don't overwrite it, since the user might have comments
  # in there.
  if path.readable?
    data = path.read

    # go thru the known attributes/keys
    CONF_KEYS.keys.sort.each do |k|
      # if the key exists, update it.
      if data =~ /^#{k}:/
        data.sub!(/^#{k}:.*$/, "#{k}: #{send k}")

      # if not, add it to the end unless it's nil
      else
        data += "\n#{k}: #{send k}" unless send(k).nil?
      end # if data =~ /^#{k}:/
    end # each do |k|

  else # not readable, make a new file
    data = ''
    CONF_KEYS.keys.sort.each do |k|
      data << "#{k}: #{send k}\n" unless send(k).nil?
    end
  end # if path readable

  # make sure we end with a newline, the save it.
  data << "\n" unless data.end_with?("\n")
  path.open('w') { |f| f.write data }
end