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.
24
25
26
27
28
29
30
31
|
# File 'lib/rsmp/options/options.rb', line 24
def initialize(options = nil, source: nil, log_settings: nil, validate: true, **)
options = if options.nil? && .any?
@source = source
@log_settings = normalize(log_settings || {})
normalized = normalize(options || {})
@data = apply_defaults(normalized)
validate! if validate
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
|
# 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)
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
|
Instance Method Details
#[](key) ⇒ Object
67
68
69
|
# File 'lib/rsmp/options/options.rb', line 67
def [](key)
@data[key]
end
|
#defaults ⇒ Object
33
34
35
|
# File 'lib/rsmp/options/options.rb', line 33
def defaults
{}
end
|
#dig(*path, default: nil, assume: nil) ⇒ Object
58
59
60
61
62
63
64
65
|
# File 'lib/rsmp/options/options.rb', line 58
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
|
#schema_file ⇒ Object
37
38
39
|
# File 'lib/rsmp/options/options.rb', line 37
def schema_file
nil
end
|
#schema_path ⇒ Object
41
42
43
44
45
|
# File 'lib/rsmp/options/options.rb', line 41
def schema_path
return unless schema_file
File.join(SCHEMAS_PATH, schema_file)
end
|
#to_h ⇒ Object
71
72
73
|
# File 'lib/rsmp/options/options.rb', line 71
def to_h
@data
end
|
#validate! ⇒ Object
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/rsmp/options/options.rb', line 47
def validate!
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
|