Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support YARD notation #724

Merged
merged 10 commits into from
Jan 24, 2020
19 changes: 19 additions & 0 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,14 @@ def get_schema_info(klass, header, options = {})
end
if options[:format_rdoc]
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
elsif options[:format_yard]
info << sprintf("# @!attribute #{col_name}") + "\n"
if col_type == 'boolean'
info << sprintf("# @return [TrueClass]") + "\n"
info << sprintf("# @return [FalseClass]") + "\n"
else
info << sprintf("# @return [#{map_col_type_to_ruby_classes(col_type)}]") + "\n"
end
elsif options[:format_markdown]
name_remainder = max_size - col_name.length - non_ascii_length(col_name)
type_remainder = (md_type_allowance - 2) - col_type.length
Expand Down Expand Up @@ -896,6 +904,17 @@ def classified_sort(cols)
([id] << rest_cols << timestamps << associations).flatten.compact
end

def map_col_type_to_ruby_classes(col_type)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious - can you elaborate why it should not be private and public instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, my first idea was that it can be reused somewhere else but seems not yet so i'll add it in private

case col_type
when 'integer' then Integer.to_s
when 'float' then Float.to_s
when 'decimal' then BigDecimal.to_s
when 'datetime', 'timestamp', 'time' then Time.to_s
when 'date' then Date.to_s
when 'text', 'string', 'binary', 'inet', 'uuid' then String.to_s
when 'json', 'jsonb' then Hash.to_s
end
end
private

def with_comments?(klass, options)
Expand Down
2 changes: 1 addition & 1 deletion lib/annotate/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Constants
FLAG_OPTIONS = [
:show_indexes, :simple_indexes, :include_version, :exclude_tests,
:exclude_fixtures, :exclude_factories, :ignore_model_sub_dir,
:format_bare, :format_rdoc, :format_markdown, :sort, :force, :frozen,
:format_bare, :format_rdoc, :format_yard, :format_markdown, :sort, :force, :frozen,
:trace, :timestamp, :exclude_serializers, :classified_sort,
:show_foreign_keys, :show_complete_foreign_keys,
:exclude_scaffolds, :exclude_controllers, :exclude_helpers,
Expand Down
4 changes: 2 additions & 2 deletions lib/annotate/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.parse(args, env = {})
ANNOTATION_POSITIONS = %w[before top after bottom].freeze
FILE_TYPE_POSITIONS = %w[position_in_class position_in_factory position_in_fixture position_in_test position_in_routes position_in_serializer].freeze
EXCLUSION_LIST = %w[tests fixtures factories serializers].freeze
FORMAT_TYPES = %w[bare rdoc markdown].freeze
FORMAT_TYPES = %w[bare rdoc yard markdown].freeze

def initialize(args, env)
@args = args
Expand Down Expand Up @@ -196,7 +196,7 @@ def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength
exclusions.each { |exclusion| env["exclude_#{exclusion}"] = 'yes' }
end

option_parser.on('-f', '--format [bare|rdoc|markdown]', FORMAT_TYPES, 'Render Schema Infomation as plain/RDoc/Markdown') do |fmt|
option_parser.on('-f', '--format [bare|rdoc|yard|markdown]', FORMAT_TYPES, 'Render Schema Infomation as plain/RDoc/Yard/Markdown') do |fmt|
env["format_#{fmt}"] = 'yes'
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ if Rails.env.development?
'skip_on_db_migrate' => 'false',
'format_bare' => 'true',
'format_rdoc' => 'false',
'format_yard' => 'false',
'format_markdown' => 'false',
'sort' => 'false',
'force' => 'false',
Expand Down
1 change: 1 addition & 0 deletions lib/tasks/annotate_models.rake
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ task annotate_models: :environment do
options[:ignore_model_sub_dir] = Annotate::Helpers.true?(ENV['ignore_model_sub_dir'])
options[:format_bare] = Annotate::Helpers.true?(ENV['format_bare'])
options[:format_rdoc] = Annotate::Helpers.true?(ENV['format_rdoc'])
options[:format_yard] = Annotate::Helpers.true?(ENV['format_yard'])
options[:format_markdown] = Annotate::Helpers.true?(ENV['format_markdown'])
options[:sort] = Annotate::Helpers.true?(ENV['sort'])
options[:force] = Annotate::Helpers.true?(ENV['force'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ if Rails.env.development?
'skip_on_db_migrate' => "false",
'format_bare' => "true",
'format_rdoc' => "false",
'format_yard' => "false",
'format_markdown' => "false",
'sort' => "false",
'force' => "false",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ if Rails.env.development?
'skip_on_db_migrate' => "false",
'format_bare' => "true",
'format_rdoc' => "false",
'format_yard' => "false",
'format_markdown' => "false",
'sort' => "false",
'force' => "false",
Expand Down