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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ end

group :development, :test do
gem 'byebug'
gem 'pry-byebug'
gem 'guard-rspec', require: false
gem 'rspec', require: false

Expand Down
10 changes: 3 additions & 7 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def get_schema_info(klass, header, options = {})
if col_type == 'decimal'
col_type << "(#{col.precision}, #{col.scale})"
elsif !%w[spatial geometry geography].include?(col_type)
if col.limit
if col.limit && !options[:format_yard]
if col.limit.is_a? Array
attrs << "(#{col.limit.join(', ')})"
else
Expand Down Expand Up @@ -306,12 +306,7 @@ def get_schema_info(klass, header, options = {})
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
info << sprintf("# @return [#{map_col_type_to_ruby_classes(col_type)}]") + "\n"
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 @@ -913,6 +908,7 @@ def map_col_type_to_ruby_classes(col_type)
when 'date' then Date.to_s
when 'text', 'string', 'binary', 'inet', 'uuid' then String.to_s
when 'json', 'jsonb' then Hash.to_s
when 'boolean' then 'Boolean'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this change any current behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems not. Boolean is a class that YARD understand quite well. It's better than TrueClass and FalseClass

Choose a reason for hiding this comment

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

If you are referring to just the boolean case, Boolean is the string for what YARD expects a boolean type to be in the comments.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Gotcha

end
end
private
Expand Down
20 changes: 20 additions & 0 deletions spec/lib/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,26 @@ def mock_column(name, type, options = {})
EOS
end

it 'should get schema info as YARD' do
klass = mock_class(:users,
:id,
[
mock_column(:id, :integer),
mock_column(:name, :string, limit: 50),
])
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_yard: true)).to eql(<<-EOS)
# #{AnnotateModels::PREFIX}
#
# Table name: users
#
# @!attribute id
# @return [Integer]
# @!attribute name
# @return [String]
#
EOS
end

it 'should get schema info as Markdown' do
klass = mock_class(:users,
:id,
Expand Down