Sha256: d8b9d0c9412931c77bad7171dea32330df598330c3c15dc4a748ae9a130e6bea

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

module Prmd
  def self.combine(path, options={})
    files = if File.directory?(path)
      Dir.glob(File.join(path, '**', '*.json')) +
        Dir.glob(File.join(path, '**', '*.yaml')) -
        [options[:meta]]
    else
      [path]
    end
    # sort for stable loading on any platform
    schemata = files.sort.map { |file| [file, YAML.load(File.read(file))] }

    data = {
      '$schema'     => 'http://json-schema.org/draft-04/hyper-schema',
      'definitions' => {},
      'properties'  => {},
      'type'        => ['object']
    }

    # tracks which entities where defined in which file
    schemata_map = {}

    if options[:meta] && File.exists?(options[:meta])
      data.merge!(YAML.load(File.read(options[:meta])))
    end

    schemata.each do |schema_file, schema_data|
      id_prefix, id = schema_data['id'].split('/')

      if file = schemata_map[schema_data['id']]
        $stderr.puts "`#{schema_data['id']}` (from #{schema_file}) was already defined in `#{file}` and will overwrite the first definition"
      end
      schemata_map[schema_data['id']] = schema_file

      data['definitions'][id_prefix] ||= {}
      data['definitions'][id_prefix][id] = schema_data
      reference_localizer = lambda do |datum|
        case datum
        when Array
          datum.map {|element| reference_localizer.call(element)}
        when Hash
          if datum.has_key?('$ref')
            datum['$ref'] = '#/definitions' + datum['$ref'].gsub('#', '')
          end
          if datum.has_key?('href')
            datum['href'] = datum['href'].gsub('%23', '').gsub(%r{(%2Fschemata%2F[^%]*%2F)}, '%23%2Fdefinitions\1')
          end
          datum.each { |k,v| datum[k] = reference_localizer.call(v) }
        else
          datum
        end
      end
      reference_localizer.call(data['definitions'][id_prefix][id])

      data['properties'][id] = { '$ref' => "#/definitions/#{id_prefix}/#{id}" }
    end

    Prmd::Schema.new(data)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
prmd-0.3.1 lib/prmd/commands/combine.rb
prmd-0.3.0 lib/prmd/commands/combine.rb