Class: RSMP::Options
- Inherits:
-
Object
show all
- Defined in:
- lib/rsmp/options/options.rb
Overview
Base class for configuration options.
Constant Summary
collapse
- SCHEMAS_PATH =
File.expand_path('schemas', __dir__)
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(options = nil, source: nil, log_settings: nil, validate: true, **extra) ⇒ Options
Returns a new instance of Options.
30
31
32
33
34
35
36
37
38
|
# File 'lib/rsmp/options/options.rb', line 30
def initialize(options = nil, source: nil, log_settings: nil, validate: true, **)
options = if options.nil? && .any?
@source = source
@log_settings = normalize(log_settings || {})
validate_log_settings! if validate
config = normalize_config(options || {})
validate!(config) if validate
@data = normalize(apply_defaults(config))
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
9
10
11
|
# File 'lib/rsmp/options/options.rb', line 9
def data
@data
end
|
#log_settings ⇒ Object
Returns the value of attribute log_settings.
9
10
11
|
# File 'lib/rsmp/options/options.rb', line 9
def log_settings
@log_settings
end
|
#source ⇒ Object
Returns the value of attribute source.
9
10
11
|
# File 'lib/rsmp/options/options.rb', line 9
def source
@source
end
|
Class Method Details
.load_file(path, validate: true) ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/rsmp/options/options.rb', line 11
def self.load_file(path, validate: true)
raise RSMP::ConfigurationError, "Config #{path} not found" unless File.exist?(path)
raise RSMP::ConfigurationError, "Config #{path} is not a file" unless File.file?(path)
raise RSMP::ConfigurationError, "Config #{path} must be a YAML file (.yml or .yaml)" unless yaml_file?(path)
raw = YAML.load_file(path)
raise RSMP::ConfigurationError, "Config #{path} must be a hash" unless raw.is_a?(Hash) || raw.nil?
raw ||= {}
log_settings = raw.delete('log') || {}
new(raw, source: path, log_settings: log_settings, validate: validate)
rescue Psych::SyntaxError => e
raise RSMP::ConfigurationError, "Cannot read config file #{path}: #{e}"
end
|
.yaml_file?(path) ⇒ Boolean
26
27
28
|
# File 'lib/rsmp/options/options.rb', line 26
def self.yaml_file?(path)
%w[.yml .yaml].include?(File.extname(path).downcase)
end
|
Instance Method Details
#[](key) ⇒ Object
79
80
81
|
# File 'lib/rsmp/options/options.rb', line 79
def [](key)
@data[key]
end
|
#defaults ⇒ Object
40
|
# File 'lib/rsmp/options/options.rb', line 40
def defaults = {}
|
#dig(*path, default: nil, assume: nil) ⇒ Object
70
71
72
73
74
75
76
77
|
# File 'lib/rsmp/options/options.rb', line 70
def dig(*path, default: nil, assume: nil)
value = @data.dig(*path)
return value unless value.nil?
return default unless default.nil?
return assume unless assume.nil?
raise RSMP::ConfigurationError, "Config #{path.inspect} is missing"
end
|
#log_schema_path ⇒ Object
46
|
# File 'lib/rsmp/options/options.rb', line 46
def log_schema_path = File.join(SCHEMAS_PATH, 'log.json')
|
#schema_file ⇒ Object
42
|
# File 'lib/rsmp/options/options.rb', line 42
def schema_file = nil
|
#schema_path ⇒ Object
44
|
# File 'lib/rsmp/options/options.rb', line 44
def schema_path = schema_file && File.join(SCHEMAS_PATH, schema_file)
|
#to_h ⇒ Object
83
84
85
|
# File 'lib/rsmp/options/options.rb', line 83
def to_h
@data
end
|
#validate!(data = @data) ⇒ Object
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/rsmp/options/options.rb', line 59
def validate!(data = @data)
return unless schema_path && File.exist?(schema_path)
schemer = JSONSchemer.schema(Pathname.new(schema_path))
errors = schemer.validate(data).to_a
return if errors.empty?
message = errors.map { |error| format_error(error) }.join("\n")
raise RSMP::ConfigurationError, "Invalid configuration#{source_suffix}:\n#{message}"
end
|
#validate_log_settings! ⇒ Object
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/rsmp/options/options.rb', line 48
def validate_log_settings!
return unless File.exist?(log_schema_path)
schemer = JSONSchemer.schema(Pathname.new(log_schema_path))
errors = schemer.validate(@log_settings).to_a
return if errors.empty?
message = errors.map { |error| format_error(error) }.join("\n")
raise RSMP::ConfigurationError, "Invalid log configuration#{source_suffix}:\n#{message}"
end
|