lib/verdict/tasks.rake in verdict-0.2.1 vs lib/verdict/tasks.rake in verdict-0.3.0

- old
+ new

@@ -1,59 +1,88 @@ -def require_env(key) - value = ENV[key.downcase].presence || ENV[key.upcase].presence - raise "Provide #{key} as environment variable" if value.blank? - value +module Verdict + module Rake + def self.require_env(key) + if ENV.has_key?(key.upcase) && !ENV[key.upcase].empty? + ENV[key.upcase] + elsif ENV.has_key?(key.downcase) && !ENV[key.downcase].empty? + ENV[key.downcase] + else + raise ArgumentError, "Provide #{key.upcase} as environment variable" + end + end + + def self.stdout + $stdout + end + + def self.experiment + experiment_handle = Verdict::Rake.require_env('experiment') + Verdict[experiment_handle] or raise "Experiment `#{handle}` not found" + end + + def self.group + group_handle = Verdict::Rake.require_env('group') + experiment.group(group_handle) or raise "Group `#{group_handle}` not found." + end + + def self.subject_identifier + Verdict::Rake.require_env('subject') + end + end end -namespace :experiments do +namespace :verdict do desc "List all defined experiments" - task :list => 'environment' do + task :experiments => 'environment' do length = Verdict.repository.keys.map(&:length).max Verdict.repository.each do |_, experiment| - print "#{experiment.handle.ljust(length)} | " - print "Groups: #{experiment.groups.values.map(&:to_s).join(', ')}" - puts + Verdict::Rake.stdout.print "#{experiment.handle.ljust(length)} | " + Verdict::Rake.stdout.print "Groups: #{experiment.groups.values.map(&:to_s).join(', ')}" + Verdict::Rake.stdout.puts end end desc "Looks up the assignment for a given experiment and subject" task :lookup_assignment => 'environment' do - experiment = Verdict[require_env('experiment')] or raise "Experiment not found" - subject_identifier = require_env('subject') + experiment = Verdict::Rake.experiment + subject_identifier = Verdict::Rake.subject_identifier + assignment = experiment.lookup_assignment_for_identifier(subject_identifier) if assignment.nil? - puts "Subject #{ENV['subject']} is not assigned to experiment #{experiment.handle} yet." + Verdict::Rake.stdout.puts "Subject `#{subject_identifier}` is not assigned to experiment `#{experiment.handle}` yet." elsif assignment.qualified? - puts "Subject #{ENV['subject']} is assigned to group `#{assignment.group.handle}`" + Verdict::Rake.stdout.puts "Subject `#{subject_identifier}` is assigned to group `#{assignment.group.handle}` of experiment `#{experiment.handle}`." else - puts "Subject #{ENV['subject']} is unqualified for experiment #{experiment.handle}" + Verdict::Rake.stdout.puts "Subject `#{subject_identifier}` is unqualified for experiment `#{experiment.handle}`." end end desc "Manually assign a subject to a given group in an experiment" task :assign_manually => 'environment' do - experiment = Verdict[require_env('experiment')] or raise "Experiment not found" - group = experiment.group(require_env('group')) or raise "Group not found" - assignment = experiment.subject_assignment(require_env('subject'), group, false) - experiment.store_assignment(assignment) + experiment = Verdict::Rake.experiment + group = Verdict::Rake.group + subject_identifier = Verdict::Rake.subject_identifier + + experiment.assign_manually_by_identifier(Verdict::Rake.require_env('subject'), group) + Verdict::Rake.stdout.puts "Subject `#{subject_identifier}` has been assigned to group `#{group.handle}` of experiment `#{experiment.handle}`." end - desc "Disqualify a subject from an experiment" - task :disqualify => 'environment' do - experiment = Verdict[require_env('experiment')] or raise "Experiment not found" - assignment = experiment.subject_assignment(require_env('subject'), nil, false) - experiment.store_assignment(assignment) + desc "Manually disqualify a subject from an experiment" + task :disqualify_manually => 'environment' do + experiment = Verdict::Rake.experiment + subject_identifier = Verdict::Rake.subject_identifier + + experiment.disqualify_manually_by_identifier(subject_identifier) + Verdict::Rake.stdout.puts "Subject `#{subject_identifier}` has been disqualified from experiment `#{experiment.handle}`." end desc "Removes the assignment for a subject so it will be reassigned to the experiment." task :remove_assignment => 'environment' do - experiment = Verdict[require_env('experiment')] or raise "Experiment not found" - experiment.remove_subject_identifier(require_env('subject')) - end + experiment = Verdict::Rake.experiment + subject_identifier = Verdict::Rake.subject_identifier - desc "Runs the cleanup tasks for an experiment" - task :wrapup => 'environment' do - experiment = Verdict[require_env('experiment')] or raise "Experiment not found" - experiment.wrapup + experiment.remove_subject_assignment_by_identifier(subject_identifier) + Verdict::Rake.stdout.puts "Removed assignment of subject with identifier `#{subject_identifier}`." + Verdict::Rake.stdout.puts "The subject will be reasigned when it encounters the experiment `#{experiment.handle}` again." end end