class Spoonerize::Cli
The class for handling the command-line interface.
Constants
- PREFERENCE_FILE
The preferences file the user can create to change runtime options.
@return [String]
Attributes
Arguments passed at runtime.
@return [Array] ARGV
Preferences after reading config file and parsing ARGV.
@return [Array]
Public Class Methods
Creates an instance of Spoonerism
and runs what the user requested.
@param [Array] options
# File lib/spoonerize/cli.rb, line 20 def self.execute(options = []) exe = new(options) if exe.print? exe.print_log return end puts exe.spoonerism.to_s exe.print_mappings if exe.map? if exe.save? puts "Saving..." exe.spoonerism.save end end
Create instance of Cli
@param [Array] options
@return [self]
# File lib/spoonerize/cli.rb, line 55 def initialize(options) @map = false @save = false @print = false @options = options @preferences = get_preferences end
Public Instance Methods
The length of the longest word in the phrase.
@return [Integer]
# File lib/spoonerize/cli.rb, line 103 def longest_word_length @longest_word_length ||= spoonerism.spoonerize.group_by(&:size).max.first.size end
Should we print the mappings to the command line?
@return [Boolean]
# File lib/spoonerize/cli.rb, line 87 def map? @map end
Should we print to the command line?
@return [Boolean]
# File lib/spoonerize/cli.rb, line 95 def print? @print end
Print the log file contents to the command line.
@return [nil]
# File lib/spoonerize/cli.rb, line 112 def print_log s = Spoonerize::Log.new(spoonerism.logfile_name) s.each { |row| print row.join(' | ') + "\n" } end
Print the hash of mappings to the command line.
@return [nil]
# File lib/spoonerize/cli.rb, line 121 def print_mappings spoonerism.to_h.each do |k, v| puts "%-#{longest_word_length}s => %s" % [k, v] end end
Should we save to the log file?
@return [Boolean]
# File lib/spoonerize/cli.rb, line 79 def save? @save end
Sets up an instance of Spoonerize::Spoonerism
and passes all user preferences.
@return [Spoonerize::Spoonerism]
# File lib/spoonerize/cli.rb, line 68 def spoonerism pf = File.file?(PREFERENCE_FILE) ? PREFERENCE_FILE : nil @spoonerism ||= Spoonerism.new(options, pf) do |s| preferences.each { |k, v| s.send("#{k}=", v) } end end