Sha256: 30dd55ac02c303dd1033a931f0d3dfc28c57a0bc39d850e4e5a6dd1c5b72e085

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

require 'jsduck/parallel_wrap'
require 'jsduck/logger'
require 'jsduck/stdout'
require 'fileutils'

module JsDuck

  # Writes class data into files in JSON or JSONP format or to STDOUT.
  class ClassWriter
    def initialize(exporter_class, relations, opts)
      @relations = relations
      @exporter = exporter_class.new(relations, opts)
    end

    # Writes class data into given directory or STDOUT when dir == :stdout.
    #
    # Extension is either ".json" for normal JSON output
    # or ".js" for JsonP output.
    def write(dir, extension)
      dir == :stdout ? write_stdout : write_dir(dir, extension)
    end

    private

    def write_stdout
      json = ParallelWrap.map(@relations.classes) {|cls| @exporter.export(cls) }.compact
      Stdout.instance.add(json)
    end

    def write_dir(dir, extension)
      FileUtils.mkdir(dir)
      ParallelWrap.each(@relations.classes) do |cls|
        filename = dir + "/" + cls[:name] + extension
        Logger.instance.log("Writing docs", filename)
        json = @exporter.export(cls)
        # skip file if exporter returned nil
        if json
          if extension == ".json"
            JsonDuck.write_json(filename, json)
          elsif extension == ".js"
            JsonDuck.write_jsonp(filename, cls[:name].gsub(/\./, "_"), json)
          else
            throw "Unexpected file extension: #{extension}"
          end
        end
      end
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jsduck-4.0.1 lib/jsduck/class_writer.rb
jsduck-4.0.0 lib/jsduck/class_writer.rb
jsduck-4.0.beta2 lib/jsduck/class_writer.rb