Sha256: a2ebbb0d1ab7328c976b2b0a14a6a1e1a80994503dcb38ee8888a1722c6b677b

Contents?: true

Size: 1.91 KB

Versions: 5

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

require 'tempfile'

RSpec::Core::RakeTask.new(:spec)

task default: :spec

# rubocop:disable all
class Documentator
  DELIMITER = '# Generated by `rake yard`. Do not modify anything below here.'

  def self.update(path, &block)
    new(path).update(&block)
  end

  def initialize(path)
    @path = Jimmy::ROOT.join(*path)
  end

  def update(&block)
    @indent = ''
    mute    = false

    File.foreach(@path) do |line|
      if line.match? /^#{@indent}(class|module) /
        @indent += '  '
      elsif line == @indent + DELIMITER + "\n"
        mute = true
        render &block
      elsif line == @indent[0..-3] + "end\n"
        render "\n", &block unless mute
        mute = false
      end
      write line unless mute
    end

    tempfile.close
    @path.unlink
    File.rename tempfile.path, @path.to_s
  rescue
    tempfile.unlink
    raise
  end

  def <<(str)
    lines = str.lines
    lines.each { |line| write "#{@indent}# #{line}" }
  end

  private

  def write(line)
    tempfile.write line
  end

  def render(prefix = '')
    write "%s%s%s\n%s#\n" % [prefix, @indent, DELIMITER, @indent]
    yield self
  end

  def tempfile
    @tempfile ||= Tempfile.new('documented')
  end
end
# rubocop:enable all

desc 'Generate YARD for some runtime-defined methods'
task :yard do
  require 'jimmy'

  Documentator.update %w[lib jimmy declaration string.rb] do |y|
    Jimmy::Declaration::FORMATS.each do |format|
      y << <<~YARD
        @!method #{format.gsub '-', '_'}()
          Validate a string with format "#{format}".
          @return [Schema]
      YARD
    end
  end

  Documentator.update %w[lib jimmy declaration types.rb] do |y|
    Jimmy::Declaration::SIMPLE_TYPES.each do |type|
      y << <<~YARD
        @!method #{type}()
          Make the schema allow type "#{type}".
          @return [Schema]
      YARD
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
jimmy-2.1.0 Rakefile
jimmy-2.0.3 Rakefile
jimmy-2.0.2 Rakefile
jimmy-2.0.1 Rakefile
jimmy-2.0.0 Rakefile