Skip to content

Commit 39cbf3b

Browse files
committed
Extract class
1 parent 800f405 commit 39cbf3b

File tree

3 files changed

+86
-76
lines changed

3 files changed

+86
-76
lines changed

.rubocop_todo.yml

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,42 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2023-10-10 15:10:18 UTC using RuboCop version 1.56.4.
3+
# on 2024-06-04 09:54:55 UTC using RuboCop version 1.64.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 36
9+
# Offense count: 40
1010
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
1111
Metrics/AbcSize:
1212
Max: 35
1313

14-
# Offense count: 1
15-
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode.
16-
# AllowedMethods: refine
17-
Metrics/BlockLength:
18-
Max: 54
19-
20-
# Offense count: 11
14+
# Offense count: 12
2115
# Configuration parameters: CountComments, CountAsOne.
2216
Metrics/ClassLength:
23-
Max: 283
17+
Max: 177
2418

25-
# Offense count: 3
19+
# Offense count: 4
2620
# Configuration parameters: AllowedMethods, AllowedPatterns.
2721
Metrics/CyclomaticComplexity:
2822
Max: 11
2923

30-
# Offense count: 72
24+
# Offense count: 75
3125
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
3226
Metrics/MethodLength:
33-
Max: 56
27+
Max: 32
3428

3529
# Offense count: 2
3630
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
3731
Metrics/ParameterLists:
3832
Max: 8
3933

40-
# Offense count: 4
34+
# Offense count: 3
4135
# Configuration parameters: AllowedMethods, AllowedPatterns.
4236
Metrics/PerceivedComplexity:
4337
Max: 11
4438

45-
# Offense count: 21
39+
# Offense count: 29
4640
# Configuration parameters: CountAsOne.
4741
RSpec/ExampleLength:
4842
Max: 16

lib/imap/backup/cli/helpers.rb

+3-61
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require "thor"
22

3+
require "imap/backup/cli/options"
34
require "imap/backup/configuration"
45
require "imap/backup/configuration_not_found"
56

@@ -11,67 +12,8 @@ class CLI < Thor; end
1112
# Provides helper methods for CLI classes
1213
module CLI::Helpers
1314
def self.included(base)
14-
base.class_eval do
15-
def self.accounts_option
16-
method_option(
17-
"accounts",
18-
type: :string,
19-
desc: "a comma-separated list of accounts (defaults to all configured accounts)",
20-
aliases: ["-a"]
21-
)
22-
end
23-
24-
def self.config_option
25-
method_option(
26-
"config",
27-
type: :string,
28-
desc: "supply the configuration file path (default: ~/.imap-backup/config.json)",
29-
aliases: ["-c"]
30-
)
31-
end
32-
33-
def self.format_option
34-
method_option(
35-
"format",
36-
type: :string,
37-
desc: "the output type, 'text' for plain text or 'json'",
38-
aliases: ["-f"]
39-
)
40-
end
41-
42-
def self.quiet_option
43-
method_option(
44-
"quiet",
45-
type: :boolean,
46-
desc: "silence all output",
47-
aliases: ["-q"]
48-
)
49-
end
50-
51-
def self.refresh_option
52-
method_option(
53-
"refresh",
54-
type: :boolean,
55-
desc: "in the default 'keep all emails' mode, " \
56-
"updates flags for messages that are already downloaded",
57-
aliases: ["-r"]
58-
)
59-
end
60-
61-
def self.verbose_option
62-
method_option(
63-
"verbose",
64-
type: :boolean,
65-
desc:
66-
"increase the amount of logging. " \
67-
"Without this option, the program gives minimal output. " \
68-
"Using this option once gives more detailed output. " \
69-
"Whereas, using this option twice also shows all IMAP network calls",
70-
aliases: ["-v"],
71-
repeatable: true
72-
)
73-
end
74-
end
15+
options = CLI::Options.new(base: base)
16+
options.define_options
7517
end
7618

7719
# Processes command-line parameters

lib/imap/backup/cli/options.rb

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
require "thor"
2+
3+
module Imap; end
4+
5+
module Imap::Backup
6+
class CLI < Thor; end
7+
8+
# Defines option methods for CLI classes
9+
class CLI::Options
10+
attr_reader :base
11+
12+
# Options common to many commands
13+
OPTIONS = [
14+
{
15+
name: "accounts",
16+
parameters: {
17+
type: :string, aliases: ["-a"],
18+
desc: "a comma-separated list of accounts (defaults to all configured accounts)"
19+
}
20+
},
21+
{
22+
name: "config",
23+
parameters: {
24+
type: :string, aliases: ["-c"],
25+
desc: "supply the configuration file path (default: ~/.imap-backup/config.json)"
26+
}
27+
},
28+
{
29+
name: "format",
30+
parameters: {
31+
type: :string, desc: "the output type, 'text' for plain text or 'json'", aliases: ["-f"]
32+
}
33+
},
34+
{
35+
name: "quiet",
36+
parameters: {
37+
type: :boolean, desc: "silence all output", aliases: ["-q"]
38+
}
39+
},
40+
{
41+
name: "refresh",
42+
parameters: {
43+
type: :boolean, aliases: ["-r"],
44+
desc: "in the default 'keep all emails' mode, " \
45+
"updates flags for messages that are already downloaded"
46+
}
47+
},
48+
{
49+
name: "verbose",
50+
parameters: {
51+
type: :boolean, aliases: ["-v"], repeatable: true,
52+
desc: "increase the amount of logging. " \
53+
"Without this option, the program gives minimal output. " \
54+
"Using this option once gives more detailed output. " \
55+
"Whereas, using this option twice also shows all IMAP network calls"
56+
}
57+
}
58+
].freeze
59+
60+
def initialize(base:)
61+
@base = base
62+
end
63+
64+
def define_options
65+
OPTIONS.each do |option|
66+
base.singleton_class.class_eval do
67+
define_method("#{option[:name]}_option") do
68+
method_option(option[:name], **option[:parameters])
69+
end
70+
end
71+
end
72+
end
73+
end
74+
end

0 commit comments

Comments
 (0)