Gem 关于 Rails 的 console reload!命令

edisonhsu · August 31, 2015 · Last by douxiance replied at September 01, 2015 · 2759 hits

最近在用 grape on rack 的结构开发 API Service,非常想念 rails c 下的 reload!命令 请问同样的功能,在 pry 下怎么实现?

module IRBExtensition
  def reload!
    puts "reloading..."
    ActiveSupport::Dependencies.clear
    nil 
  end 
end

IRB::ExtendCommandBundle.send :include, IRBExtensition

IRB.start

前提是你使用 ActiveSupport,并且利用它的 const missing 机制加载文件。

可以用 Rails 自带的 FileUpdateChecker 进行 reload. 之前写过的,观察 services/views/ 下 .rabl 文件有更改时,自动更新。供参考。(api.rb 同样的做法,也可以自动更新)

rabl_files = Dir.glob(Rails.root.join("app/services/views/v1/**/*.rabl"))

reloader = ActiveSupport::FileUpdateChecker.new(rabl_files){
  lib_reloader.execute
}

Rails.application.reloaders << reloader

ActionDispatch::Reloader.to_prepare {
  reloader.execute_if_updated
}

rails c 里 reload! 同样起效。

You need to Sign in before reply, if you don't have an account, please Sign up first.