Class: PDK::Config::JSONSchemaSetting

Inherits:
Setting
  • Object
show all
Defined in:
lib/pdk/config/json_schema_setting.rb

Instance Attribute Summary

Attributes inherited from Setting

#namespace, #previous_setting

Instance Method Summary collapse

Methods inherited from Setting

#default_to, #qualified_name, #to_s, #validate, #value, #value=

Constructor Details

#initialize(_name, namespace, _initial_value) ⇒ JSONSchemaSetting

Initialises the PDK::Config::JSONSchemaSetting object.

See Also:

  • Setting.initialize


9
10
11
12
# File 'lib/pdk/config/json_schema_setting.rb', line 9

def initialize(_name, namespace, _initial_value)
  raise 'The JSONSchemaSetting object can only be created within the JSONSchemaNamespace' unless namespace.is_a?(PDK::Config::JSONSchemaNamespace)
  super
end

Instance Method Details

#defaultObject?

Evaluate the default setting, firstly from the JSON schema and then from any other default evaluators in the settings chain.

Returns:

  • (Object, nil)

    the result of evaluating the block given to Setting#default_to, or ‘nil` if the setting has no default.

See Also:

  • Setting.default


41
42
43
44
45
46
47
48
49
50
# File 'lib/pdk/config/json_schema_setting.rb', line 41

def default
  # Return the default from the schema document if it exists
  if namespace.schema_property_names.include?(@name)
    prop_schema = namespace.schema['properties'][@name]
    return prop_schema['default'] unless prop_schema['default'].nil?
  end
  # ... otherwise call the settings chain default
  # and if that doesn't exist, just return nil
  @previous_setting.nil? ? nil : @previous_setting.default
end

#validate!(value) ⇒ Object

Verifies that the new setting value is valid by calling the JSON schema validator on a hash which includes the new setting

See Also:

  • Setting.validate!


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pdk/config/json_schema_setting.rb', line 18

def validate!(value)
  # Get the existing namespace data
  new_document = namespace.to_h
  # ... set the new value
  new_document[@name] = value
  begin
    # ... add validate it
    namespace.validate_document!(new_document)
  rescue ::JSON::Schema::ValidationError => e
    raise ArgumentError, _('%{key} %{message}') % {
      key:     qualified_name,
      message: e.message,
    }
  end
end