2016-04-27 105 views
0

我想弄清楚如何僅在需要時加載特定的寶石。這裏的場景:只有在導軌3.2需要時才自動加載寶石?

我使用偉大的axlsx創建Excel文件。在我的應用程序,允許這樣做的功能被稱爲只有當用戶問一個Excel文件:

# model 
require 'axlsx' 
class AssessmentRaw < ActiveRecord::Base 
    # fun stuff here 
    def create_excel_file_io 
     xls = Axlsx::Package.new 
     # fun stuff here too 
    end 
end 

# a call in a controller 
@assessment_raw_instance.create_excel_file_io 

使用derailed寶石,我可以看到,axlsx是內存重:

axlsx: 9.8516 MiB (Also required by: /path-to-rails/app/models/assessment_raw) 
    axlsx/workbook/workbook.rb: 3.5391 MiB 
    axlsx/workbook/worksheet/worksheet.rb: 0.3477 MiB 
    axlsx/drawing/drawing.rb: 1.8438 MiB 
    zip: 1.6797 MiB 
    zip/entry: 0.3047 MiB 
    axlsx/stylesheet/styles.rb: 0.8516 MiB 
    htmlentities: 0.5273 MiB 
    htmlentities/flavors: 0.4453 MiB 
     htmlentities/mappings/expanded: 0.4258 MiB 
    axlsx/util/simple_typed_list.rb: 0.4727 MiB 

所以我不知道。 ..如果rails/ruby​​允許延遲加載寶石?

希望我很清楚。 :-) 謝謝!

+0

你有沒有考慮延遲就業工作隊列,像resque?這將完全從您的Rails應用程序中取出,儘管它會產生自己的開銷。 – noel

+0

謝謝您的評論。我的觀點是真的在應用程序啓動時消耗更少的內存。 –

回答

0

在Gemfile中:

gem 'axlsx', :require => false 

在模型:

require 'axlsx'