Skip to content

Instantly share code, notes, and snippets.

@connorshea
Last active September 20, 2019 23:38
Show Gist options
  • Save connorshea/d1f5e0aa2e79575dc68e651a06dad192 to your computer and use it in GitHub Desktop.
Save connorshea/d1f5e0aa2e79575dc68e651a06dad192 to your computer and use it in GitHub Desktop.
A simple little script to make it easier to add YARD docs for a Faker class.
# A simple little script to make it easier to add YARD docs for a Faker class.
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'activesupport', '~> 6.0.0'
gem 'tty-prompt'
end
require 'open-uri'
require 'active_support/all'
require "tty-prompt"
prompt = TTY::Prompt.new
def indefinite_articlerize(params_word)
%w(a e i o u).include?(params_word[0].downcase) ? "an #{params_word}" : "a #{params_word}"
end
def deindent_heredoc(x)
lines = x.lines
/^( *)/ === lines.first
indent_amount = $1.length
lines.map do |line|
/^ +$/ === line[0...indent_amount] \
? line[indent_amount..-1]
: line
end.join.rstrip
end
def open_link(url)
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
system "start #{url}"
elsif RbConfig::CONFIG['host_os'] =~ /darwin/
system "open #{url}"
elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/
system "xdg-open #{url}"
end
end
category = prompt.select("Category?", %w(Default Games TvShows Sports Quotes Music Movies JapaneseMedia Books Blockchain Creature))
klass_name = prompt.ask("Faker Class Name? (e.g. SuperSmashBros):")
raise StandardError if klass_name.nil?
url = "https://github.com/faker-ruby/faker/blob/master/doc/#{category.underscore}/#{klass_name.underscore}.md"
open_link(url)
is_humanized_name = prompt.yes?("Is '#{klass_name.titleize}' the correct 'humanized' name?")
humanized_name = klass_name.titleize if is_humanized_name
humanized_name = prompt.ask("What's the correct 'humanized' name?") unless is_humanized_name
methods = []
loop do
method = prompt.ask("Method name (input nothing to stop requesting methods):")
break if method.nil?
methods << method
end
all_use_same_version = prompt.yes?('Use the same version for all methods?')
faker_version_all = prompt.ask('What version?') if all_use_same_version
all_use_autogenerated_description = prompt.yes?('Use the autogenerated description for all methods?')
all_return_string = prompt.yes?('Use "String" as the return type for all methods?')
method_hashes = []
methods.each do |method|
description = prompt.ask("Description for #{method} (empty to autogenerate a description):") unless all_use_autogenerated_description
description = "Produces #{indefinite_articlerize(method.humanize(capitalize: false))} from #{humanized_name}." if all_use_autogenerated_description
return_type = prompt.ask("Return type for #{method}: ", default: "String") unless all_return_string
return_type = "String" if all_return_string
if all_use_same_version
faker_version = faker_version_all
else
faker_version = prompt.ask("Faker version for #{method}?")
faker_version = "1.0.0" if faker_version.nil?
end
example_output = prompt.ask("Example output for #{method}?", default: 'PLACEHOLDER')
# Add quotes if they don't have them and the return type is String.
example_output = "\"#{example_output}\"" if return_type == "String" && (!example_output.start_with?('"') || !example_output.end_with?('"'))
example = "Faker::#{category}::#{klass_name}.#{method} #=> #{example_output}" if category != "Default"
example = "Faker::#{klass_name}.#{method} #=> #{example_output}" if category == "Default"
method_hashes << {
name: method,
description: description,
return_type: return_type,
faker_version: faker_version,
example: example
}
end
method_hashes.each do |method_hash|
text = <<-RUBY
##
# #{method_hash[:description]}
#
# @return [#{method_hash[:return_type]}]
#
# @example
# #{method_hash[:example]}
#
# @faker.version #{method_hash[:faker_version]}
RUBY
puts deindent_heredoc(text)
2.times { puts }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment