class Spoonerize::Cli
The class for handling the command-line interface.
Constants
- CONFIG_FILE
-
The config file the CLI loads before parsing runtime options.
@return [String]
Attributes
Arguments passed at runtime.
@return [Array] ARGV
Overrides after reading config file and parsing ARGV.
@return [Hash]
Public Class Methods
Source
# File lib/spoonerize/cli.rb, line 19 def self.execute(options = []) exe = new(options) if exe.print_log? exe.print_log return end puts exe.spoonerism exe.print_mappings if exe.map? if exe.save? puts "Saving..." exe.spoonerism.save end end
Creates an instance of Spoonerism and runs what the user requested.
@param [Array] options
Source
# File lib/spoonerize/cli.rb, line 54 def initialize(options) Spoonerize.load_config_file(CONFIG_FILE) if File.file?(CONFIG_FILE) @map = false @save = false @print_log = false @options = options @overrides = get_overrides end
Public Instance Methods
Source
# File lib/spoonerize/cli.rb, line 99 def longest_word_length @longest_word_length ||= spoonerism.spoonerize.max_by(&:size).size end
The length of the longest word in the phrase.
@return [Integer]
Source
# File lib/spoonerize/cli.rb, line 83 def map? @map end
Should we print the mappings to the command line?
@return [Boolean]
Source
# File lib/spoonerize/cli.rb, line 107 def print_log Spoonerize::Log.new(Spoonerize.config.logfile_name).each do |row| puts row.join(" | ") end end
Print the log file contents to the command line.
@return [nil]
Source
# File lib/spoonerize/cli.rb, line 91 def print_log? @print_log end
Should we print to the command line?
@return [Boolean]
Source
# File lib/spoonerize/cli.rb, line 117 def print_mappings spoonerism.to_h.each do |k, v| printf("%-#{longest_word_length + 1}s => %s\n", k, v) end end
Print the hash of mappings to the command line.
@return [nil]
Source
# File lib/spoonerize/cli.rb, line 75 def save? @save end
Should we save to the log file?
@return [Boolean]
Source
# File lib/spoonerize/cli.rb, line 67 def spoonerism @spoonerism ||= Spoonerism.new(*options, **overrides) end
Sets up an instance of Spoonerize::Spoonerism
@return [Spoonerize::Spoonerism]