Skip to content

Commit

Permalink
Merge pull request #22 from ryanlecompte/master
Browse files Browse the repository at this point in the history
Minor code cleanup
  • Loading branch information
andrew committed Oct 9, 2011
2 parents 610531e + dc05add commit b03ebcd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
19 changes: 8 additions & 11 deletions lib/split/alternative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(name, experiment_name, counters = {})
@experiment_name = experiment_name
@participant_count = counters['participant_count'].to_i
@completed_count = counters['completed_count'].to_i
if name.class == Hash
if Hash === name
@name = name.keys.first
@weight = name.values.first
else
Expand Down Expand Up @@ -72,11 +72,12 @@ def z_score
end

def save
if Split.redis.hgetall("#{experiment_name}:#{name}")
Split.redis.hset "#{experiment_name}:#{name}", 'participant_count', @participant_count
Split.redis.hset "#{experiment_name}:#{name}", 'completed_count', @completed_count
key = "#{experiment_name}:#{name}"
if Split.redis.hgetall(key)
Split.redis.hset key, 'participant_count', @participant_count
Split.redis.hset key, 'completed_count', @completed_count
else
Split.redis.hmset "#{experiment_name}:#{name}", 'participant_count', 'completed_count', @participant_count, @completed_count
Split.redis.hmset key, 'participant_count', 'completed_count', @participant_count, @completed_count
end
end

Expand Down Expand Up @@ -106,15 +107,11 @@ def self.create(name, experiment_name)
end

def self.valid?(name)
string?(name) or hash_with_correct_values?(name)
end

def self.string?(name)
name.class == String
String === name || hash_with_correct_values?(name)
end

def self.hash_with_correct_values?(name)
name.class == Hash && name.keys.first.class == String && Float(name.values.first) rescue false
Hash === name && String === name.keys.first && Float(name.values.first) rescue false
end
end
end
2 changes: 1 addition & 1 deletion lib/split/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Configuration
attr_accessor :robot_regex
attr_accessor :ignore_ip_addresses

def initialize()
def initialize
@robot_regex = /\b(Baidu|Gigabot|Googlebot|libwww-perl|lwp-trivial|msnbot|SiteUptime|Slurp|WordPress|ZIBB|ZyBorg)\b/i
@ignore_ip_addresses = []
end
Expand Down
8 changes: 4 additions & 4 deletions lib/split/experiment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(name, *alternative_names)

def winner
if w = Split.redis.hget(:experiment_winner, name)
return Split::Alternative.find(w, name)
Split::Alternative.find(w, name)
else
nil
end
Expand Down Expand Up @@ -45,7 +45,7 @@ def next_alternative
def random_alternative
weights = alternatives.map(&:weight)

total = weights.inject(0.0) {|t,w| t+w}
total = weights.inject(:+)
point = rand * total

alternatives.zip(weights).each do |n,w|
Expand Down Expand Up @@ -148,12 +148,12 @@ def self.find_or_create(key, *alternatives)
experiment.save
end
return experiment

end

def self.initialize_alternatives(alternatives, name)

if alternatives.reject {|a| Split::Alternative.valid? a}.any?
unless alternatives.all? { |a| Split::Alternative.valid?(a) }
raise InvalidArgument, 'Alternatives must be strings'
end

Expand Down
2 changes: 1 addition & 1 deletion lib/split/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def finished(experiment_name, options = {:reset => true})
end

def override(experiment_name, alternatives)
return params[experiment_name] if defined?(params) && alternatives.include?(params[experiment_name])
params[experiment_name] if defined?(params) && alternatives.include?(params[experiment_name])
end

def begin_experiment(experiment, alternative_name)
Expand Down

0 comments on commit b03ebcd

Please sign in to comment.