Module: Swee::Lodder

Defined in:
lib/swee/lodder.rb

Constant Summary collapse

CACHE_FILE_MTIME_DIR =
["models","controllers"]
EXTENSION_NAMES =
[".rb",".erb",".haml",".slim","ymal"]
@@mtime_files_cache =
{}

Class Method Summary collapse

Class Method Details

.allObject



88
89
90
91
# File 'lib/swee/lodder.rb', line 88

def all
  conditional_require
  base_require
end

.app_requireObject

用户配置



71
72
73
74
75
76
77
78
# File 'lib/swee/lodder.rb', line 71

def app_require
  # require_relative './application'
  begin
    require File.expand_path('config', ENV["app_path"])
  rescue LoadError
    raise "未找到config.rb" 
  end
end

.base_requireObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/swee/lodder.rb', line 35

def base_require
  require_relative './support'
  require_relative './config'
  # require_relative './application'

  # patches
  require_relative './patches/logger.rb'

  # app
  require_relative './routes'
  require_relative './helper'
  require_relative './controller_filter'
  require_relative './controller'
  require_relative './view'
  require_relative './app_executor'

  # middlewaves
  require_relative './middlewaves/content_length'
  require_relative './middlewaves/common_logger'
  require_relative './middlewaves/reloader'

  # thin
  require_relative './thin/headers'
  require_relative './thin/request'
  require_relative './thin/response'

  # server
  require_relative './swee_logger'
  require_relative './daemonize'
  require_relative './connection'
  require_relative './server'
  require_relative './exception'

end

.cache_file_mtimeObject



11
12
13
# File 'lib/swee/lodder.rb', line 11

def cache_file_mtime
  CACHE_FILE_MTIME_DIR.each { |d| Lodder.search_app_file(d) }
end

.conditional_requireObject

条件读取



81
82
83
84
85
86
# File 'lib/swee/lodder.rb', line 81

def conditional_require
  # 存在 AR 读取
  unless Gem.find_files("active_record").empty? 
    require 'active_record'
  end
end

.mtime_filesObject



15
16
17
# File 'lib/swee/lodder.rb', line 15

def mtime_files
  @@mtime_files_cache
end

.search_app_file(dir) ⇒ Object

递归寻找目录下所有文件 保存为如下结构(用于 代码修改 reload) filename => mtime



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/swee/lodder.rb', line 22

def search_app_file dir
  app_path = ENV["app_path"]
  Dir.glob("#{app_path}/#{dir}/*") do |file|
    if EXTENSION_NAMES.include? File.extname(file).downcase
      @@mtime_files_cache[file] = File.mtime(file)
    else
      if File.directory?(file)
        search_app_file(dir + "/" + file.split("/").last)
      end
    end
  end
end