Class: Patchboard::SchemaManager

Inherits:
Object
  • Object
show all
Defined in:
lib/patchboard/schema_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(schemas) ⇒ SchemaManager

Returns a new instance of SchemaManager.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/patchboard/schema_manager.rb', line 5

def initialize(schemas)
  @schemas = schemas
  @media_types = {}
  @ids = {}
  @names = {}

  @schemas.each do |schema|
    # TODO error checking for missing id
    base_id = schema[:id].chomp("#")
    schema[:definitions].each do |name, definition|
      # `definitions` is the conventional place to put schemas,
      # so we'll define fragment IDs by default where they are
      # not explicitly specified.
      id = definition[:id] || [base_id, name].join("#")
      self.register_schema(id, definition)
    end
  end
end

Instance Method Details

#find(options) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/patchboard/schema_manager.rb', line 35

def find(options)
  if type = options[:mediaType] || options[:media_type]
    @media_types[type]
  elsif ref = options[:ref]
    @ids[ref]
  elsif name = options[:name]
    @names[name]
  else
    raise "Unusable argument to find: #{options}"
  end
end

#register_schema(id, schema) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/patchboard/schema_manager.rb', line 24

def register_schema(id, schema)
  # FIXME:  extensions and refs are not imported.
  schema[:id] = id
  @ids[id] = schema
  name = id.split("#")[1].to_sym
  @names[name] = schema
  if type = schema[:mediaType]
    @media_types[type] = schema
  end
end