我有一个ruby应用程序/网站,用户可以扫描二维码并捐赠比特币钱包。每当捐赠发生时,就会创建一个事务和事件,并更新比特币钱包值。这是下面的代码
require 'date'
module Server
class Event
include DataMapper::Resource
property :id, Serial
property :duration, Float, required: true, default: ->(x, y) { 1.0 }
property :created_at, DateTime, required: true, index: true, default: ->(x, y) { DateTime.now }
property :valve, Integer, required: true
belongs_to :transaction, index: true
belongs_to :bucket, index: true
def self.register(wallet, amount, hash)
bucket = Bucket.first(wallet: wallet)
unless bucket
raise DataMapper::ObjectNotFoundError, "Bucket not found for wallet #{wallet}"
end
tr = Transaction.create!({
wallet: wallet,
amount: amount,
thash: hash,
created_at: DateTime.now
})
ev = Event.create!({
valve: bucket.valve,
duration: calculate_duration(amount * bucket.multiplier),
transaction: tr,
bucket: bucket,
created_at: tr.created_at
})
bucket.update(amount: bucket.amount + amount)
end
def amount
self.transaction { |x| x.amount }
end
def self.calculate_duration(amount)
[[amount, Config.server.min_duration].max, Config.server.max_duration].min
end
def simple
return {
valve: valve,
duration: duration_millis
}
end
def duration_millis
(duration * 1000).to_i
end
def to_hash(is_simple)
if is_simple
simple
else
attributes.merge simple
end
end
def self.latest(valve = nil)
if valve.nil?
Event.first(order: [:created_at.asc])
else
Event.first(order: [:created_at.asc], valve: valve)
end
end
def self.latest!(valve = nil)
result = self.latest(valve)
result.destroy unless result.nil?
result
end
end
end
总共有3个钱包。所以0,1和& 2.我的问题是,在调用存储桶更新功能后,如何根据使用if语句更新钱包来播放特定声音。