Class: Starter::Orms

Inherits:
Object
  • Object
show all
Defined in:
lib/starter/builder/orms.rb

Class Method Summary collapse

Class Method Details

.add_migration(klass_name, resource) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/starter/builder/orms.rb', line 58

def add_migration(klass_name, resource)
  load_orm
  return if @orm.nil?

  file_name = "#{Time.now.strftime('%Y%m%d%H%m%S')}_create_#{klass_name.downcase}.rb"
  migration_dest = File.join(Dir.pwd, 'db', 'migrate', file_name)
  FileFoo.write_file(migration_dest, migration(klass_name, resource))
end

.build(name, dest, orm) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/starter/builder/orms.rb', line 6

def build(name, dest, orm)
  load_orm(orm: orm)
  return if @orm.nil?

  @name = name

  if @orm == 'ar' || @orm == 'activerecord'
    # Fixes pooling
    add_middleware(dest, 'ActiveRecord::Rack::ConnectionManagement')
    build_standalone_migrations(File.join(dest, '.standalone_migrations'))
  end

  build_initializer(File.join(dest, 'config', 'initializers'))
  build_config(File.join(dest, 'config'))
  append_to_file(File.join(dest, 'Rakefile'), rakefile)
  append_to_file(File.join(dest, 'Gemfile'), gemfile)
  prepare_for_migrations(File.join(dest, 'db'))
end

.configObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/starter/builder/orms.rb', line 32

def config
  <<-FILE.strip_heredoc
  # ActiveRecord Database Configuration
  development:
    adapter: #{adapter}
    encoding: unicode
    timeout: 5000
    user: postgres
    database: #{@name}_development

  test:
    adapter: #{adapter}
    encoding: unicode
    timeout: 5000
    user: postgres
    database: #{@name}_test

  production:
    adapter: #{adapter}
    encoding: unicode
    timeout: 5000
    user: postgres
    database: #{@name}_production
  FILE
end

.load_orm(orm: ::Starter::Config.read[:orm]) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/starter/builder/orms.rb', line 67

def load_orm(orm: ::Starter::Config.read[:orm])
  @orm = orm

  case @orm
  when 'sequel'
    require 'starter/builder/templates/sequel'
    extend(::Starter::Templates::Sequel)
  when 'activerecord', 'ar'
    require 'starter/builder/templates/activerecord'
    extend(::Starter::Templates::ActiveRecord)
  else
    @orm = nil
  end
end

.standalone_migrationsObject



25
26
27
28
29
30
# File 'lib/starter/builder/orms.rb', line 25

def standalone_migrations
  <<-FILE.strip_heredoc
  config:
    database: config/database.yml
  FILE
end