Module: SchemaGraphy::SchemaUtils
- Defined in:
- lib/schemagraphy/schema_utils.rb
Overview
A utility module for introspecting schema definitions.
Provides methods for retrieving metadata, default values, and type information
from a schema hash using a dot-separated path syntax.
Class Method Summary collapse
-
.crawl_meta(schema, path = nil) ⇒ Hash
Crawl the schema to find the metadata for a given path.
-
.crawl_properties(schema, path) ⇒ Hash?
Retrieve a nested property definition from a schema using a dot-separated path.
-
.default_for(schema, path) ⇒ Object?
Get the default value for a property from the schema.
-
.templated_field?(schema, path) ⇒ Boolean
Check if a property is a templated field.
-
.templating_config_for(schema, path) ⇒ Hash
Get the templating configuration for a property from the schema.
-
.type_for(schema, path) ⇒ String?
Get the type for a property from the schema.
Class Method Details
.crawl_meta(schema, path = nil) ⇒ Hash
Crawl the schema to find the metadata for a given path.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/schemagraphy/schema_utils.rb', line 108 def self. schema, path = nil parts = path ? path.split('.') : [] node = schema['$schema'] || schema = {} parts.each do |part| node = node['properties'][part] if node['properties']&.key?(part) break unless node.is_a?(Hash) # Only update meta if this level has it = node if node['templating'] end ['$meta'] || ['sgyml'] || ['templating'] || {} end |
.crawl_properties(schema, path) ⇒ Hash?
Retrieve a nested property definition from a schema using a dot-separated path.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/schemagraphy/schema_utils.rb', line 33 def crawl_properties schema, path path_components = path.split('.') current = schema['$schema'] || schema path_components.each do |component| return nil unless current.is_a?(Hash) return nil unless current['properties']&.key?(component) current = current['properties'][component] end current end |
.default_for(schema, path) ⇒ Object?
Get the default value for a property from the schema.
52 53 54 55 56 57 |
# File 'lib/schemagraphy/schema_utils.rb', line 52 def default_for schema, path property = crawl_properties(schema, path) return nil unless property.is_a?(Hash) property['default'] || property['dflt'] end |
.templated_field?(schema, path) ⇒ Boolean
Check if a property is a templated field.
96 97 98 99 100 101 |
# File 'lib/schemagraphy/schema_utils.rb', line 96 def templated_field? schema, path property = crawl_properties(schema, path) return false unless property.is_a?(Hash) property.key?('templating') && property['templating'].is_a?(Hash) end |
.templating_config_for(schema, path) ⇒ Hash
Get the templating configuration for a property from the schema.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/schemagraphy/schema_utils.rb', line 76 def templating_config_for schema, path property = crawl_properties(schema, path) return {} unless property.is_a?(Hash) return property['templating'] if property['templating'] if property['type'].to_s.downcase == 'liquid' { 'default' => 'liquid', 'delay' => true } elsif property['type'].to_s.downcase == 'erb' { 'default' => 'erb', 'delay' => true } else {} end end |
.type_for(schema, path) ⇒ String?
Get the type for a property from the schema.
64 65 66 67 68 69 |
# File 'lib/schemagraphy/schema_utils.rb', line 64 def type_for schema, path property = crawl_properties(schema, path) return nil unless property.is_a?(Hash) property['type'] end |