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

PREFERENCE_FILE =

The preferences file the user can create to change runtime options.

Returns:

  • (String)
File.expand_path(File.join(ENV["HOME"], ".spoonerize.yml")).freeze

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
# File 'lib/spoonerize/cli.rb', line 54

def initialize(options)
  @map = false
  @save = false
  @print = 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?
    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)


102
103
104
105
# File 'lib/spoonerize/cli.rb', line 102

def longest_word_length
  @longest_word_length ||=
    spoonerism.spoonerize.group_by(&:size).max.first.size
end

#map?Boolean

Should we print the mappings to the command line?

Returns:

  • (Boolean)


86
87
88
# File 'lib/spoonerize/cli.rb', line 86

def map?
  @map
end

#print?Boolean

Should we print to the command line?

Returns:

  • (Boolean)


94
95
96
# File 'lib/spoonerize/cli.rb', line 94

def print?
  @print
end

Print the log file contents to the command line.

Returns:

  • (nil)


111
112
113
114
# File 'lib/spoonerize/cli.rb', line 111

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.

Returns:

  • (nil)


120
121
122
123
124
# File 'lib/spoonerize/cli.rb', line 120

def print_mappings
  spoonerism.to_h.each do |k, v|
    printf("%-#{longest_word_length}s => %s\n", k, v)
  end
end

#save?Boolean

Should we save to the log file?

Returns:

  • (Boolean)


78
79
80
# File 'lib/spoonerize/cli.rb', line 78

def save?
  @save
end

#spoonerismSpoonerize::Spoonerism

Sets up an instance of Spoonerize::Spoonerism and passes all user preferences.



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

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