Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/yard/tags/types_explainer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def initialize(name)

def to_s(singular = true)
if name[0, 1] == "#"
singular ? "an object that responds to #{name}" : "objects that respond to #{name}"
(singular ? "an object that responds to " : "objects that respond to ") + list_join(name.split(/ *& */), with: "and")
elsif name[0, 1] =~ /[A-Z]/
singular ? "a#{name[0, 1] =~ /[aeiou]/i ? 'n' : ''} " + name : "#{name}#{name[-1, 1] =~ /[A-Z]/ ? "'" : ''}s"
else
Expand All @@ -42,12 +42,12 @@ def to_s(singular = true)

private

def list_join(list)
def list_join(list, with: "or")
index = 0
list.inject(String.new) do |acc, el|
acc << el.to_s
acc << ", " if index < list.size - 2
acc << " or " if index == list.size - 2
acc << " #{with} " if index == list.size - 2
index += 1
acc
end
Expand Down
12 changes: 12 additions & 0 deletions spec/tags/types_explainer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ def parse_fail(types)
expect(@t.to_s(false)).to eq "objects that respond to #mymethod"
end

it "works for multiple methods joined with '&' (ducktype)" do
@t.name = "#mymethod&#myothermethod&#mythirdmethod"
expect(@t.to_s).to eq "an object that responds to #mymethod, #myothermethod and #mythirdmethod"
expect(@t.to_s(false)).to eq "objects that respond to #mymethod, #myothermethod and #mythirdmethod"
end

it "works for multiple methods joined with ' & ' (ducktype)" do
@t.name = "#mymethod & #myothermethod & #mythirdmethod"
expect(@t.to_s).to eq "an object that responds to #mymethod, #myothermethod and #mythirdmethod"
expect(@t.to_s(false)).to eq "objects that respond to #mymethod, #myothermethod and #mythirdmethod"
end

it "works for a constant value" do
['false', 'true', 'nil', '4'].each do |name|
@t.name = name
Expand Down