Module: Raboot::Utils

Included in:
CLI
Defined in:
lib/raboot/utils.rb

Instance Method Summary collapse

Instance Method Details

#rename_app(new_name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/raboot/utils.rb', line 5

def rename_app(new_name)
  replace_text(
    new_name + '/config/application.rb',
    /module\s[a-zA-Z0-9]+/,
    'module ' + new_name.capitalize
  )
  replace_text(
    new_name + '/config/environments/production.rb',
    /config.active_job.queue_name_prefix\s=\s"[a-zA-Z0-9]+_\#\{Rails\.env\}"/,
    'config.active_job.queue_name_prefix = "'+ new_name + '_#{Rails.env}"'
  )
  replace_text(
    new_name + '/config/initializers/session_store.rb',
    /Rails.application.config.session_store\s:cookie_store,\skey:\s'_[a-zA-Z0-9]+_session'/,
    "Rails.application.config.session_store :cookie_store, key: '_" + new_name + "_session'"
  )
end

#replace_text(path, old_pattern, new_pattern) ⇒ Object



23
24
25
26
27
# File 'lib/raboot/utils.rb', line 23

def replace_text(path, old_pattern, new_pattern)
  buffer = File.open(path, "r") { |f| f.read() }
  buffer.gsub!(old_pattern, new_pattern)
  File.open(path, "w") { |f| f.write(buffer) }
end

#validate_name?(new_name) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/raboot/utils.rb', line 29

def validate_name?(new_name)
  reserved_names = %w[application destroy plugin runner test]

  if new_name.strip.empty?
    raise Thor::Error, "[Error] Application name can't be blank."
  elsif new_name =~ /^\d/
    raise Thor::Error, '[Error] Please give a name which does not start with numbers.'
  elsif reserved_names.include?(new_name.downcase)
    raise Thor::Error, '[Error] Please give a name which does not match any of the reserved Rails keywords.'
  end

  return true
end