Class: Fabes::Alternative

Inherits:
Object show all
Defined in:
lib/fabes/alternative.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ Alternative

Returns a new instance of Alternative.



6
7
8
9
10
11
12
# File 'lib/fabes/alternative.rb', line 6

def initialize(payload)
  @id = generate_id
  @weight = 0.0
  @participants = 0
  @hits = 0
  @payload = payload
end

Instance Attribute Details

#hitsObject

TODO: move some of these to attr_reader



4
5
6
# File 'lib/fabes/alternative.rb', line 4

def hits
  @hits
end

#idObject

TODO: move some of these to attr_reader



4
5
6
# File 'lib/fabes/alternative.rb', line 4

def id
  @id
end

#participantsObject

TODO: move some of these to attr_reader



4
5
6
# File 'lib/fabes/alternative.rb', line 4

def participants
  @participants
end

#payloadObject

TODO: move some of these to attr_reader



4
5
6
# File 'lib/fabes/alternative.rb', line 4

def payload
  @payload
end

#weightObject

TODO: move some of these to attr_reader



4
5
6
# File 'lib/fabes/alternative.rb', line 4

def weight
  @weight
end

Class Method Details

.create_from(data) ⇒ Object



29
30
31
32
33
34
35
36
37
# 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

Instance Method Details

#increment_hits!Object



19
20
21
22
# File 'lib/fabes/alternative.rb', line 19

def increment_hits!
  @hits = @hits.to_i + 1
  Fabes.db.increment_hits!(id)
end

#increment_participants!Object



14
15
16
17
# File 'lib/fabes/alternative.rb', line 14

def increment_participants!
  @participants = @participants.to_i + 1
  Fabes.db.increment_participants!(id)
end

#update_weightObject



24
25
26
27
# File 'lib/fabes/alternative.rb', line 24

def update_weight
  @weight = calculate_weight
  Fabes.db.update_weight(id, @weight)
end