Class: MiddlewareCli::ExtractMiddleware

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

Defined Under Namespace

Classes: InvalidMiddlewarePath

Constant Summary collapse

ACTION_DISPATCH_FLASH =
"ActionDispatch::Flash"

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

.constantize(middleware) ⇒ Object



45
46
47
# File 'lib/middleware-cli/extract_middleware.rb', line 45

def constantize(middleware)
   Object.const_get(middleware)
end

.execute(requested_path, options) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/middleware-cli/extract_middleware.rb', line 12

def execute(requested_path, options)
  begin
    load_application
  rescue LoadError
    puts "Fail to load your application"
    return
  end
  if options["name"].blank?
    extract_all(requested_path)
  else
    extract_one(requested_path, options["name"], options["terminal"])
  end
end

.extract_all(path) ⇒ Object



61
62
63
64
65
66
# File 'lib/middleware-cli/extract_middleware.rb', line 61

def extract_all(path)
  middleware_list do | middleware, _|
    perform(middleware, path)
  end
  render_disclaimer(path)
end

.extract_one(path, middleware_name, terminal_output) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/middleware-cli/extract_middleware.rb', line 68

def extract_one(path, middleware_name, terminal_output)
  if middleware_name.blank?
    puts "Please enter any middleware name"
  elsif filtered_list.find { |name| name == middleware_name}
    perform(middleware_name, path, terminal_output)
    render_disclaimer(path) unless terminal_output
  else
    puts "Middleware not found :("
  end
end

.find(middleware) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/middleware-cli/extract_middleware.rb', line 26

def find(middleware)
  file_path, _ = begin
                  find_source(middleware)
                rescue StandardError => error
                  [error.backtrace_locations.first.to_s.match(/:\d*:in/).pre_match, nil]
                end
  raise InvalidMiddlewarePath if file_path.include?("lib/middleware/helper/extract_middleware")
  [file_path, namespace_to_path(middleware)]
end

.find_source(middleware_name) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/middleware-cli/extract_middleware.rb', line 36

def find_source(middleware_name)
  middleware = if is_route_middleware?(middleware_name)
                 constantize(middleware_name.match('.routes').pre_match).routes
               else
                 constantize(middleware_name).send(*init_with(middleware_name))
               end
  middleware.method(:call).source_location
end

.init_with(middleware) ⇒ Object



49
50
51
# File 'lib/middleware-cli/extract_middleware.rb', line 49

def init_with(middleware)
  middleware == ACTION_DISPATCH_FLASH ? ['new'] : ['new', OpenStruct.new]
end

.namespace_to_path(middleware) ⇒ Object



53
54
55
# File 'lib/middleware-cli/extract_middleware.rb', line 53

def namespace_to_path(middleware)
  middleware.split(/\W+/).map(&:underscore).join("/")
end

.perform(middleware, requested_path, terminal_output = false) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/middleware-cli/extract_middleware.rb', line 79

def perform(middleware, requested_path, terminal_output = false)
  begin
    path, folder = find(middleware)
    if terminal_output
      puts `cat #{path}`
    else
      copy_file(path, relative_app_path(requested_path, folder))
    end
  rescue StandardError => e
    return
  end
end

.render_disclaimer(path) ⇒ Object



57
58
59
# File 'lib/middleware-cli/extract_middleware.rb', line 57

def render_disclaimer(path)
  puts "Find your middlewares at '\\#{path}' :)"
end