Module: RailsEngineToolkit::Utils

Defined in:
lib/rails_engine_toolkit/utils.rb

Class Method Summary collapse

Class Method Details

.ask(prompt, default: nil, input: $stdin, output: $stdout) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/rails_engine_toolkit/utils.rb', line 23

def ask(prompt, default: nil, input: $stdin, output: $stdout)
  shown = default.nil? || default.to_s.empty? ? "#{prompt}: " : "#{prompt} [#{default}]: "
  output.print(shown)
  answer = input.gets&.chomp
  return default if answer.nil? || answer.empty?

  answer
end

.ask_yes_no(prompt, default: false, input: $stdin, output: $stdout) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/rails_engine_toolkit/utils.rb', line 32

def ask_yes_no(prompt, default: false, input: $stdin, output: $stdout)
  suffix = default ? ' [Y/n]: ' : ' [y/N]: '
  output.print("#{prompt}#{suffix}")
  answer = input.gets&.chomp.to_s.strip.downcase
  return default if answer.empty?

  %w[y yes].include?(answer)
end

.classify(snake_name) ⇒ Object



11
12
13
# File 'lib/rails_engine_toolkit/utils.rb', line 11

def classify(snake_name)
  snake_name.split('_').map(&:capitalize).join
end

.git_config(key) ⇒ Object



46
47
48
49
# File 'lib/rails_engine_toolkit/utils.rb', line 46

def git_config(key)
  value = `git config #{key} 2>/dev/null`.to_s.strip
  value.empty? ? nil : value
end

.git_remote_urlObject



51
52
53
54
# File 'lib/rails_engine_toolkit/utils.rb', line 51

def git_remote_url
  value = `git remote get-url origin 2>/dev/null`.to_s.strip
  value.empty? ? nil : value
end

.humanize_slug(slug) ⇒ Object



15
16
17
# File 'lib/rails_engine_toolkit/utils.rb', line 15

def humanize_slug(slug)
  slug.split('_').map(&:capitalize).join(' ')
end

.repo_slug_from_path(pathname) ⇒ Object



19
20
21
# File 'lib/rails_engine_toolkit/utils.rb', line 19

def repo_slug_from_path(pathname)
  File.basename(Pathname(pathname).expand_path.to_s)
end

.safe_system(*cmd, chdir:) ⇒ Object

Raises:



41
42
43
44
# File 'lib/rails_engine_toolkit/utils.rb', line 41

def safe_system(*cmd, chdir:)
  success = system(*cmd, chdir: chdir.to_s)
  raise CommandError, "Command failed: #{cmd.join(' ')}" unless success
end

.snake_case?(value) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/rails_engine_toolkit/utils.rb', line 7

def snake_case?(value)
  value.is_a?(String) && value.match?(/\A[a-z0-9_]+\z/)
end