Module: ElasticGraph::Support::Config::ClassMethods

Defined in:
elasticgraph-support/lib/elastic_graph/support/config.rb

Overview

Defines class methods for configuration classes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#path::String (readonly)

Returns path from the global configuration root to where this configuration resides.

Returns:

  • (::String)

    path from the global configuration root to where this configuration resides.



66
67
68
# File 'elasticgraph-support/lib/elastic_graph/support/config.rb', line 66

def path
  @path
end

#required::Boolean (readonly)

Returns whether this configuration property is required.

Returns:

  • (::Boolean)

    whether this configuration property is required



69
70
71
# File 'elasticgraph-support/lib/elastic_graph/support/config.rb', line 69

def required
  @required
end

#validatorSupport::JSONSchema::Validator (readonly)

Returns validator for this configuration class.

Returns:



63
64
65
# File 'elasticgraph-support/lib/elastic_graph/support/config.rb', line 63

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.

Parameters:

  • parsed_yaml (::Hash<::String, ::Object>)

    config hash parsed from YAML

Returns:

  • (::Data, nil)

    the instantiated config object or nil if there is nothing at the specified path



115
116
117
118
119
120
121
122
123
124
# File 'elasticgraph-support/lib/elastic_graph/support/config.rb', line 115

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.

Parameters:

  • parsed_yaml (::Hash<::String, ::Object>)

    config hash parsed from YAML

Returns:

  • (::Data)

    the instantiated config object

Raises:

  • (Errors::ConfigError)

    if there is no config at the specified path.



131
132
133
# File 'elasticgraph-support/lib/elastic_graph/support/config.rb', line 131

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.

Examples:

Define a configuration class

require "elastic_graph/support/config"

ExampleConfigClass = ElasticGraph::Support::Config.define(:size, :name) do
  json_schema at: "example", optional: false,
    properties: {
      size: {
        description: "Determines the size.",
        examples: [10, 100],
        type: "integer",
        minimum: 1,
      },
      name: {
        description: "Determines the name.",
        examples: ["widget"],
        type: "string",
        minLength: 1
      }
    },
    required: ["size", "name"]
end

Parameters:

  • at (::String)

    path from the global configuration root to where this configuration resides

  • optional (::Boolean)

    whether configuration at the provided path is optional

  • schema (::Hash<::Symbol, ::Object>)

    JSON schema definition



99
100
101
102
103
104
105
106
107
108
# File 'elasticgraph-support/lib/elastic_graph/support/config.rb', line 99

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