Class: MiddlewareCli::Formatter

Inherits:
Object
  • Object
show all
Extended by:
App
Defined in:
lib/middleware-cli/formatter.rb

Constant Summary collapse

DEFAULT_TEMPLATE_NAME =
"MiddlewareTemplate".freeze

Constants included from App

App::APPLICATION_ROUTE

Class Method Summary collapse

Methods included from App

app_path, copy_file, filtered_list, is_route_middleware?, load_application, middleware_list, relative_app_path

Class Method Details

.create_template(name, path) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/middleware-cli/formatter.rb', line 9

def create_template(name, path)
  begin
    load_application
  rescue LoadError
    puts "Fail to load your application"
    return
  end
  FileUtils.mkdir_p namespaced_path(path) unless Dir.exists?(namespaced_path(path))
  File.open("#{namespaced_path(path, name.underscore)}.rb", 'w') do |file|
    file.write formatted_template(name.camelize)
  end
  puts disclaimer(name.camelize, path)
end

.disclaimer(name, path) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/middleware-cli/formatter.rb', line 46

def disclaimer(name, path)
  %Q(
    Find your Middleware #{name} at #{path}

    Now configure your middleware by adding:
      config.middleware.insert_before "Rails::Logger", #{name}
    in config/application.rb
  )
end

.formatted_template(temp_name) ⇒ Object



27
28
29
# File 'lib/middleware-cli/formatter.rb', line 27

def formatted_template(temp_name)
  template.sub!(DEFAULT_TEMPLATE_NAME, temp_name).gsub(' ' * 10, '')
end

.namespaced_path(*paths) ⇒ Object



23
24
25
# File 'lib/middleware-cli/formatter.rb', line 23

def namespaced_path(*paths)
  [app_path, paths].join('/')
end

.templateObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/middleware-cli/formatter.rb', line 31

def template
  %q(class MiddlewareTemplate
      def initialize(app)
        # app here is our rails app
        @app = app
      end

      def call(env)
        # env variable is a hash comprising of request parameters such as
        # headers, request url, request parameters etc.
        @app.call(env)
      end
    end)
end