Class: IWonder::AbTesting::Loader

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

Constant Summary collapse

I_WONDER_DATA_DIR =
Rails.root.join("i_wonder").to_s
AB_TEST_DATA_DIR =
File.join(I_WONDER_DATA_DIR,"ab_tests").to_s

Class Method Summary collapse

Class Method Details

.load_allObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/i_wonder/ab_testing/loader.rb', line 16

def self.load_all
  if File.exists?(I_WONDER_DATA_DIR) and File.exists?(AB_TEST_DATA_DIR)
    Dir.open(AB_TEST_DATA_DIR) do |dir|
      dir.each{|file_name|
        if file_name =~ /(.+).xml/
          load_sym($1)
        end
      }
    end
  end
end

.load_sym(ab_test_sym) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/i_wonder/ab_testing/loader.rb', line 28

def self.load_sym(ab_test_sym)
  if File.exists?(file_name(ab_test_sym))
    File.open(file_name(ab_test_sym), 'r') do |file|
      
      ab_test = AbTest.find_by_sym(ab_test_sym)
      ab_test ||= AbTest.new
      
      ab_test.from_xml(file.read)
      ab_test.skip_file_save = true
      ab_test.save!
    end          
  end
end

.remove_file_for(ab_test) ⇒ Object



42
43
44
45
46
# File 'lib/i_wonder/ab_testing/loader.rb', line 42

def self.remove_file_for(ab_test)
  if File.exists?(file_name(ab_test.sym))
    File.delete(file_name(ab_test.sym))
  end
end

.save_ab_test(ab_test) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/i_wonder/ab_testing/loader.rb', line 7

def self.save_ab_test(ab_test)
  Dir.mkdir(I_WONDER_DATA_DIR) unless File.exists?(I_WONDER_DATA_DIR) 
  Dir.mkdir(AB_TEST_DATA_DIR) unless File.exists?(AB_TEST_DATA_DIR)
  
  File.open(file_name(ab_test.sym), 'w') do |file|
    file.write ab_test.to_xml(:except => [:id, :created_at, :updated_at], :include => :ab_test_goals)
  end
end