Module: ElasticGraph::Support::Config::ClassMethods
- Includes:
- FromYamlFile
- Defined in:
- lib/elastic_graph/support/config.rb
Overview
Defines class methods for configuration classes.
Instance Attribute Summary collapse
-
#path ⇒ ::String
readonly
Path from the global configuration root to where this configuration resides.
-
#required ⇒ ::Boolean
readonly
Whether this configuration property is required.
-
#validator ⇒ Support::JSONSchema::Validator
readonly
Validator for this configuration class.
Instance Method Summary collapse
-
#from_parsed_yaml(parsed_yaml) ⇒ ::Data?
Instantiates a config instance from the given parsed YAML class, returning ‘nil` if there is no config.
-
#from_parsed_yaml!(parsed_yaml) ⇒ ::Data
Instantiates a config instance from the given parsed YAML class, raising an error if there is no config.
-
#json_schema(at:, optional:, **schema) ⇒ void
Defines the JSON schema and path for this configuration class.
-
#new_without_validation(**data) ⇒ Object
Like ‘new`, but avoids applying JSON schema validation.
- #raise_invalid_config(error) ⇒ Object
Methods included from FromYamlFile
Instance Attribute Details
#path ⇒ ::String (readonly)
Returns path from the global configuration root to where this configuration resides.
67 68 69 |
# File 'lib/elastic_graph/support/config.rb', line 67 def path @path end |
#required ⇒ ::Boolean (readonly)
Returns whether this configuration property is required.
70 71 72 |
# File 'lib/elastic_graph/support/config.rb', line 70 def required @required end |
#validator ⇒ Support::JSONSchema::Validator (readonly)
Returns validator for this configuration class.
64 65 66 |
# File 'lib/elastic_graph/support/config.rb', line 64 def validator @validator end |
Instance Method Details
#from_parsed_yaml(parsed_yaml) ⇒ ::Data?
Instantiates a config instance from the given parsed YAML class, returning ‘nil` if there is no config. In addition, this (along with `Support::FromYamlFile`) makes `from_yaml_file(path_to_file)` available.
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/elastic_graph/support/config.rb', line 116 def from_parsed_yaml(parsed_yaml) value_at_path = Support::HashUtil.fetch_value_at_path(parsed_yaml, path.split(".")) { return nil } if value_at_path.is_a?(::Hash) config = (_ = value_at_path).transform_keys(&:to_sym) # : ::Hash[::Symbol, untyped] new(**config) else raise_invalid_config("Expected a hash at `#{path}`, got: `#{value_at_path.inspect}`.") end end |
#from_parsed_yaml!(parsed_yaml) ⇒ ::Data
Instantiates a config instance from the given parsed YAML class, raising an error if there is no config.
132 133 134 |
# File 'lib/elastic_graph/support/config.rb', line 132 def from_parsed_yaml!(parsed_yaml) from_parsed_yaml(parsed_yaml) || raise_invalid_config("missing configuration at `#{path}`.") end |
#json_schema(at:, optional:, **schema) ⇒ void
This method returns an undefined value.
Defines the JSON schema and path for this configuration class.
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/elastic_graph/support/config.rb', line 100 def json_schema(at:, optional:, **schema) @path = at @required = !optional schema = Support::HashUtil.stringify_keys(schema) @validator = Support::JSONSchema::ValidatorFactory .new(schema: {"$schema" => "http://json-schema.org/draft-07/schema#", "$defs" => {"config" => schema}}, sanitize_pii: false) .with_unknown_properties_disallowed .validator_for("config") end |
#new_without_validation(**data) ⇒ Object
Like ‘new`, but avoids applying JSON schema validation. This is needed so that we can make `#with` work correctly with the validation and conversion features we offer.
145 146 147 148 149 |
# File 'lib/elastic_graph/support/config.rb', line 145 def new_without_validation(**data) instance = allocate instance.send(:__data_initialize, **data) instance end |
#raise_invalid_config(error) ⇒ Object
137 138 139 |
# File 'lib/elastic_graph/support/config.rb', line 137 def raise_invalid_config(error) raise Errors::ConfigError, "Invalid configuration for `#{name}` at `#{path}`: #{error}" end |