Sha256: 8bf9574e064e6178d4708ce1bf6129f93a4cbd83b680765a29569d54eb6066ad

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

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

module JsDuck

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

    # Writes guide 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(all_guides) {|guide| @exporter.export_guide(guide) }.compact
      Stdout.instance.add(json)
    end

    def write_dir(dir, extension)
      FileUtils.mkdir(dir) unless File.exists?(dir)
      ParallelWrap.each(all_guides) do |guide|
        filename = dir + "/" + guide["name"] + extension
        Logger.instance.log("Writing guide", filename)
        json = @exporter.export_guide(guide)
        # skip file if exporter returned nil
        if json
          if extension == ".json"
            JsonDuck.write_json(filename, json)
          elsif extension == ".js"
            JsonDuck.write_jsonp(filename, guide["name"], json)
          else
            throw "Unexpected file extension: #{extension}"
          end
        end
      end
    end

    def all_guides
      arr = []
      @guides.each_item {|g| arr << g }
      arr
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

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