Class: Prmd::Combiner

Inherits:
Object
  • Object
show all
Defined in:
lib/prmd/core/combiner.rb

Overview

Schema combiner

Instance Method Summary collapse

Constructor Details

#initialize(properties = {}) ⇒ Combiner

Returns a new instance of Combiner.

Parameters:

  • properties (Hash<Symbol, Object>) (defaults to: {})

11
12
13
14
15
16
17
# File 'lib/prmd/core/combiner.rb', line 11

def initialize(properties = {})
  @properties = properties
  @schema = properties.fetch(:schema)
  @base = properties.fetch(:base, {})
  @meta = properties.fetch(:meta, {})
  @options = properties.fetch(:options, {})
end

Instance Method Details

#combine(*schemata) ⇒ Prmd::Schema

Parameters:

Returns:


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/prmd/core/combiner.rb', line 28

def combine(*schemata)
  # tracks which entities where defined in which file
  schemata_map = {}

  data = {}
  data.merge!(@base)
  data.merge!(@meta)

  schemata.each do |schema|
    id = schema.fetch('id')
    id_ary = id.split('/').last

    if s = schemata_map[id]
      $stderr.puts "`#{id}` (from #{schema.filename}) was already defined " \
                   "in `#{s.filename}` and will overwrite the first " \
                   "definition"
    end
    # avoinding damaging the original schema
    embedded_schema = schema.dup
    # schemas are now in a single scope by combine
    embedded_schema.delete('id')
    schemata_map[id] = embedded_schema

    data['definitions'][id_ary] = embedded_schema.to_h
    data['properties'][id_ary] = { '$ref' => "#/definitions/#{id_ary}" }

    reference_localizer(data['definitions'][id_ary])
  end

  Prmd::Schema.new(data, @options)
end