【问题标题】:sinatra helper in external file外部文件中的 sinatra 助手
【发布时间】:2011-08-02 18:16:16
【问题描述】:

我的主要 Sinatra project_name.rb 中有很多助手,我想将它们删除到外部文件中,这样做的最佳做法是什么?

来自 ./preject_name.rb

   helpers do
     ...#bunch of helpers
   end

例如 ./helpers/something.rb

谢谢你

【问题讨论】:

    标签: sinatra


    【解决方案1】:

    简单推荐的方法:

    module ApplicationHelper
    
    # methods
    
    end
    
    class Main < Sinatra::Base
    
      helpers ApplicationHelper
    
    end
    

    【讨论】:

      【解决方案2】:

      唉,如果您像我一样构建一个模块化 Sinatra 应用程序,它比简单地将helpers 移出另一个文件要复杂一些。

      我让它工作的唯一方法如下。

      首先在你的应用程序中(我会称之为my_modular_app.rb

      require 'sinatra/base'
      require 'sinatra/some_helpers'
      
      class MyModularApp < Sinatra::Base
        helpers Sinatra::SomeHelpers
      
        ...
      
      end
      

      然后创建文件夹结构./lib/sinatra/并创建some_helpers.rb如下:

      require 'sinatra/base'
      
      module Sinatra
        module SomeHelpers
      
          def help_me_world
            logger.debug "hello from a helper"
          end
      
        end
      
        helpers SomeHelpers
      
      end
      

      这样做,您可以简单地将所有助手拆分为多个文件,从而在更大的项目中提供更高的清晰度。

      【讨论】:

      • 我觉得可以更简单。请参阅下面的答案。
      【解决方案3】:

      正如你自己所说:

      helpers 块移动到另一个文件中,并将require 移动到您需要的位置。

      #helpers.rb
      helpers do
      ...
      end
      
      #project_name.rb
      require 'path/to/helpers.rb'
      

      【讨论】:

      • 大声笑,很简单 :) 我为什么不先尝试 :) 谢谢,我用的就像require "#{File.dirname(__FILE__)}/helpers/helpers.rb"
      • 当您使用 Ruby 1.9.2 时,您不妨使用 require_relative 'helpers/helpers' 而不是 File-construct
      • 有什么优势? ...我有点希望这个项目在所有机器上运行,将在 github 上分享:)
      • 不确定它是否有任何影响,除了看起来更好/更清洁恕我直言。
      • 看我的回答。我认为我们应该使用模块,尤其是在我们开发模块化 Sinatra 应用程序时。
      【解决方案4】:

      @DaveSag 提供的答案似乎漏掉了一些东西。应该在my_modular_app.rb开头加一行:

      $:.unshift File.expand_path('../lib', __FILE__)  # add ./lib to $LOAD_PATH
      
      require 'sinatra/base'
      require 'sinatra/some_helpers' # this line breaks unless line 1 is added.
      
      # more code below...
      

      另外,如果有人像我一样喜欢“古典风格”,以下是给你的:)

      app.rb

      $:.unshift File.expand_path('../lib', __FILE__)
      
      require 'sinatra'
      require 'sinatra/some_helpers'
      
      get '/' do
        hello_world
      end
      

      lib/sinatra/some_helpers.rb

      module Sinatra
        module SomeHelper
          def hello_world
            "Hello World from Helper!!"
          end
        end
      
        helpers SomeHelper
      end
      

      【讨论】:

        【解决方案5】:

        我刚刚将require_relative './lib/sinatra/helpers' 添加到我的config.ru 中,仅此而已。

        所以它看起来像这样:

        require_relative './config/environment'
        require_relative './lib/sinatra/helpers'
        use ProjectsController
        run ApplicationController
        

        我的./lib/sinatra/helpers.rb 文件甚至不是一个模块,我没有使用任何要求或包含在其中。我可以直接在这个文件中定义方法并在整个应用程序中使用它们。

        @kgpdeveloper 的答案对我不起作用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-08-25
          • 1970-01-01
          • 2011-08-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多