Sha256: 69b00cb6bbcf71a35dc8a5d61225635a2889730cb7920d02062e00df3efd256c

Contents?: true

Size: 1.61 KB

Versions: 15

Compression:

Stored size: 1.61 KB

Contents

require_relative '../schema'
require_relative 'schema_hash'
require_relative 'reference_localizer'

# :nodoc:
module Prmd
  # Schema combiner
  class Combiner
    #
    # @param [Hash<Symbol, Object>] properties
    def initialize(properties = {})
      @properties = properties
      @schema = properties.fetch(:schema)
      @base = properties.fetch(:base, {})
      @meta = properties.fetch(:meta, {})
    end

    # @param [Object] datum
    # @return [Object]
    def reference_localizer(datum)
      ReferenceLocalizer.localize(datum)
    end

    #
    # @param [Prmd::SchemaHash] schemata
    # @return [Prmd::Schema]
    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

    private :reference_localizer
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
prmd-0.11.11 lib/prmd/core/combiner.rb
prmd-0.11.10 lib/prmd/core/combiner.rb
prmd-0.11.9 lib/prmd/core/combiner.rb
prmd-0.11.8 lib/prmd/core/combiner.rb
prmd-0.11.7 lib/prmd/core/combiner.rb
prmd-0.11.6 lib/prmd/core/combiner.rb
prmd-0.11.5 lib/prmd/core/combiner.rb
prmd-0.11.4 lib/prmd/core/combiner.rb
prmd-0.11.3 lib/prmd/core/combiner.rb
prmd-0.11.2 lib/prmd/core/combiner.rb
prmd-0.11.1 lib/prmd/core/combiner.rb
prmd-0.11.0 lib/prmd/core/combiner.rb
prmd-0.10.0 lib/prmd/core/combiner.rb
prmd-0.9.0 lib/prmd/core/combiner.rb
prmd-0.8.0 lib/prmd/core/combiner.rb