Class: VScripts::Config

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

Overview

VScripts Configuration

Constant Summary collapse

DOT_FILE =

User’s configuration file

"#{File.expand_path('~')}/.vscripts.yml"
SYSTEM_CONFIG_FILE =

Global configuration file

'/etc/vscripts/config.yml'
GLOBAL_DEFAULTS =

Global defaults

{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = nil) ⇒ Hash

Loads class

Parameters:

  • cfg_file (String)

    the path to the configuration file



19
20
21
22
# File 'lib/vscripts/config.rb', line 19

def initialize(config_file = nil)
  @file = config_file
  @get  = GLOBAL_DEFAULTS.merge(options)
end

Instance Attribute Details

#getHash (readonly)

Returns all configuration options.

Returns:

  • (Hash)

    all configuration options



14
15
16
# File 'lib/vscripts/config.rb', line 14

def get
  @get
end

Instance Method Details

#check_config(file) ⇒ Boolean

Returns true if the file exists.

Parameters:

  • file (String)

    the path to the configuration file

Returns:

  • (Boolean)

    true if the file exists



48
49
50
# File 'lib/vscripts/config.rb', line 48

def check_config(file)
  file && File.exist?(file)
end

#optionsHash

Parses the configuration files in order

Returns:

  • (Hash)

    the first configuration hash found



26
27
28
29
30
31
# File 'lib/vscripts/config.rb', line 26

def options
  parse(@file) ||
    parse(DOT_FILE) ||
    parse(SYSTEM_CONFIG_FILE) ||
    {}
end

#parse(file) ⇒ Hash

Parses the configuration

Parameters:

  • file (String)

    the path to the configuration file

Returns:

  • (Hash)

    the configuration hash



36
37
38
# File 'lib/vscripts/config.rb', line 36

def parse(file)
  YAML.load(read(file)) if check_config(file)
end

#read(file) ⇒ String

Returns the contents of the file.

Parameters:

  • file (String)

    the path to the configuration file

Returns:

  • (String)

    the contents of the file



42
43
44
# File 'lib/vscripts/config.rb', line 42

def read(file)
  File.read(file)
end