Class: Spoonerize::Cli
- Inherits:
-
Object
- Object
- Spoonerize::Cli
- Defined in:
- lib/spoonerize/cli.rb
Overview
The class for handling the command-line interface.
Constant Summary collapse
- CONFIG_FILE =
The config file the user can create to change default runtime options.
File.(File.join(ENV["HOME"], ".spoonerizerc"))
Instance Attribute Summary collapse
-
#options ⇒ Array
readonly
Arguments passed at runtime.
-
#preferences ⇒ Array
readonly
Preferences after reading config file and parsing ARGV.
Class Method Summary collapse
-
.execute(options = []) ⇒ Object
Creates an instance of
Spoonerismand runs what the user requested.
Instance Method Summary collapse
-
#initialize(options) ⇒ self
constructor
Create instance of
Cli. -
#longest_word_length ⇒ Integer
The length of the longest word in the phrase.
-
#map? ⇒ Boolean
Should we print the mappings to the command line?.
-
#print_log ⇒ nil
Print the log file contents to the command line.
-
#print_log? ⇒ Boolean
Should we print to the command line?.
-
#print_mappings ⇒ nil
Print the hash of mappings to the command line.
-
#save? ⇒ Boolean
Should we save to the log file?.
-
#spoonerism ⇒ Spoonerize::Spoonerism
Sets up an instance of
Spoonerize::Spoonerism.
Constructor Details
#initialize(options) ⇒ self
Create instance of Cli
54 55 56 57 58 59 60 61 |
# File 'lib/spoonerize/cli.rb', line 54 def initialize() Spoonerize.load_config_file(CONFIG_FILE) if File.file?(CONFIG_FILE) @map = false @save = false @print_log = false @options = @preferences = get_preferences end |
Instance Attribute Details
#options ⇒ Array (readonly)
Arguments passed at runtime.
40 41 42 |
# File 'lib/spoonerize/cli.rb', line 40 def @options end |
#preferences ⇒ Array (readonly)
Preferences after reading config file and parsing ARGV.
46 47 48 |
# File 'lib/spoonerize/cli.rb', line 46 def preferences @preferences end |
Class Method Details
.execute(options = []) ⇒ Object
Creates an instance of Spoonerism and runs what the user requested.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/spoonerize/cli.rb', line 19 def self.execute( = []) exe = new() 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 |
Instance Method Details
#longest_word_length ⇒ Integer
The length of the longest word in the phrase.
99 100 101 |
# File 'lib/spoonerize/cli.rb', line 99 def longest_word_length @longest_word_length ||= spoonerism.spoonerize.max_by(&:size).size end |
#map? ⇒ Boolean
Should we print the mappings to the command line?
83 84 85 |
# File 'lib/spoonerize/cli.rb', line 83 def map? @map end |
#print_log ⇒ nil
Print the log file contents to the command line.
107 108 109 110 111 |
# 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_log? ⇒ Boolean
Should we print to the command line?
91 92 93 |
# File 'lib/spoonerize/cli.rb', line 91 def print_log? @print_log end |
#print_mappings ⇒ nil
Print the hash of mappings to the command line.
117 118 119 120 121 |
# 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 |
#save? ⇒ Boolean
Should we save to the log file?
75 76 77 |
# File 'lib/spoonerize/cli.rb', line 75 def save? @save end |
#spoonerism ⇒ Spoonerize::Spoonerism
Sets up an instance of Spoonerize::Spoonerism
67 68 69 |
# File 'lib/spoonerize/cli.rb', line 67 def spoonerism @spoonerism ||= Spoonerism.new() end |