Class: ForgeCLI::RouteInstaller

Inherits:
Object
  • Object
show all
Defined in:
lib/forge-cli/route_installer.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, module_path) ⇒ RouteInstaller

Returns a new instance of RouteInstaller.



2
3
4
5
# File 'lib/forge-cli/route_installer.rb', line 2

def initialize(app, module_path)
  @app         = app
  @module_path = module_path
end

Instance Method Details

#forge_routesObject



30
31
32
# File 'lib/forge-cli/route_installer.rb', line 30

def forge_routes
  @forge_routes ||= get_routes('forge_')
end

#get_routes(prefix = '') ⇒ Object



34
35
36
37
38
39
# File 'lib/forge-cli/route_installer.rb', line 34

def get_routes(prefix = '')
  file = File.join(@module_path, "#{prefix}routes.rb")
  if File.exist?(file)
    File.open(file, "r").read
  end
end

#install_routes(type = :normal) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/forge-cli/route_installer.rb', line 7

def install_routes(type = :normal)
  file = File.join(@app, 'config', 'routes.rb')
  existing_routes = File.read(file)
  if type.to_sym == :normal
    routes_to_add = routes
    line = "Application.routes.draw do"
    indent = 2
  else
    routes_to_add = self.send("#{type}_routes")
    line = "namespace :#{type} do"
    indent = 4
  end
  routes = routes_to_add.split("\n").map {|r| " " * indent + r }.join("\n")
  updated_routes = existing_routes.gsub(line, "#{line}\n#{routes}")
  File.open(file, 'w') do |f|
    f.puts updated_routes
  end
end

#routesObject



26
27
28
# File 'lib/forge-cli/route_installer.rb', line 26

def routes
  @routes ||= get_routes
end