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?



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

attr_accessor :name, :required, :map

#nameString



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



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

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

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



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



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

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



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

def to_validation_schema(version: nil)
  to_schema
end