Skip to content

Commit

Permalink
Extract class
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyates committed Jun 4, 2024
1 parent 800f405 commit 39cbf3b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 76 deletions.
24 changes: 9 additions & 15 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,48 +1,42 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2023-10-10 15:10:18 UTC using RuboCop version 1.56.4.
# on 2024-06-04 09:54:55 UTC using RuboCop version 1.64.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 36
# Offense count: 40
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 35

# Offense count: 1
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode.
# AllowedMethods: refine
Metrics/BlockLength:
Max: 54

# Offense count: 11
# Offense count: 12
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 283
Max: 177

# Offense count: 3
# Offense count: 4
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/CyclomaticComplexity:
Max: 11

# Offense count: 72
# Offense count: 75
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Max: 56
Max: 32

# Offense count: 2
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
Metrics/ParameterLists:
Max: 8

# Offense count: 4
# Offense count: 3
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/PerceivedComplexity:
Max: 11

# Offense count: 21
# Offense count: 29
# Configuration parameters: CountAsOne.
RSpec/ExampleLength:
Max: 16
Expand Down
64 changes: 3 additions & 61 deletions lib/imap/backup/cli/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "thor"

require "imap/backup/cli/options"
require "imap/backup/configuration"
require "imap/backup/configuration_not_found"

Expand All @@ -11,67 +12,8 @@ class CLI < Thor; end
# Provides helper methods for CLI classes
module CLI::Helpers
def self.included(base)
base.class_eval do
def self.accounts_option
method_option(
"accounts",
type: :string,
desc: "a comma-separated list of accounts (defaults to all configured accounts)",
aliases: ["-a"]
)
end

def self.config_option
method_option(
"config",
type: :string,
desc: "supply the configuration file path (default: ~/.imap-backup/config.json)",
aliases: ["-c"]
)
end

def self.format_option
method_option(
"format",
type: :string,
desc: "the output type, 'text' for plain text or 'json'",
aliases: ["-f"]
)
end

def self.quiet_option
method_option(
"quiet",
type: :boolean,
desc: "silence all output",
aliases: ["-q"]
)
end

def self.refresh_option
method_option(
"refresh",
type: :boolean,
desc: "in the default 'keep all emails' mode, " \
"updates flags for messages that are already downloaded",
aliases: ["-r"]
)
end

def self.verbose_option
method_option(
"verbose",
type: :boolean,
desc:
"increase the amount of logging. " \
"Without this option, the program gives minimal output. " \
"Using this option once gives more detailed output. " \
"Whereas, using this option twice also shows all IMAP network calls",
aliases: ["-v"],
repeatable: true
)
end
end
options = CLI::Options.new(base: base)
options.define_options
end

# Processes command-line parameters
Expand Down
74 changes: 74 additions & 0 deletions lib/imap/backup/cli/options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require "thor"

module Imap; end

module Imap::Backup
class CLI < Thor; end

# Defines option methods for CLI classes
class CLI::Options
attr_reader :base

# Options common to many commands
OPTIONS = [
{
name: "accounts",
parameters: {
type: :string, aliases: ["-a"],
desc: "a comma-separated list of accounts (defaults to all configured accounts)"
}
},
{
name: "config",
parameters: {
type: :string, aliases: ["-c"],
desc: "supply the configuration file path (default: ~/.imap-backup/config.json)"
}
},
{
name: "format",
parameters: {
type: :string, desc: "the output type, 'text' for plain text or 'json'", aliases: ["-f"]
}
},
{
name: "quiet",
parameters: {
type: :boolean, desc: "silence all output", aliases: ["-q"]
}
},
{
name: "refresh",
parameters: {
type: :boolean, aliases: ["-r"],
desc: "in the default 'keep all emails' mode, " \
"updates flags for messages that are already downloaded"
}
},
{
name: "verbose",
parameters: {
type: :boolean, aliases: ["-v"], repeatable: true,
desc: "increase the amount of logging. " \
"Without this option, the program gives minimal output. " \
"Using this option once gives more detailed output. " \
"Whereas, using this option twice also shows all IMAP network calls"
}
}
].freeze

def initialize(base:)
@base = base
end

def define_options
OPTIONS.each do |option|
base.singleton_class.class_eval do
define_method("#{option[:name]}_option") do
method_option(option[:name], **option[:parameters])
end
end
end
end
end
end

0 comments on commit 39cbf3b

Please sign in to comment.