اذهب إلى المحتوى
  • 0

كيف أستعمل Sinatra للمشاريع الكبيرة؟

Simoh

السؤال

Recommended Posts

  • 0

مرحبا، يُعتبر إطار Sinatra من أفضل أطر العمل الخاصّة بالويب، ويمكن أن تستعمله لبناء تطبيقات ويب بسرعة وبساطة، ويمكنك أن تبنيّ به مشاريع صغيرة ومُتوسّطة أمّا إذا كان مشروعك ضخما ويتطلّب الكثير من الملفّات فإطار Sinatra وحده لا يكفي، ولكن يُمكن أن تستعين ببعض الأطر المبنيّة عليه إذا كنت ترغب في برمجة وتطوير مشاريع كبيرة، ويُعتبر إطار padrinorb  من أهمّ هذه الأطر؛ وهوّ إطار مبنيّ على Sinatra والغرض منه إنشاء مشاريع ضخمة باستعمال لغة روبي مع الحفاظ على جماليّة وفلسفة إطار Sinatra.

مميّزات padrinorb تتمثّل في كونه يمتلك العديد من الوظائف الجاهزة وبعض المُساعدات، مثل المولّدات Generators لتوليد المتحكّمات والنماذج (models, controllers) للعمل بطريقة MVC.

يُمكنك الاطّلاع على المزيد من المميزات في الصفحة الرسميّة لإطار padrinorb.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

sinatra من أطر العمل التي لا تلقى إقبالاً كبيراً في العالم العربي مع الأسف، لكن كمثيلتها Rails المبرمجة بلغة روبي، حيث يمكننا تشغيل التطبيق المبرمج بـ sinatra عن طريق الأمر التالي:

thin -R config.ru start

 
# قبل إنشاء المشروع ننفذ الأمر التالي
monk add riblits git://github.com/Phrogz/riblits.git

#داخل الملف الفارغ المنشأ
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

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...