2016-04-27 46 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'