Sinatra使用纯Ruby数据库

时间:2012-04-12 17:16:11

标签: ruby sinatra

我想使用一个纯粹的ruby数据库(kirbybase http://www.netpromi.com/files/kirbybase_ruby_manual.html)和Sinatra,所以在ruby中我们会有这样的东西来显示记录:

require 'kirbybase'
db = KirbyBase.new
cars_tbl= db.get_table(:cars)
ferrari = cars_tbl.select { |r| r.recno == 9 }
puts ferrari

或所有记录

results = cars_tbl.select(:name,:race,:color,:recno).sort(+:color)

在没有混淆“rubysh”kirbybase语法的情况下,将它与Sinatra集成的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

要了解sinatra-redis的书,您可以这样做:

require 'sinatra'
require 'kirbybase'

helpers do
  # Create a helper to allow easier access to settings.kirby
  # Name it whatever you want
  def kirby
    settings.kirby
  end
end

configure do
  # Initialise kirby
  set :kirby, KirbyBase.new
end

get '/' do
  cars_tbl = kirby.get_table(:cars)
  ferrari = cars_tbl.select { |r| r.recno == 9 }
  "Car is #{ferrari}"
end