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: {})


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

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

Instance Method Details

#combine(*schemata) ⇒ Prmd::Schema

Parameters:

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/prmd/core/combiner.rb', line 55

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)
end