From 39cbf3b6471f76b38aafac410db278bd11a27525 Mon Sep 17 00:00:00 2001 From: Joe Yates Date: Tue, 4 Jun 2024 11:54:31 +0200 Subject: [PATCH] Extract class --- .rubocop_todo.yml | 24 +++++------ lib/imap/backup/cli/helpers.rb | 64 ++--------------------------- lib/imap/backup/cli/options.rb | 74 ++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 76 deletions(-) create mode 100644 lib/imap/backup/cli/options.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 08f7b78d..174d7d94 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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 diff --git a/lib/imap/backup/cli/helpers.rb b/lib/imap/backup/cli/helpers.rb index ad883530..ac67a04e 100644 --- a/lib/imap/backup/cli/helpers.rb +++ b/lib/imap/backup/cli/helpers.rb @@ -1,5 +1,6 @@ require "thor" +require "imap/backup/cli/options" require "imap/backup/configuration" require "imap/backup/configuration_not_found" @@ -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 diff --git a/lib/imap/backup/cli/options.rb b/lib/imap/backup/cli/options.rb new file mode 100644 index 00000000..f3edce91 --- /dev/null +++ b/lib/imap/backup/cli/options.rb @@ -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