Class: Verquest::Properties::Const

Inherits:
Base
  • Object
show all
Defined in:
lib/verquest/properties/const.rb

Overview

The Const class represents a constant property with a fixed value in a JSON schema. It’s used for properties that must have a specific, immutable value.

Examples:

const = Const.new(name: "type", value: "user")

Instance Attribute Summary collapse

Attributes inherited from Base

#map, #name, #nullable, #required

Instance Method Summary collapse

Methods inherited from Base

#add, #mapping_value_key, #mapping_value_prefix, #to_validation_schema

Methods included from HelperMethods::RequiredProperties

#dependent_required_properties, #required_properties

Constructor Details

#initialize(name:, value:, map: nil, required: false, **schema_options) ⇒ Const

Initialize a new constant property

Parameters:

  • name (String, Symbol)

    The name of the constant property

  • value (Object)

    The fixed value of the constant (can be any scalar value)

  • map (Object, nil) (defaults to: nil)

    Optional mapping information

  • required (Boolean, Array<Symbol>) (defaults to: false)

    Whether this property is required, or array of dependency names (can be overridden by custom type)

  • schema_options (Hash)

    Additional JSON schema options for this property



18
19
20
21
22
23
24
# File 'lib/verquest/properties/const.rb', line 18

def initialize(name:, value:, map: nil, required: false, **schema_options)
  @name = name.to_s
  @value = value
  @map = map
  @required = required
  @schema_options = schema_options&.transform_keys(&:to_s)
end

Instance Attribute Details

#schema_optionsObject (readonly, private)

Returns the value of attribute schema_options.



50
51
52
# File 'lib/verquest/properties/const.rb', line 50

def schema_options
  @schema_options
end

#valueObject (readonly, private)

Returns the value of attribute value.



50
51
52
# File 'lib/verquest/properties/const.rb', line 50

def value
  @value
end

Instance Method Details

#mapping(key_prefix:, value_prefix:, mapping:, version: nil) ⇒ Hash

Create mapping for this const property

Parameters:

  • key_prefix (Array<Symbol>)

    Prefix for the source key

  • value_prefix (Array<String>)

    Prefix for the target value

  • mapping (Hash)

    The mapping hash to be updated

  • version (String, nil) (defaults to: nil)

    The version to create mapping for

Returns:

  • (Hash)

    The updated mapping hash



44
45
46
# File 'lib/verquest/properties/const.rb', line 44

def mapping(key_prefix:, value_prefix:, mapping:, version: nil)
  mapping[(key_prefix + [name]).join("/")] = mapping_value_key(value_prefix:)
end

#to_schemaHash

Generate JSON schema definition for this constant

Returns:

  • (Hash)

    The schema definition for this constant



29
30
31
32
33
34
35
# File 'lib/verquest/properties/const.rb', line 29

def to_schema
  {
    name => {
      "const" => value
    }.merge(schema_options)
  }
end