Skip to content

Commit 5589664

Browse files
committed
Can sort by name or date
1 parent e852453 commit 5589664

File tree

6 files changed

+112
-3
lines changed

6 files changed

+112
-3
lines changed

lib/gem_dating.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def self.fetch_specs(gems, options)
2020
specs = Rubygems.fetch(gems)
2121
results = Result.new(specs)
2222
results.older_than(options[:older_than]) if options[:older_than]
23+
results.sort(options)
2324
results
2425
end
2526

lib/gem_dating/cli.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class Cli
1212
Options:
1313
--help, -h, -? Show this help message
1414
--older-than=<AGE>, --ot=<AGE> Filter gems updated within the last X (e.g. 2y, 1m, 4w, 10d)
15+
--sort-by=<FIELD> Sort by field (name or date), defaults to name
16+
--order=<DIRECTION> Sort direction (asc or desc), defaults to asc
1517
HELP
1618

1719
def initialize(argv = [])
@@ -50,9 +52,19 @@ def run
5052
def parse_args(args = @args)
5153
options = {}
5254
options[:help] = true if (args & %w[-h --help -?]).any?
55+
5356
if (older_than = args.find { |arg| arg.start_with?("--older-than=", "--ot=") })
5457
options[:older_than] = older_than.split("=").last
5558
end
59+
60+
if (sort_by = args.find { |arg| arg.start_with?("--sort-by=") })
61+
options[:sort_by] = sort_by.split("=").last
62+
end
63+
64+
if (order = args.find { |arg| arg.start_with?("--order=") })
65+
options[:order] = order.split("=").last
66+
end
67+
5668
options
5769
end
5870
end

lib/gem_dating/result.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ def table_print
2727

2828
def older_than(date)
2929
specs.select! { |spec| spec.date.to_date < self.cut_off(date) }
30+
self
31+
end
32+
33+
def sort(options = {})
34+
field = options[:sort_by] || "name"
35+
direction = options[:order] || "asc"
36+
37+
@specs = @specs.sort_by do |spec|
38+
case field
39+
when "name"
40+
spec.name.downcase
41+
when "date"
42+
spec.date
43+
else
44+
spec.name.downcase
45+
end
46+
end
47+
48+
@specs = @specs.reverse if direction.downcase == "desc"
49+
50+
self
3051
end
3152

3253
private

test/cli_test.rb

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def test_gemfile
4242
NAME | VERSION | DATE
4343
----------------|----------|-----------
4444
banana-client | 21.1.0 | 1990-08-21
45-
rails-on-rubies | 70.0.5 | 2123-05-24
4645
giraffeql | 0.0.2227 | 2023-05-17
46+
rails-on-rubies | 70.0.5 | 2123-05-24
4747
EXPECTED
4848

4949
assert_equal 0, exit_code
@@ -64,8 +64,8 @@ def test_default_to_existing_relative_gemfile
6464
NAME | VERSION | DATE
6565
----------------|----------|-----------
6666
banana-client | 21.1.0 | 1990-08-21
67-
rails-on-rubies | 70.0.5 | 2123-05-24
6867
giraffeql | 0.0.2227 | 2023-05-17
68+
rails-on-rubies | 70.0.5 | 2123-05-24
6969
EXPECTED
7070

7171
assert_equal 0, exit_code
@@ -117,4 +117,42 @@ def test_parse_args
117117
cli = GemDating::Cli.new([])
118118
assert_equal({}, cli.send(:parse_args))
119119
end
120+
121+
def test_sort_by_name_asc
122+
exit_code = nil
123+
124+
stdout, _stderr = capture_io do
125+
exit_code = GemDating::Cli.new(["test/Gemfile.example", "--sort-by=name", "--order=asc"]).run
126+
end
127+
128+
expected_out = <<~EXPECTED
129+
NAME | VERSION | DATE
130+
----------------|----------|-----------
131+
banana-client | 21.1.0 | 1990-08-21
132+
giraffeql | 0.0.2227 | 2023-05-17
133+
rails-on-rubies | 70.0.5 | 2123-05-24
134+
EXPECTED
135+
136+
assert_equal 0, exit_code
137+
assert_equal expected_out, stdout
138+
end
139+
140+
def test_sort_by_date_desc
141+
exit_code = nil
142+
143+
stdout, _stderr = capture_io do
144+
exit_code = GemDating::Cli.new(["test/Gemfile.example", "--sort-by=date", "--order=desc"]).run
145+
end
146+
147+
expected_out = <<~EXPECTED
148+
NAME | VERSION | DATE
149+
----------------|----------|-----------
150+
rails-on-rubies | 70.0.5 | 2123-05-24
151+
giraffeql | 0.0.2227 | 2023-05-17
152+
banana-client | 21.1.0 | 1990-08-21
153+
EXPECTED
154+
155+
assert_equal 0, exit_code
156+
assert_equal expected_out, stdout
157+
end
120158
end

test/gem_dating_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def test_gems_copy_pasted_from_gemfile
3232
gem "puma", "~> 5.0"
3333
TEXT
3434

35-
rails, _rest = GemDating.from_string(pasteboard).to_a
35+
specs = GemDating.from_string(pasteboard).to_a
36+
rails = specs.find { |spec| spec.name == "rails" }
3637

3738
assert_equal "rails", rails.name
3839
assert_operator Gem::Version.new("7.0"), :<=, rails.version

test/result_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,40 @@ def test_cut_off_invalid_format_raises
110110
assert_raises(ArgumentError) { @date_result.older_than("abc") }
111111
assert_raises(ArgumentError) { @date_result.older_than("") }
112112
end
113+
114+
def test_sort_by_name_asc
115+
result = GemDating::Result.new([@spec1, @spec2])
116+
sorted = result.sort(sort_by: "name", order: "asc")
117+
118+
assert_equal ["hi", "there"], sorted.to_a.map(&:name)
119+
end
120+
121+
def test_sort_by_name_desc
122+
result = GemDating::Result.new([@spec1, @spec2])
123+
sorted = result.sort(sort_by: "name", order: "desc")
124+
125+
assert_equal ["there", "hi"], sorted.to_a.map(&:name)
126+
end
127+
128+
def test_sort_by_date_asc
129+
result = GemDating::Result.new([@spec1, @spec2])
130+
sorted = result.sort(sort_by: "date", order: "asc")
131+
132+
assert_equal ["there", "hi"], sorted.to_a.map(&:name)
133+
end
134+
135+
def test_sort_by_date_desc
136+
result = GemDating::Result.new([@spec1, @spec2])
137+
sorted = result.sort(sort_by: "date", order: "desc")
138+
139+
assert_equal ["hi", "there"], sorted.to_a.map(&:name)
140+
end
141+
142+
def test_sort_defaults
143+
result = GemDating::Result.new([@spec2, @spec1])
144+
sorted = result.sort
145+
146+
# Default sort is by name in ascending order
147+
assert_equal ["hi", "there"], sorted.to_a.map(&:name)
148+
end
113149
end

0 commit comments

Comments
 (0)