Skip to content

Commit 0b8b33d

Browse files
authored
Some code cleanup (#21)
* Adding some return types, and cleaning up the method name to register tasks * use the correct types on these helpers
1 parent f6c9c2f commit 0b8b33d

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class SendDailyNotifications < LuckyTask::Task
4646
# Name is inferred from class name ("send_daily_notifications")
4747
# It can be overridden:
4848
#
49-
# name "app.send_daily_notifications"
49+
# task_name "app.send_daily_notifications"
5050
5151
def call
5252
# Code that sends notifications to all your users...

src/lucky_task/runner.cr

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
class LuckyTask::Runner
22
@@tasks = [] of LuckyTask::Task
3-
class_property? exit_with_error_if_not_found = true
3+
class_property? exit_with_error_if_not_found : Bool = true
44

55
extend LuckyTask::TextHelpers
66

7-
def self.tasks
7+
def self.register_task(task : LuckyTask::Task) : Nil
8+
@@tasks.push(task)
9+
end
10+
11+
def self.tasks : Array(LuckyTask::Task)
812
@@tasks.sort_by!(&.task_name)
913
end
1014

@@ -32,7 +36,7 @@ class LuckyTask::Runner
3236
end
3337
end
3438

35-
def self.help_text
39+
def self.help_text : Nil
3640
puts <<-HELP_TEXT
3741
Usage: lucky name.of.task [options]
3842
@@ -42,11 +46,11 @@ class LuckyTask::Runner
4246
HELP_TEXT
4347
end
4448

45-
def self.find_task(task_name : String)
49+
def self.find_task(task_name : String) : LuckyTask::Task?
4650
@@tasks.find { |task| task.task_name == task_name }
4751
end
4852

49-
def self.tasks_list
53+
def self.tasks_list : String
5054
String.build do |list|
5155
tasks.each do |task|
5256
list << (" #{arrow} " + task.task_name).colorize(:green)
@@ -57,11 +61,11 @@ class LuckyTask::Runner
5761
end
5862
end
5963

60-
def self.list_padding_for(task_name)
64+
def self.list_padding_for(task_name) : String
6165
" " * (longest_task_name - task_name.size + 2)
6266
end
6367

64-
def self.longest_task_name
68+
def self.longest_task_name : Int32
6569
tasks.max_of(&.task_name.size)
6670
end
6771
end

src/lucky_task/task.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ abstract class LuckyTask::Task
66
property output : IO = STDOUT
77

88
{% if !@type.abstract? %}
9-
LuckyTask::Runner.tasks << self.new
9+
LuckyTask::Runner.register_task(self.new)
1010
{% end %}
1111

1212
@[Deprecated("Use `task_name` instead.")]
@@ -44,7 +44,7 @@ abstract class LuckyTask::Task
4444
end
4545

4646
macro summary(summary_text)
47-
def summary
47+
def summary : String
4848
{{summary_text}}
4949
end
5050
end
@@ -279,5 +279,5 @@ abstract class LuckyTask::Task
279279
end
280280

281281
abstract def call
282-
abstract def summary
282+
abstract def summary : String
283283
end

src/lucky_task/task_not_found_error_message.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ class LuckyTask::TaskNotFoundErrorMessage
44
def initialize(@task_name : String, @io : IO = STDERR)
55
end
66

7-
def self.print(*args)
7+
def self.print(*args) : Nil
88
new(*args).print
99
end
1010

11-
def print
11+
def print : Nil
1212
message = "Task #{@task_name.colorize(:cyan)} not found."
1313

1414
similar_task_name.try do |name|
@@ -18,7 +18,7 @@ class LuckyTask::TaskNotFoundErrorMessage
1818
@io.puts message
1919
end
2020

21-
private def similar_task_name
21+
private def similar_task_name : String?
2222
Levenshtein::Finder.find(
2323
@task_name,
2424
LuckyTask::Runner.tasks.map(&.task_name),

src/lucky_task/text_helpers.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module LuckyTask::TextHelpers
2-
def arrow
2+
def arrow : String
33
""
44
end
55

6-
def red_arrow
6+
def red_arrow : Colorize::Object(String)
77
arrow.colorize(:red)
88
end
99

10-
def green_arrow
10+
def green_arrow : Colorize::Object(String)
1111
arrow.colorize(:green)
1212
end
1313
end

0 commit comments

Comments
 (0)