Class: Spoonerize::Cli

Inherits:
Object
  • Object
show all
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.

Returns:

  • (String)
File.expand_path(File.join(ENV["HOME"], ".spoonerizerc"))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ self

Create instance of Cli

Parameters:

  • options (Array)


54
55
56
57
58
59
60
61
# 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
  @preferences = get_preferences
end

Instance Attribute Details

#optionsArray (readonly)

Arguments passed at runtime.

Returns:

  • (Array)

    ARGV



40
41
42
# File 'lib/spoonerize/cli.rb', line 40

def options
  @options
end

#preferencesArray (readonly)

Preferences after reading config file and parsing ARGV.

Returns:

  • (Array)


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.

Parameters:

  • options (Array) (defaults to: [])


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(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

Instance Method Details

#longest_word_lengthInteger

The length of the longest word in the phrase.

Returns:

  • (Integer)


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?

Returns:

  • (Boolean)


83
84
85
# File 'lib/spoonerize/cli.rb', line 83

def map?
  @map
end

Print the log file contents to the command line.

Returns:

  • (nil)


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

Should we print to the command line?

Returns:

  • (Boolean)


91
92
93
# File 'lib/spoonerize/cli.rb', line 91

def print_log?
  @print_log
end

Print the hash of mappings to the command line.

Returns:

  • (nil)


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?

Returns:

  • (Boolean)


75
76
77
# File 'lib/spoonerize/cli.rb', line 75

def save?
  @save
end

#spoonerismSpoonerize::Spoonerism

Sets up an instance of Spoonerize::Spoonerism



67
68
69
# File 'lib/spoonerize/cli.rb', line 67

def spoonerism
  @spoonerism ||= Spoonerism.new(options)
end