Class: Verquest::Properties::Reference
- Defined in:
- lib/verquest/properties/reference.rb
Overview
Reference property type for schema reuse
Allows referencing other schema definitions to promote reuse and DRY principles. Can reference either a complete schema or a specific property within a schema.
Instance Attribute Summary collapse
-
#from ⇒ Object
readonly
private
Returns the value of attribute from.
-
#property ⇒ Object
readonly
private
Returns the value of attribute property.
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(name:, from:, property: nil, map: nil, required: false) ⇒ Reference
constructor
Initialize a new Reference property.
-
#mapping(key_prefix:, value_prefix:, mapping:, version:) ⇒ Hash
Create mapping for this reference property This delegates to the referenced schema’s mapping with appropriate key prefixing.
-
#to_schema ⇒ Hash
Generate JSON schema definition for this reference property.
-
#to_validation_schema(version: nil) ⇒ Hash
Generate validation schema for this reference property.
Methods inherited from Base
#add, #mapping_value_key, #mapping_value_prefix
Constructor Details
#initialize(name:, from:, property: nil, map: nil, required: false) ⇒ Reference
Initialize a new Reference property
31 32 33 34 35 36 37 |
# File 'lib/verquest/properties/reference.rb', line 31 def initialize(name:, from:, property: nil, map: nil, required: false) @name = name @from = from @property = property @map = map @required = required end |
Instance Attribute Details
#from ⇒ Object (readonly, private)
Returns the value of attribute from.
89 90 91 |
# File 'lib/verquest/properties/reference.rb', line 89 def from @from end |
#property ⇒ Object (readonly, private)
Returns the value of attribute property.
89 90 91 |
# File 'lib/verquest/properties/reference.rb', line 89 def property @property end |
Instance Method Details
#mapping(key_prefix:, value_prefix:, mapping:, version:) ⇒ Hash
Create mapping for this reference property This delegates to the referenced schema’s mapping with appropriate key prefixing
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/verquest/properties/reference.rb', line 66 def mapping(key_prefix:, value_prefix:, mapping:, version:) reference_mapping = from.mapping(version:, property:).dup value_key_prefix = mapping_value_key(value_prefix:) # Single field mapping if property && reference_mapping.size == 1 && !reference_mapping.keys.first.include?(".") reference_mapping = { (key_prefix + [name]).join(".") => value_key_prefix } else if value_key_prefix != "" && !value_key_prefix.end_with?(".") value_key_prefix = "#{value_key_prefix}." end reference_mapping.transform_keys! { "#{(key_prefix + [name]).join(".")}.#{_1}" } reference_mapping.transform_values! { "#{value_key_prefix}#{_1}" } end mapping.merge!(reference_mapping) end |
#to_schema ⇒ Hash
Generate JSON schema definition for this reference property
42 43 44 45 46 |
# File 'lib/verquest/properties/reference.rb', line 42 def to_schema { name => {"$ref": from.to_ref(property:)} } end |
#to_validation_schema(version: nil) ⇒ Hash
Generate validation schema for this reference property
52 53 54 55 56 |
# File 'lib/verquest/properties/reference.rb', line 52 def to_validation_schema(version: nil) { name => from.to_validation_schema(version:, property: property) } end |