ruby - 通过多个文件将 Sinatra 用于更大的项目

标签 ruby sinatra

似乎在 Sinatra 中,所有路由处理程序都被写入单个文件,如果我理解正确的话,它充当一个大/小 Controller 。有什么方法可以将它拆分成单独的独立文件,所以当假设有人调用“/”时 - 执行一个操作,如果收到类似“/posts/2”的 smth,那么另一个操作 - 在 PHP 中应用类似的逻辑?

最佳答案

这是我使用的 Sinatra 应用程序的基本模板。 (我的大型应用程序有 200 多个文件像这样分解,不包括供应商的 gem,涵盖 75-100 条显式路由。其中一些路由是 Regexp 路由,覆盖了额外的 50 多个路由模式。)使用 Thin 时,你运行一个像这样的应用程序使用:
thin -R config.ru start

编辑:我现在维护我自己的Monk基于以下称为 Riblits 的骨架。要使用它来复制我的模板作为您自己项目的基础:

# Before creating your project
monk add riblits git://github.com/Phrogz/riblits.git

# Inside your empty project directory
monk init -s riblits

文件布局:

config.ru
app.rb
helpers/
  init.rb
  partials.rb
models/
  init.rb
  user.rb
routes/
  init.rb
  login.rb
  main.rb
views/
  layout.haml
  login.haml
  main.haml

 
config.ru

root = ::File.dirname(__FILE__)
require ::File.join( root, 'app' )
run MyApp.new


app.rb

# encoding: utf-8
require 'sinatra'
require 'haml'

class MyApp < Sinatra::Application
  enable :sessions

  configure :production do
    set :haml, { :ugly=>true }
    set :clean_trace, true
  end

  configure :development do
    # ...
  end

  helpers do
    include Rack::Utils
    alias_method :h, :escape_html
  end
end

require_relative 'models/init'
require_relative 'helpers/init'
require_relative 'routes/init'


helpers/init.rb

# encoding: utf-8
require_relative 'partials'
MyApp.helpers PartialPartials

require_relative 'nicebytes'
MyApp.helpers NiceBytes


helpers/partials.rb

# encoding: utf-8
module PartialPartials
  def spoof_request(uri,env_modifications={})
    call(env.merge("PATH_INFO" => uri).merge(env_modifications)).last.join
  end

  def partial( page, variables={} )
    haml page, {layout:false}, variables
  end
end


helpers/nicebytes.rb

# encoding: utf-8
module NiceBytes
  K = 2.0**10
  M = 2.0**20
  G = 2.0**30
  T = 2.0**40
  def nice_bytes( bytes, max_digits=3 )
    value, suffix, precision = case bytes
      when 0...K
        [ bytes, 'B', 0 ]
      else
        value, suffix = case bytes
          when K...M then [ bytes / K, 'kiB' ]
          when M...G then [ bytes / M, 'MiB' ]
          when G...T then [ bytes / G, 'GiB' ]
          else            [ bytes / T, 'TiB' ]
        end
        used_digits = case value
          when   0...10   then 1
          when  10...100  then 2
          when 100...1000 then 3
          else 4
        end
        leftover_digits = max_digits - used_digits
        [ value, suffix, leftover_digits > 0 ? leftover_digits : 0 ]
    end
    "%.#{precision}f#{suffix}" % value
  end
  module_function :nice_bytes  # Allow NiceBytes.nice_bytes outside of Sinatra
end


models/init.rb

# encoding: utf-8
require 'sequel'
DB = Sequel.postgres 'dbname', user:'bduser', password:'dbpass', host:'localhost'
DB << "SET CLIENT_ENCODING TO 'UTF8';"

require_relative 'users'


models/user.rb

# encoding: utf-8
class User < Sequel::Model
  # ...
end


routes/init.rb

# encoding: utf-8
require_relative 'login'
require_relative 'main'


routes/login.rb

# encoding: utf-8
class MyApp < Sinatra::Application
  get "/login" do
    @title  = "Login"
    haml :login
  end

  post "/login" do
    # Define your own check_login
    if user = check_login
      session[ :user ] = user.pk
      redirect '/'
    else
      redirect '/login'
    end
  end

  get "/logout" do
    session[:user] = session[:pass] = nil
    redirect '/'
  end
end


routes/main.rb

# encoding: utf-8
class MyApp < Sinatra::Application
  get "/" do
    @title = "Welcome to MyApp"        
    haml :main
  end
end


views/layout.haml

!!! XML
!!! 1.1
%html(xmlns="http://www.w3.org/1999/xhtml")
  %head
    %title= @title
    %link(rel="icon" type="image/png" href="/favicon.png")
    %meta(http-equiv="X-UA-Compatible" content="IE=8")
    %meta(http-equiv="Content-Script-Type" content="text/javascript" )
    %meta(http-equiv="Content-Style-Type" content="text/css" )
    %meta(http-equiv="Content-Type" content="text/html; charset=utf-8" )
    %meta(http-equiv="expires" content="0" )
    %meta(name="author" content="MeWho")
  %body{id:@action}
    %h1= @title
    #content= yield

关于ruby - 通过多个文件将 Sinatra 用于更大的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5015471/

相关文章:

ruby - 如何从 Padrino 中的模型访问应用程序设置

ruby-on-rails - 如何以编程方式将 ActiveModel 验证器从一个模型复制到另一个模型?

php - 如何将一些用 Ruby 编写的功能混合到 WordPress 站点中?

ruby-on-rails - 如何使用 YARD 记录 Rake 任务?

ruby 文件::目录?问题

ruby - Sinatra on Rack under Passenger 返回 0 字节页面

ruby-on-rails - 使用 Passenger 和 Nginx 将 Rails 应用程序部署到子 URI?

ruby - mac os x 10.9-brew 服务器需要 sinatra 错误

ruby - Sinatra EC2 部署安全组错误

ruby - Array#sample 随机数生成器