Class: Sincli::Generators::Config

Inherits:
Base
  • Object
show all
Defined in:
lib/sincli/generators/config.rb

Instance Attribute Summary

Attributes inherited from Base

#name, #options

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Sincli::Generators::Base

Instance Method Details

#db_configObject



29
30
31
32
33
34
# File 'lib/sincli/generators/config.rb', line 29

def db_config
  return unless db = @options[:db]

  @options[:adapter] = db == 'pg' ? 'postgresql' : db
  template('database.yml.tt', 'config/database.yml')
end

#environmentObject



42
43
44
45
46
47
48
49
# File 'lib/sincli/generators/config.rb', line 42

def environment
  copy_file('environment.rb', 'config/environment.rb')
  @extensions = extensions.map do |e|
    ext = e == 'json' ? e.upcase : e.camelize
    "Sinatra::#{ext}"
  end
  template('application.rb.tt', 'config/application.rb')
end

#generateObject



4
5
6
7
8
9
10
11
# File 'lib/sincli/generators/config.rb', line 4

def generate
  Dir.mkdir('config')
  sec_key
  db_config
  environment
  puma_config
  locales
end

#localesObject



51
52
53
# File 'lib/sincli/generators/config.rb', line 51

def locales
  directory('locales', 'config/locales')
end

#puma_configObject



36
37
38
39
40
# File 'lib/sincli/generators/config.rb', line 36

def puma_config
  return unless @options[:server] == 'puma'

  copy_file('puma.rb', 'config/puma.rb')
end

#sec_keyObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/sincli/generators/config.rb', line 18

def sec_key
  begin
    require 'securerandom'
    sec_key = SecureRandom.hex(64)
  rescue LoadError, NotImplementedError
    # SecureRandom raises a NotImplementedError if no random device is available
    sec_key = '%064x' % Kernel.rand(2**256 - 1)
  end
  create_file('config/.secret_key', sec_key)
end