Class: JsonSchemaRails::Loaders::HyperSchema::SchemaIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/json_schema_rails/loaders/hyper_schema.rb

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ SchemaIndex

Returns a new instance of SchemaIndex.



27
28
29
30
# File 'lib/json_schema_rails/loaders/hyper_schema.rb', line 27

def initialize(schema)
  @index = { _schema: schema }
  build(schema)
end

Instance Method Details

#build(schema) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/json_schema_rails/loaders/hyper_schema.rb', line 32

def build(schema)
  schema.links.each do |link|
    if link.href
      keys = index_keys("#{link.method || 'GET'}/#{link.href}", true)
      tail = keys.reduce(@index) { |hash, key| hash[key] ||= {} }
      tail[:_schema] = schema
    end
  end
  schema.properties.each do |key, subschema|
    build(subschema)
  end
end

#find(schema_path) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/json_schema_rails/loaders/hyper_schema.rb', line 45

def find(schema_path)
  keys = index_keys(schema_path, false)
  tail = keys.reduce(@index) do |hash, key|
    if hash.key?(key)
      hash[key]
    else
      next_key = hash.keys.find { |k| k === key } or break
      hash[next_key]
    end
  end
  tail[:_schema] if tail
end