class JsDuck::ClassWriter

Writes class data into files in JSON or JSONP format or to STDOUT.

Public Class Methods

new(exporter_class, relations, opts) click to toggle source
# File lib/jsduck/class_writer.rb, line 11
def initialize(exporter_class, relations, opts)
  @relations = relations
  @exporter = exporter_class.new(relations, opts)
end

Public Instance Methods

write(dir, extension) click to toggle source

Writes class data into given directory or STDOUT when dir == :stdout.

Extension is either “.json” for normal JSON output or “.js” for JsonP output.

# File lib/jsduck/class_writer.rb, line 20
def write(dir, extension)
  dir == :stdout ? write_stdout : write_dir(dir, extension)
end

Private Instance Methods

write_dir(dir, extension) click to toggle source
# File lib/jsduck/class_writer.rb, line 31
def write_dir(dir, extension)
  FileUtils.mkdir(dir) unless File.exists?(dir)

  Util::Parallel.each(@relations.classes) do |cls|
    filename = dir + "/" + cls[:name] + extension
    Logger.log("Writing docs", filename)
    json = @exporter.export(cls)
    # skip file if exporter returned nil
    if json
      if extension == ".json"
        Util::Json.write_json(filename, json)
      elsif extension == ".js"
        Util::Json.write_jsonp(filename, cls[:name].gsub(/\./, "_"), json)
      else
        throw "Unexpected file extension: #{extension}"
      end
    end
  end
end
write_stdout() click to toggle source
# File lib/jsduck/class_writer.rb, line 26
def write_stdout
  json = Util::Parallel.map(@relations.classes) {|cls| @exporter.export(cls) }.compact
  Util::Stdout.add(json)
end