Class: Modesty::RedisData::ExperimentData

Inherits:
Datastore::ExperimentData show all
Defined in:
lib/modesty/datastore/redis.rb

Instance Method Summary collapse

Methods inherited from Datastore::ExperimentData

#initialize

Constructor Details

This class inherits a constructor from Modesty::Datastore::ExperimentData

Instance Method Details

#dataObject



134
135
136
# File 'lib/modesty/datastore/redis.rb', line 134

def data
  Modesty.data
end

#get_cached_alternative(identity) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/modesty/datastore/redis.rb', line 151

def get_cached_alternative(identity)
  @experiment.alternatives.each do |alt|
    if data.sismember(self.key(alt), identity)
      return alt
    end
  end
  return nil
end

#key(*args) ⇒ Object



138
139
140
# File 'lib/modesty/datastore/redis.rb', line 138

def key(*args)
  RedisData.keyify(:experiments, @experiment.slug, *args)
end

#num_users(alt = nil) ⇒ Object



168
169
170
171
172
173
174
175
176
# File 'lib/modesty/datastore/redis.rb', line 168

def num_users(alt=nil)
  if alt.nil?
    @experiment.alternatives.map do |alt|
      data.scard(self.key(alt)).to_i
    end.sum
  else
    data.scard(self.key(alt)).to_i
  end
end

#register!(alt, identity) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/modesty/datastore/redis.rb', line 142

def register!(alt, identity)
  old_alt = self.get_cached_alternative(identity)
  if old_alt
    data.srem(self.key(old_alt), identity)
  end
  data.sadd(self.key(alt), identity)
  return alt
end

#users(alt = nil) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/modesty/datastore/redis.rb', line 160

def users(alt=nil)
  if alt.nil? #return the union
    data.sunion(*@experiment.alternatives.map {|a| self.key(a) })
  else
    data.smembers(self.key(alt))
  end.map(&:to_i)
end