SORU
16 ŞUBAT 2011, ÇARŞAMBA


Kullanarak birden fazla dosya ile daha büyük projeler için Sinatra

Sinatra tüm yol işleyicileri eğer bir kimse olarak büyük/küçük denetleyicisi görür eğer doğru anladıysam tek bir dosya içine yazılmış olması gibi görünüyor. Herhangi bir şekilde split içine ayrı bağımsız dosyaları, o zaman diyelim ki, birisi arar "/" - bir eylem idam, ve eğer bir şey anımsatıyor gibi "/posts/2" aldıktan sonra başka bir eylem benzer bir mantık uygulanır PHP?

CEVAP
17 ŞUBAT 2011, PERŞEMBE


Burada kullandığım Sinatra uygulamalar için temel bir şablon. (Büyük uygulamalarım 200 dosyalar bu, sayma satıcı değil taşlar, 75-100 açık yolları kapsayan gibi kırık var. Bu yolların bazı Regexp yolları ek 50 rota deseni de kapsayan.) İnce kullanırken, bu gibi bir uygulama kullanarak çalıştırın:
thin -R config.ru start

EditŞimdi Monk kendi iskelet aşağıda aranan dayanarak idare ediyorumRiblits. Kendi projeleri için temel olarak benim şablon kopyalamak için:

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

# Inside your empty project directory
monk init -s riblits

Düzeni Dosyası:

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'


yardımcıları/init.rb

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

require_relative 'nicebytes'
MyApp.helpers NiceBytes


yardımcıları/kısımlar.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


yardımcıları/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


modelleri 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'


modelleri/kullanıcı.rb

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


yolları init/.rb

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


yolları giriş/.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


yolları/ana.rb

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


görüntüleme/düzenleme.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

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • BeginnersTech

    BeginnersTec

    8 NİSAN 2011
  • Hey Nadine

    Hey Nadine

    24 Kasım 2006
  • majesticdubstep

    majesticdubs

    3 Kasım 2011