Class: Blackbeard::CohortData

Inherits:
Object
  • Object
show all
Includes:
ConfigurationMethods
Defined in:
lib/blackbeard/cohort_data.rb

Instance Method Summary collapse

Methods included from ConfigurationMethods

#config, #db, #guest_method, included, #tz

Constructor Details

#initialize(cohort) ⇒ CohortData

Returns a new instance of CohortData.



7
8
9
# File 'lib/blackbeard/cohort_data.rb', line 7

def initialize(cohort)
  @cohort = cohort
end

Instance Method Details

#add_with_force(uid, hour) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/blackbeard/cohort_data.rb', line 31

def add_with_force(uid, hour)
  prior_hour_id = db.hash_get(participants_hash_key, uid)

  # Force not necessary
  return add_without_force(uid, hour) unless prior_hour_id

  hour_id = hour_id(hour)
  # No change in cohort status
  return true if prior_hour_id == hour_id

  # Deincrement old, increment new
  db.hash_increment_by(hours_hash_key, prior_hour_id, -1)
  db.hash_increment_by(hours_hash_key, hour_id, 1)
  db.hash_set(participants_hash_key, uid, hour_id)
  true
end

#add_without_force(uid, hour) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/blackbeard/cohort_data.rb', line 48

def add_without_force(uid, hour)
  hour_id = hour_id(hour)
  # Check if uid is alreaty in cohort
  return false unless db.hash_key_set_if_not_exists(participants_hash_key, uid, hour_id)
  db.hash_increment_by(hours_hash_key, hour_id, 1)
  true
end

#hour_id_for_participant(uid) ⇒ Object



11
12
13
# File 'lib/blackbeard/cohort_data.rb', line 11

def hour_id_for_participant(uid)
  db.hash_get(participants_hash_key, uid)
end

#participants_for_day(date) ⇒ Object



19
20
21
22
23
24
# File 'lib/blackbeard/cohort_data.rb', line 19

def participants_for_day(date)
  start_of_day = date.to_time
  hours_in_day = Array(0..23).map{|x| start_of_day + (3600 * x) }
  participants_by_hour = participants_for_hours(hours_in_day)
  participants_by_hour.reduce(:+)
end

#participants_for_hour(time) ⇒ Object



15
16
17
# File 'lib/blackbeard/cohort_data.rb', line 15

def participants_for_hour(time)
  db.hash_get(hours_hash_key, hour_id(time)).to_i
end

#participants_for_hours(hours) ⇒ Object



26
27
28
29
# File 'lib/blackbeard/cohort_data.rb', line 26

def participants_for_hours(hours)
  hour_ids = hours.map{ |hour| hour_id(hour) }
  db.hash_multi_get(hours_hash_key, *hour_ids).map{|s| s.to_i }
end