Sha256: f0155d5532d40ea30ce13d08ea83ec9848baa07571647f7ea5ec11f903f1615e

Contents?: true

Size: 991 Bytes

Versions: 17

Compression:

Stored size: 991 Bytes

Contents

class ForgeCLI::RouteInstaller
  def initialize(app, module_path)
    @app         = app
    @module_path = module_path
  end

  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

  def routes
    @routes ||= get_routes
  end

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

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

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
forge-cli-0.1.10 lib/forge-cli/route_installer.rb
forge-cli-0.1.9 lib/forge-cli/route_installer.rb
forge-cli-0.1.8 lib/forge-cli/route_installer.rb
forge-cli-0.1.7 lib/forge-cli/route_installer.rb
forge-cli-0.1.6 lib/forge-cli/route_installer.rb
forge-cli-0.1.5 lib/forge-cli/route_installer.rb
forge-cli-0.1.4 lib/forge-cli/route_installer.rb
forge-cli-0.1.3 lib/forge-cli/route_installer.rb
forge-cli-0.1.2 lib/forge-cli/route_installer.rb
forge-cli-0.1.1 lib/forge-cli/route_installer.rb
forge-cli-0.1.0 lib/forge-cli/route_installer.rb
forge-cli-0.0.18 lib/forge-cli/route_installer.rb
forge-cli-0.0.17 lib/forge-cli/route_installer.rb
forge-cli-0.0.16 lib/forge-cli/route_installer.rb
forge-cli-0.0.15 lib/forge-cli/route_installer.rb
forge-cli-0.0.14 lib/forge-cli/route_installer.rb
forge-cli-0.0.13 lib/forge-cli/route_installer.rb