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

options[R]

Arguments passed at runtime.

@return [Array] ARGV

preferences[R]

Preferences after reading config file and parsing ARGV.

@return [Array]

Public Class Methods

execute(options = []) click to toggle source

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
new(options) click to toggle source

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

longest_word_length() click to toggle source

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
map?() click to toggle source

Should we print the mappings to the command line?

@return [Boolean]

# File lib/spoonerize/cli.rb, line 87
def map?
  @map
end
print?() click to toggle source

Should we print to the command line?

@return [Boolean]

print_log() click to toggle source

Print the log file contents to the command line.

@return [nil]

print_mappings() click to toggle source

Print the hash of mappings to the command line.

@return [nil]

save?() click to toggle source

Should we save to the log file?

@return [Boolean]

# File lib/spoonerize/cli.rb, line 79
def save?
  @save
end
spoonerism() click to toggle source

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