Class: Aspera::Schema::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/schema/reader.rb

Overview

JSON schema reader

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, current = nil) ⇒ Hash?

Read schema from file or from cache

Parameters:

  • root (Hash)

    root schema

  • current (Hash, nil) (defaults to: nil)

    current position in



38
39
40
41
# File 'lib/aspera/schema/reader.rb', line 38

def initialize(root, current = nil)
  @root = root
  @current = current || root
end

Instance Attribute Details

#currentObject (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

Parameters:

Returns:

  • (Hash, Array, String, Integer)

    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

Parameters:

  • prefix (String) (defaults to: '')

    Prefix for property names (e.g., ‘parent.child.’)

Yields:

  • (property_schema, name, full_name)

    Yields property info to block

Yield Parameters:

  • property_schema (Reader)

    Schema reader for this property (use .current to get node hash)

  • name (String)

    Property name

  • full_name (String)

    Full property name with prefix

Returns:

  • (nil)


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