Class: Experiment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/experiment.rb

Constant Summary collapse

@@cache =
{}

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



12
13
14
15
16
17
18
# File 'app/models/experiment.rb', line 12

def method_missing(meth, *args, &block)
  if meth.to_s =~ /^page_(\d+)$/
    page($1)
  else
    super
  end
end

Class Method Details

.alternative(sid, url) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/experiment.rb', line 43

def Experiment.alternative(sid, url)
  key = Experiment.cache_key(sid, url)
  return @@cache[key] if @@cache.include?(key)

  # simple method to stop it getting too big
  Exerpiemtn.flush_cache if @@cache.size > 10000

  e = Experiment.sys(sid).where(:url1=>url).where("experiments.end_date>now()").where("experiments.enabled=1").first

  @@cache[key] = e || false
end

.cache_key(sid, url) ⇒ Object



55
56
57
# File 'app/models/experiment.rb', line 55

def Experiment.cache_key(sid, url)
  return "#{sid}_#{url}"
end

.flush_cacheObject



28
29
30
# File 'app/models/experiment.rb', line 28

def Experiment.flush_cache
  @@cache = {}
end

Instance Method Details

#invoke(option) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'app/models/experiment.rb', line 32

def invoke(option)
  if option!=nil
    alt = option.to_i
  else
    participate = ((100 - percentage_visitors) * 10) < (1 + rand(1000))
    alt = participate ? rand(2) + 1 : 0
  end
  self.update_attributes("impressions#{alt}" => self.send("impressions#{alt}") + 1) if alt != 0 && option==nil
  return [self.send("url#{alt==0 ? 1 : alt}"), alt]
end

#page(n) ⇒ Object



20
21
22
23
24
# File 'app/models/experiment.rb', line 20

def page(n)
  url = self.send("url#{n}")

  Page.sys(self.system_id).where(:full_path=>"/#{url}").first
end