Module: Gemsmith::CLIHelpers

Included in:
CLI
Defined in:
lib/gemsmith/cli_helpers.rb

Overview

Command Line Interface (CLI) helpers that aid the CLI class. These are extracted to a module in order to not clutter up the main CLI object.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.enforce_symbol_keys(options = {}) ⇒ Object

Converts hash keys from strings to symbols (if any).

Parameters
  • options - Optional. The hash to convert. Default: {}



35
36
37
# File 'lib/gemsmith/cli_helpers.rb', line 35

def enforce_symbol_keys options = {}
  options.each.with_object({}) { |(key, value), hash| hash[key.to_sym] = value }
end

.open_gem(spec) ⇒ Object

Opens selected gem within default editor.

Parameters
  • spec - Required. The gem specification.



69
70
71
# File 'lib/gemsmith/cli_helpers.rb', line 69

def open_gem spec
  `#{editor} #{spec.full_gem_path}` if spec
end

.pick_gem(gems, name) ⇒ Object

Picks a gem specification for processing.

Parameters
  • gems - Required. The array of gem specifications.

  • name - Required. The gem name to search for.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gemsmith/cli_helpers.rb', line 53

def pick_gem gems, name
  result = ask "Please pick one (or type 'q' to quit):"

  return if result == "q" # Exit early.

  if (1..gems.size).include?(result.to_i)
    Gem::Specification.find_by_name name, gems[result.to_i - 1].version.version
  else
    error "Invalid option: #{result}"
    nil
  end
end

Prints currently installed gem name and version information.

Parameters
  • gems - Required. The array of gem names (i.e. gem specifications).



42
43
44
45
46
47
# File 'lib/gemsmith/cli_helpers.rb', line 42

def print_gems gems
  say "Multiple versions found:"
  gems.each.with_index do |spec, index|
    say "#{index + 1}. #{spec.name} #{spec.version.version}"
  end
end

.process_gem(name, command) ⇒ Object

Processes a gem for given name and command.

Parameters
  • name - Required. The gem name.

  • command - Required. The command to process the gem.



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/gemsmith/cli_helpers.rb', line 84

def process_gem name, command
  specs = Gem::Specification.find_all_by_name name

  case
    when specs.size == 1
      send "#{command}_gem", specs.first
    when specs.size > 1
      print_gems specs
      send "#{command}_gem", pick_gem(specs, name)
    else
      say "Unable to find gem: #{name}"
  end
end

.read_gem(spec) ⇒ Object

Opens selected gem within default browser.

Parameters
  • spec - Required. The gem specification.



76
77
78
# File 'lib/gemsmith/cli_helpers.rb', line 76

def read_gem spec
  `open #{spec.homepage}` if spec
end

Instance Method Details

#editorObject

Answers default editor. NOTE: This will be replaced by the Thor+ gem in the future.



7
8
9
# File 'lib/gemsmith/cli_helpers.rb', line 7

def editor
  ENV["EDITOR"]
end

#gem_class(name = nil) ⇒ Object

Answers the gem class (camel case).

Parameters

  • name - Optional. The gem class. Default: nil



21
22
23
# File 'lib/gemsmith/cli_helpers.rb', line 21

def gem_class name = nil
  @gem_class ||= Thor::Util.camel_case name
end

#gem_name(name = nil) ⇒ Object

Answers the gem name (snake case).

Parameters

  • name - Optional. The gem name. Default: nil



14
15
16
# File 'lib/gemsmith/cli_helpers.rb', line 14

def gem_name name = nil
  @gem_name ||= Thor::Util.snake_case name
end

#template_optionsObject

Answers all gem template options.



26
27
28
# File 'lib/gemsmith/cli_helpers.rb', line 26

def template_options
  @template_options
end