Sha256: 61b580bb26fd59188ba54545947cabbccd7e08d770ffa2327ba1bdd7ea19bd17

Contents?: true

Size: 1.09 KB

Versions: 7

Compression:

Stored size: 1.09 KB

Contents

module Split
  class Trial
    attr_accessor :experiment
    attr_accessor :goals

    def initialize(attrs = {})
      self.experiment = attrs[:experiment]  if !attrs[:experiment].nil?
      self.alternative = attrs[:alternative] if !attrs[:alternative].nil?
      self.goals = attrs[:goals].nil? ? [] : attrs[:goals]
    end

    def alternative
      @alternative ||=  if experiment.winner
                          experiment.winner
                        end
    end

    def complete!
      if alternative
        if self.goals.empty?
          alternative.increment_completion
        else
          self.goals.each {|g| alternative.increment_completion(g)}
        end
      end
    end

    def choose!
      choose
      record!
    end

    def record!
      alternative.increment_participation
    end

    def choose
      self.alternative = experiment.next_alternative
    end

    def alternative=(alternative)
      @alternative = if alternative.kind_of?(Split::Alternative)
        alternative
      else
        self.experiment.alternatives.find{|a| a.name == alternative }
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
split-0.7.0 lib/split/trial.rb
split-0.6.6 lib/split/trial.rb
split-0.6.5 lib/split/trial.rb
split-0.6.4 lib/split/trial.rb
split-0.6.3 lib/split/trial.rb
split-0.6.2 lib/split/trial.rb
split-0.6.1 lib/split/trial.rb