class Fabes::Alternative
Attributes
hits[RW]
TODO: move some of these to attr_reader
id[RW]
TODO: move some of these to attr_reader
participants[RW]
TODO: move some of these to attr_reader
payload[RW]
TODO: move some of these to attr_reader
weight[RW]
TODO: move some of these to attr_reader
Public Class Methods
create_from(data)
click to toggle source
# File lib/fabes/alternative.rb, line 29 def self.create_from(data) alternative = new(data.delete :payload) data.each do |k, v| alternative.send "#{k}=", v end alternative rescue raise 'Error creating an alternative' end
new(payload)
click to toggle source
# File lib/fabes/alternative.rb, line 6 def initialize(payload) @id = generate_id @weight = 0.0 @participants = 0 @hits = 0 @payload = payload end
Public Instance Methods
increment_hits!()
click to toggle source
# File lib/fabes/alternative.rb, line 19 def increment_hits! @hits = @hits.to_i + 1 Fabes.db.increment_hits!(id) end
increment_participants!()
click to toggle source
# File lib/fabes/alternative.rb, line 14 def increment_participants! @participants = @participants.to_i + 1 Fabes.db.increment_participants!(id) end
update_weight()
click to toggle source
# File lib/fabes/alternative.rb, line 24 def update_weight @weight = calculate_weight Fabes.db.update_weight(id, @weight) end
Private Instance Methods
calculate_weight()
click to toggle source
# File lib/fabes/alternative.rb, line 50 def calculate_weight @hits.to_f / @participants.to_f end
generate_id()
click to toggle source
# File lib/fabes/alternative.rb, line 45 def generate_id #TODO: maybe this could be easier with a redis counter? rand(36**10).to_s(36) end