1
我正在將Rails應用程序轉換爲Sinatra。該應用程序依賴於支持虛擬屬性的ActiveRecord,但是,我正努力在DataMapper中尋找類似的支持。使用Sinatra和Datamapper的虛擬屬性
在Rails中,我使用了一種算法來根據分數生成分數和排序記錄。虛擬屬性是這一過程的核心。有誰知道如何在Sinatra/DM中複製這個功能?
我接受任何解決方法。重構可能會稍後,因爲我現在只對功能感興趣。
理論上的解決方案將工作是這樣的:
require 'sinatra'
require 'data_mapper'
require 'haml'
DataMapper::setup(:default,"sqlite3://#{Dir.pwd}/example.db")
class Link
include DataMapper::Resource
property :id, Serial
property :title, Text
property :url, String
property :points, Integer, :default => 0
property :created_at, Time
attr_accessor :score
def calculate_score
time_elapsed = (Time.now - self.created_at)/3600
self.score = (self.points-1)/(time_elapsed+2)**1.8
end
end
DataMapper.finalize.auto_upgrade!
get '/hot' do
@links = Link.all :order => :score.desc
haml :index
end