Class: Aspera::Schema::Reader
- Inherits:
-
Object
- Object
- Aspera::Schema::Reader
- Defined in:
- lib/aspera/schema/reader.rb
Overview
JSON schema reader
Instance Attribute Summary collapse
-
#current ⇒ Object
readonly
Returns the value of attribute current.
Instance Method Summary collapse
-
#[](x) ⇒ Hash, ...
Shortcut to access current value at path.
-
#dig(*path) ⇒ Object
Find sub path relative to current Honors $ref.
-
#each_property(prefix = '') {|property_schema, name, full_name| ... } ⇒ nil
Recursively traverse schema properties with a block Handles nested objects and arrays automatically.
-
#initialize(root, current = nil) ⇒ Hash?
constructor
Read schema from file or from cache.
Constructor Details
Instance Attribute Details
#current ⇒ Object (readonly)
Returns the value of attribute current.
8 9 10 |
# File 'lib/aspera/schema/reader.rb', line 8 def current @current end |
Instance Method Details
#[](x) ⇒ Hash, ...
Shortcut to access current value at path
13 14 15 |
# File 'lib/aspera/schema/reader.rb', line 13 def [](x) @current[x] end |
#dig(*path) ⇒ Object
Find sub path relative to current Honors $ref
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/aspera/schema/reader.rb', line 19 def dig(*path) current = @current path.each do |p| Aspera.assert(current.key?(p)){"schema: #{p} in #{path}"} current = current[p] Aspera.assert_type(current, Hash){'schema'} if current.key?('$ref') ref = current['$ref'] Aspera.assert(ref.start_with?('#/')) current = @root.dig(*ref[2..].split('/')) end end Reader.new(@root, current) end |
#each_property(prefix = '') {|property_schema, name, full_name| ... } ⇒ nil
Recursively traverse schema properties with a block Handles nested objects and arrays automatically
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/aspera/schema/reader.rb', line 51 def each_property(prefix = '', &block) properties = dig('properties') properties.current.each_key do |name| property_full_name = "#{prefix}#{name}" property_schema = properties.dig(name) node = property_schema.current # Yield current property to block yield(property_schema, name, property_full_name) # Recursively process nested structures case node['type'] when 'object' property_schema.each_property("#{property_full_name}.", &block) if node['properties'] when 'array' if node['items'] array_item_schema = property_schema.dig('items') array_item_schema.each_property("#{property_full_name}[].", &block) if array_item_schema.current['properties'] end end end end |