Class: Verquest::Properties::Base Abstract

Inherits:
Object
  • Object
show all
Includes:
HelperMethods::RequiredProperties
Defined in:
lib/verquest/properties/base.rb

Overview

This class is abstract.

Subclass and override #to_schema, #mapping to implement

Base class for all property types

This abstract class defines the interface for all property types in the Verquest schema system. All property classes should inherit from this base class and implement its required methods.

Direct Known Subclasses

Array, Collection, Const, Enum, Field, Object, Reference

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HelperMethods::RequiredProperties

#dependent_required_properties, #required_properties

Instance Attribute Details

#mapString?

Returns The mapping path for this property.

Returns:

  • (String, nil)

    The mapping path for this property



21
# File 'lib/verquest/properties/base.rb', line 21

attr_accessor :name, :required, :map

#nameString

Returns The name of the property.

Returns:

  • (String)

    The name of the property



21
22
23
# File 'lib/verquest/properties/base.rb', line 21

def name
  @name
end

#nullableObject (readonly, private)

Returns the value of attribute nullable.



62
63
64
# File 'lib/verquest/properties/base.rb', line 62

def nullable
  @nullable
end

#requiredBoolean

Returns Whether this property is required.

Returns:

  • (Boolean)

    Whether this property is required



21
# File 'lib/verquest/properties/base.rb', line 21

attr_accessor :name, :required, :map

Instance Method Details

#add(property) ⇒ Object

This method is abstract.

Adds a child property to this property

Parameters:

Raises:

  • (NoMethodError)

    This is an abstract method that must be overridden



27
28
29
# File 'lib/verquest/properties/base.rb', line 27

def add(property)
  raise NoMethodError
end

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

This method is abstract.

Creates mapping for this property

Parameters:

  • key_prefix (Array<String>)

    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)

    The version to create mapping for

Returns:

  • (Hash)

    The updated mapping hash

Raises:

  • (NoMethodError)

    This is an abstract method that must be overridden



54
55
56
# File 'lib/verquest/properties/base.rb', line 54

def mapping(key_prefix:, value_prefix:, mapping:, version:)
  raise NoMethodError
end

#mapping_value_key(value_prefix:, collection: false) ⇒ String (private)

Determines the mapping target key based on mapping configuration

Parameters:

  • value_prefix (Array<String>)

    Prefix for the target value

  • collection (Boolean) (defaults to: false)

    Whether this is a collection mapping

Returns:

  • (String)

    The target mapping key



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/verquest/properties/base.rb', line 68

def mapping_value_key(value_prefix:, collection: false)
  value_key = if map.nil?
    (value_prefix + [name]).join("/")
  elsif map == "/"
    ""
  elsif map.start_with?("/")
    map.gsub(%r{^/}, "")
  else
    (value_prefix + map.split("/")).join("/")
  end

  if collection
    value_key + "[]"
  else
    value_key
  end
end

#mapping_value_prefix(value_prefix:, collection: false) ⇒ Array<String> (private)

Determines the mapping target value prefix based on mapping configuration

Parameters:

  • value_prefix (Array<String>)

    Prefix for the target value

  • collection (Boolean) (defaults to: false)

    Whether this is a collection mapping

Returns:

  • (Array<String>)

    The target mapping value prefix



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/verquest/properties/base.rb', line 90

def mapping_value_prefix(value_prefix:, collection: false)
  value_prefix = if map.nil?
    value_prefix + [name]
  elsif map == "/"
    []
  elsif map.start_with?("/")
    map.gsub(%r{^/}, "").split("/")
  else
    value_prefix + map.split("/")
  end

  if collection && value_prefix.any?
    last = value_prefix.pop
    value_prefix.push((last.to_s + "[]").to_sym)
  end

  value_prefix
end

#to_schemaHash

This method is abstract.

Generates JSON schema for this property

Returns:

  • (Hash)

    The schema definition for this property

Raises:

  • (NoMethodError)

    This is an abstract method that must be overridden



35
36
37
# File 'lib/verquest/properties/base.rb', line 35

def to_schema
  raise NoMethodError
end

#to_validation_schema(version: nil) ⇒ Hash

Generates validation schema for this property, defaults to the same as to_schema

Parameters:

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

    The version to generate validation schema for

Returns:

  • (Hash)

    The validation schema for this property



42
43
44
# File 'lib/verquest/properties/base.rb', line 42

def to_validation_schema(version: nil)
  to_schema
end