Class: Stratagem::AutoMock::Aquifer

Inherits:
Object
  • Object
show all
Includes:
Singleton, Factory
Defined in:
lib/stratagem/auto_mock/aquifer.rb

Constant Summary

Constants included from ValueGenerator

ValueGenerator::ADDRESSES, ValueGenerator::GENERIC_NAMES, ValueGenerator::GENERIC_URLS, ValueGenerator::NUMERIC_TYPES, ValueGenerator::USERS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Factory

#add_mocked, #mock, #mock_attributes

Methods included from ValueGenerator

#generate_boolean, #generate_datetime, #generate_float, #generate_int, #generate_string, #generate_text, #generate_value

Instance Attribute Details

#applicationObject

Returns the value of attribute application.



8
9
10
# File 'lib/stratagem/auto_mock/aquifer.rb', line 8

def application
  @application
end

Class Method Details

.init(application) ⇒ Object



10
11
12
13
14
# File 'lib/stratagem/auto_mock/aquifer.rb', line 10

def self.init(application)
  at_exit { self.instance.destroy }
  self.instance.application = application
  self.instance
end

Instance Method Details

#clearObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/stratagem/auto_mock/aquifer.rb', line 16

def clear
  objects = self.repo.values.inject([]) {|memo,obj| memo += obj.compact }
  i = 0
  while (objects.size > 0 && ((i+=1) < objects.size))
    objects = objects.select do |instance|
      # puts "deleting #{instance.class.name}"
      begin
        instance.destroy
      rescue
        begin
          instance.delete
        rescue
          puts "Unable to delete object: #{instance.class.name} - #{$!.message}"
          puts $!.backtrace
        end
      end
      !instance.frozen?
    end
  end
  self.repo.clear
end

#destroyObject



38
39
40
# File 'lib/stratagem/auto_mock/aquifer.rb', line 38

def destroy
  clear
end

#fill(model_count = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/stratagem/auto_mock/aquifer.rb', line 55

def fill(model_count=nil)
  Stratagem.logger.phase "mocking_models"
  application.models.each do |meta_model|
    models = mock_model(meta_model.klass, model_count) if (meta_model.stratagem?)
  end
  # puts "aquifer full"
  print
  self
end

#instances_of(model_klass) ⇒ Object



42
43
44
45
46
# File 'lib/stratagem/auto_mock/aquifer.rb', line 42

def instances_of(model_klass)
  objects = (repo[model_klass.name] || []).clone
  # puts "found #{objects.size} instances in well"
  objects
end

#mock_model(klass, count = nil) ⇒ Object



74
75
76
77
78
79
# File 'lib/stratagem/auto_mock/aquifer.rb', line 74

def mock_model(klass, count=nil)
  count ||= 6
  count.times do |i|
    instance,valid = mock(klass)
  end
end


65
66
67
68
69
70
71
72
# File 'lib/stratagem/auto_mock/aquifer.rb', line 65

def print
  application.models.each do |meta_model|
    puts "#{meta_model.klass.name}"
    (repo[meta_model.klass.name] || []).each do |instance|
      puts "\t#{instance.class} - #{instance}"
    end
  end
end

#random_instance(model_klass) ⇒ Object



48
49
50
51
52
53
# File 'lib/stratagem/auto_mock/aquifer.rb', line 48

def random_instance(model_klass)
  objects = repo[model_klass.name]
  # puts "found #{objects.size} instances in well"
  instance = objects[rand objects.size]
  instance
end