Class: Experimental::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/experimental/loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Loader

Returns a new instance of Loader.



5
6
7
# File 'lib/experimental/loader.rb', line 5

def initialize(options = {})
  @logger = options[:logger] || Logger.new('/dev/null')
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/experimental/loader.rb', line 9

def logger
  @logger
end

Instance Method Details

#syncObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/experimental/loader.rb', line 11

def sync
  logger.info "Synchronizing experiments..."

  Experimental::Experiment.transaction do
    active = Experimental.experiment_data.map do |name, attributes|
      experiment = Experimental::Experiment.find_or_initialize_by_name(name)
      logger.info "  * #{experiment.id ? 'updating' : 'creating'} #{name}"
      defaults = {'num_buckets' => nil, 'notes' => nil, 'population' => nil}
      experiment.assign_attributes(defaults.merge(attributes))
      experiment.start_date ||= Time.now
      experiment.tap(&:save!)
    end

    scope = Experimental::Experiment.in_code
    scope = scope.where('id NOT IN (?)', active.map(&:id)) unless active.empty?
    scope.find_each do |experiment|
      next if experiment.admin?
      logger.info "  * removing #{experiment.name}"
      experiment.remove
    end
  end

  logger.info "Done."
end