Module: Gemsmith::CLIHelpers

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

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: {}



33
34
35
# File 'lib/gemsmith/cli_helpers.rb', line 33

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.



67
68
69
# File 'lib/gemsmith/cli_helpers.rb', line 67

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.



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

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).



40
41
42
43
44
45
# File 'lib/gemsmith/cli_helpers.rb', line 40

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.



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

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.



74
75
76
# File 'lib/gemsmith/cli_helpers.rb', line 74

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.



5
6
7
# File 'lib/gemsmith/cli_helpers.rb', line 5

def editor
  ENV["EDITOR"]
end

#gem_class(name = nil) ⇒ Object

Answers the gem class (camel case).

Parameters

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



19
20
21
# File 'lib/gemsmith/cli_helpers.rb', line 19

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



12
13
14
# File 'lib/gemsmith/cli_helpers.rb', line 12

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

#template_optionsObject

Answers all gem template options.



24
25
26
# File 'lib/gemsmith/cli_helpers.rb', line 24

def template_options
  @template_options
end