Class: Verquest::Properties::Base Abstract

Inherits:
Object
  • Object
show all
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, Field, Object, Reference

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#mapString?

Returns The mapping path for this property.

Returns:

  • (String, nil)

    The mapping path for this property



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

attr_accessor :name, :required, :map

#nameString

Returns The name of the property.

Returns:

  • (String)

    The name of the property



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

def name
  @name
end

#requiredBoolean

Returns Whether this property is required.

Returns:

  • (Boolean)

    Whether this property is required



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

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



25
26
27
# File 'lib/verquest/properties/base.rb', line 25

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



52
53
54
# File 'lib/verquest/properties/base.rb', line 52

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/verquest/properties/base.rb', line 62

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/verquest/properties/base.rb', line 84

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



33
34
35
# File 'lib/verquest/properties/base.rb', line 33

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



40
41
42
# File 'lib/verquest/properties/base.rb', line 40

def to_validation_schema(version: nil)
  to_schema
end