Class: Hcheck::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/hcheck/configuration.rb,
lib/hcheck/configuration/service.rb

Overview

configuration class that loads configs via various ways initializes service classes from config

Defined Under Namespace

Classes: Service

Constant Summary collapse

DEFAULT_HCHECK_DIR =
Gem.loaded_specs['rails'] ? 'config/' : ''
DEFAULT_CONFIG_PATH =
[DEFAULT_HCHECK_DIR, 'hcheck.yml'].join

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Configuration

Returns a new instance of Configuration.



15
16
17
18
19
20
# File 'lib/hcheck/configuration.rb', line 15

def initialize(config)
  @services = config.map do |key, options|
    options = [options] unless options.is_a?(Array)
    options.map { |o| Service.new(key, o) }
  end.flatten
end

Instance Attribute Details

#servicesObject (readonly)

Returns the value of attribute services.



11
12
13
# File 'lib/hcheck/configuration.rb', line 11

def services
  @services
end

Class Method Details

.generate_configObject



45
46
47
48
# File 'lib/hcheck/configuration.rb', line 45

def generate_config
  FileUtils.copy_file(Gem.loaded_specs['hcheck'].gem_dir + '/hcheck.sample.yml', DEFAULT_CONFIG_PATH)
  puts "Generated #{DEFAULT_CONFIG_PATH}"
end

.load(config) ⇒ Object



23
24
25
# File 'lib/hcheck/configuration.rb', line 23

def load(config)
  Hcheck.configure config
end

.load_argv(args) ⇒ Object



27
28
29
# File 'lib/hcheck/configuration.rb', line 27

def load_argv(args)
  load_file(args[1].strip) if argv_config_present?(args)
end

.load_defaultObject



35
36
37
# File 'lib/hcheck/configuration.rb', line 35

def load_default
  load_file(DEFAULT_CONFIG_PATH)
end

.load_file(path) ⇒ Object



31
32
33
# File 'lib/hcheck/configuration.rb', line 31

def load_file(path)
  load read(path)
end

.read(path) ⇒ Object



39
40
41
42
43
# File 'lib/hcheck/configuration.rb', line 39

def read(path)
  YAML.safe_load(ERB.new(File.read(path)).result, [Symbol]) || {}
rescue StandardError => e
  raise Hcheck::Errors::ConfigurationError, e
end