简体   繁体   English

如何使gem中的功能可用于Sinatra视图?

[英]How do I make functions in a gem available to Sinatra views?

The question here asks how to extract Rails view helper functions into a gem, and the accept answer is pretty good. 这里的问题询问如何将Rails视图助手功能提取到gem中,接受答案是非常好的。

I am wondering - how to do the same for Sinatra? 我想知道-对于Sinatra怎么做? I'm making a gem that has a bunch of helper functions defined in a module, and I'd like to make these functions available to Sinatra views. 我正在制作一个在模块中定义了许多辅助函数的gem,我想使这些函数可用于Sinatra视图。 But whatever I try, I cannot seem to access the functions, I just get a undefined local variable or method error. 但是无论我如何尝试,我似乎都无法访问这些函数,只是得到了undefined local variable or method错误。

So far, my gem structure looks like this (other stuff like gemspec omitted): 到目前为止,我的gem结构看起来像这样(其他诸如gemspec之类的东西省略了):

cool_gem/
  lib/
    cool_gem/
      helper_functions.rb
      sinatra.rb 
  cool_gem.rb

In cool_gem.rb , I have: cool_gem.rb ,我有:

if defined?(Sinatra) and Sinatra.respond_to? :register
  require 'cool_gem/sinatra'
end

In helper_functions.rb , I have: helper_functions.rb ,我有:

module CoolGem
  module HelperFunctions

    def heading_tag(text)
      "<h1>#{text}</h1>"
    end

    # + many more functions
  end
end

In sinatra.rb , I have: sinatra.rb ,我有:

require 'cool_gem/helper_functions'

module CoolGem
  module Sinatra

    module MyHelpers
      include CoolGem::HelperFunctions
    end

    def self.registered(app)
      app.helpers MyHelpers
    end

  end
end

This doesn't work. 这行不通。 Where am I going wrong? 我要去哪里错了?

(And in case you're wondering, yes, I need the helper functions in a separate file. I plan to make the gem compatible with Rails as well, so I want to keep the functions isolated/de-coupled if possible). (而且,如果您想知道,是的,我需要在单独的文件中添加辅助函数。我还计划使gem与Rails兼容,因此,如果可能的话,我希望将这些函数隔离/解耦)。

You're mainly just missing the call to Sinatra.register (in cool_gem/sinatra.rb ): 您主要只是错过了对Sinatra.register的调用(在cool_gem/sinatra.rb ):

require 'sinatra/base'
require 'cool_gem/helper_functions'

module CoolGem
  # you could just put this directly in the CoolGem module if you wanted,
  # rather than have a Sinatra sub-module
  module Sinatra

    def self.registered(app)
      #no need to create another module here
      app.helpers CoolGem::HelperFunctions
    end

  end
end

# this is what you're missing:
Sinatra.register CoolGem::Sinatra

Now any classic style Sinatra app that requires cool_gem will have the helpers available. 现在,任何需要cool_gem经典风格的Sinatra应用程序cool_gem将提供帮助器。 If you use the modular style you'll also need to call register CoolGem::Sinatra inside the Sinatra::Base subclass. 如果您使用模块化样式,则还需要在Sinatra::Base子类中调用register CoolGem::Sinatra

In this case, if you are just providing some helper methods, an easier way might be to just use the helpers method (again in cool_gem/sinatra.rb ): 在这种情况下,如果您仅提供一些辅助方法, cool_gem/sinatra.rb简单的方法可能是仅使用helpers方法(同样在cool_gem/sinatra.rb ):

require 'sinatra/base'
require 'cool_gem/helper_functions'

Sinatra.helpers CoolGem::HelperFunctions

Now the methods will be available in classic style apps, and modular style apps will need to call helpers CoolGem::HelperFunctions . 现在,这些方法将在经典样式的应用程序中可用,而模块化样式的应用程序将需要调用helpers CoolGem::HelperFunctions This is a bit simpler, but if you are adding methods to the DSL context you will need to use registered as above. 这有点简单,但是如果要将方法添加到DSL上下文中,则需要使用上述registered方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM