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

(#267) Don’t show “Public X” header without contents #314

Merged
merged 1 commit into from
Sep 29, 2022

Conversation

danielparks
Copy link
Contributor

Previously, the “Public X” header would be displayed if there were any private X regardless of whether there were any public X.

For example, having a private function would make both the “Public Functions” and “Private Functions” headers appear, even if there weren’t any public functions.

This changes it so that there are three conditions:

  • Only public X: no “Public” or “Private“ headers are displayed; the X are listed with links to their documentation. (Public is implied.)
  • Only private X: a “Private X” header is is displayed with a list of X. There are no links to documentation for private APIs.
  • Both public and private X: both “Public X” and “Private X” headers are displayed. The public Xs are listed with links to their documentation; the private Xs are just listed with no links.

In other words, if there are no private Xs, then it just lists the public once. Otherwise, it splits them up under public/private headers but avoids showing a header if it’s contents will be empty.

This also radically simplifies and removes a bunch of boilerplate code around rendering these sections.

Copy link
Contributor

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

Nice deduplication. Looks like there's room for more. Some minor details inline.

lib/puppet-strings/markdown/table_of_contents.rb Outdated Show resolved Hide resolved
lib/puppet-strings/markdown/table_of_contents.rb Outdated Show resolved Hide resolved
@danielparks
Copy link
Contributor Author

I realized that it was a lot clearer to pull PuppetStrings::Markdown::TableOfContents into the general renderer, so I did. I also did some future-rubocop-related clean ups, though it’s still going to take some work to rebase this after #310 lands.

Copy link
Contributor

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

So tempting to remove even more code.

# @param [String] path The full path to the template file.
# @return [ERB] Template
def self.erb(path)
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.6.0')
Copy link
Contributor

Choose a reason for hiding this comment

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

I wrote this, but later on I realized you could also write it with the if outside of def so it's only ran once, though it probably doesn't matter much.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm… I thought about it, but I’m inclined not to since it ends up duplicating the doc comment. Or maybe I should just put the docs above the outer if if I do that? Pretty sure the performance difference will be negligible.

spec/unit/puppet-strings/markdown_spec.rb Outdated Show resolved Hide resolved
lib/puppet-strings/markdown/data_types.rb Outdated Show resolved Hide resolved
@danielparks danielparks marked this pull request as draft September 29, 2022 09:04
@danielparks danielparks force-pushed the better_public_private branch 2 times, most recently from dbf2c55 to 1681e77 Compare September 29, 2022 11:14
Previously, the “Public X” header would be displayed if there were any
private X regardless of whether there were any public X.

For example, having a private function would make both the “Public
Functions” and “Private Functions” headers appear, even if there weren’t
any public functions.

This changes it so that there are three conditions:

  * **Only public X:** no “Public” or “Private“ headers are displayed;
    the X are listed with links to their documentation. (Public is
    implied.)
  * **Only private X:** a “Private X” header is is displayed with a list
    of X. There are no links to documentation for private APIs.
  * **Both public and private X:** both “Public X” and “Private X”
    headers are displayed. The public Xs are listed with links to their
    documentation; the private Xs are just listed with no links.

In other words, if there are no private Xs, then it just lists the
public once. Otherwise, it splits them up under public/private headers
but avoids showing a header if it’s contents will be empty.

This also radically simplifies and removes a bunch of boilerplate code
around rendering these sections.
@danielparks danielparks marked this pull request as ready for review September 29, 2022 11:17
@chelnak chelnak merged commit f6d615c into puppetlabs:main Sep 29, 2022
@danielparks danielparks deleted the better_public_private branch September 29, 2022 14:49
#
# @param [Optional[String]] Name of the group to set
# @return [String] Name of the group
def self.group_name(name = nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

Thoughts about splitting the functionality in self.group_name() and self.group_name=(name)? Would that work?

Same comment applies to yard_types.

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 actually tried doing

class << self
  attr_accessor :group_name
  attr_accessor :yard_types
end

but I ran into a few problems. One was that it interpreted yard_types = [...] as trying to set a local variable instead of calling a class method, but maybe I could have called self.yard_types = [...].

I wanted it to look more like functions like attr_accessor since I figured that would feel more natural, but it did seem inconsistent with the way Ruby does things elsewhere.

I don’t have a real good sense for the Ruby Way anymore… been writing other languages for too long.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair enough. I suppose this is good enough.

Comment on lines +76 to +77
.map(&:to_hash)
.map { |i| new(i) }
Copy link
Contributor

Choose a reason for hiding this comment

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

You can combine both maps

Suggested change
.map(&:to_hash)
.map { |i| new(i) }
.map { |i| new(i.to_hash) }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤦

# @return [Array] list of items
def self.items
yard_types
.flat_map { |type| YARD::Registry.all(type) }
Copy link
Contributor

Choose a reason for hiding this comment

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

@chelnak chelnak added the bugfix label Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants