aboutsummaryrefslogtreecommitdiffstats
path: root/spec/support
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support')
-rw-r--r--spec/support/faster/model_stubber/object_cache.rb18
-rw-r--r--spec/support/faster/model_stubber/wrapper.rb13
2 files changed, 28 insertions, 3 deletions
diff --git a/spec/support/faster/model_stubber/object_cache.rb b/spec/support/faster/model_stubber/object_cache.rb
index 8f24dcf71..9fd71d864 100644
--- a/spec/support/faster/model_stubber/object_cache.rb
+++ b/spec/support/faster/model_stubber/object_cache.rb
@@ -2,8 +2,14 @@ module ModelStubber
module ObjectCache extend self
def add_to_cache model
- model.id = cache[model.class].keys.last.try(:succ) || 1
- cache[model.class].update( model.id => model )
+ if class_cache = cache[model.class]
+ model.id = class_cache.keys.last.try(:succ) || 10_001
+ class_cache.update( model.id => model )
+ else
+ cache[model.class] = {}
+ stub_class model.class
+ add_to_cache model
+ end
end
def empty_cache!
@@ -16,8 +22,14 @@ module ModelStubber
@__cache__ ||= _cache
end
def _cache
- Hash.new { |h, k| h[k] = {} }
+ Hash.new
end
+ def stub_class klass
+ kache = cache
+ class << klass; self end.module_eval do
+ define_method(:find){ |id| kache[klass].fetch(id) }
+ end
+ end
end
end
diff --git a/spec/support/faster/model_stubber/wrapper.rb b/spec/support/faster/model_stubber/wrapper.rb
new file mode 100644
index 000000000..25ca374dc
--- /dev/null
+++ b/spec/support/faster/model_stubber/wrapper.rb
@@ -0,0 +1,13 @@
+module ModelStubber
+ # I am wrapping ActiveRecord subclasses in order not to mess with
+ # ActiveRecord itself
+ class Wrapper
+
+ attr_reader :klass
+
+ def initialize klass
+ @klass = klass
+ end
+
+ end
+end