mongoid_cached_document 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -25,26 +25,26 @@ Assuming <tt>@user</tt> is a valid <tt>User</tt> with an <tt>id</tt> of 42 and <
25
25
 
26
26
  This will create a new <tt>Post</tt> document, with an <tt>author</tt> field, the content of which is a hash:
27
27
 
28
- { '_type' => 'User', '_id' = 1 }
28
+ { '_type' => 'User', '_id' = 42 }
29
29
 
30
30
  @post.author._type
31
31
  => 'User'
32
32
  @post.author._id
33
- => 1
33
+ => 42
34
34
 
35
35
  Attempting to get the user's <tt>login</tt> will cause the real user document to be fetched from the collection, which replaces the cached values.
36
36
 
37
37
  class User
38
38
  #...
39
39
 
40
- def cached_attributes
40
+ def cachable_attributes
41
41
  [ :login ]
42
42
  end
43
43
  end
44
44
 
45
45
  With the above class definition, the cached attributes will be:
46
46
 
47
- { '_type' => 'User', '_id' = 1, 'login' => 'jsmith' }
47
+ { '_type' => 'User', '_id' = 42, 'login' => 'jsmith' }
48
48
 
49
49
  To use the above in a criteria:
50
50
 
data/Rakefile CHANGED
@@ -28,6 +28,7 @@ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
28
  spec.libs << 'lib' << 'spec'
29
29
  spec.pattern = 'spec/**/*_spec.rb'
30
30
  spec.rcov = true
31
+ spec.rcov_opts = [ '--exclude', 'gem', '--exclude', 'spec' ]
31
32
  end
32
33
 
33
34
  task :spec => :check_dependencies
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -35,7 +35,9 @@ module Mongoid
35
35
 
36
36
  class << self
37
37
  def set(value)
38
- if value.respond_to? :cachable_attributes
38
+ if value.nil?
39
+ nil
40
+ elsif value.respond_to? :cachable_attributes
39
41
  value.cachable_attributes.merge({ '_type' => value.class.to_s, '_id' => value.id })
40
42
  else
41
43
  { '_type' => value.class.to_s, '_id' => value.id }
@@ -57,20 +59,23 @@ module Mongoid
57
59
  def method_missing(name, *args, &block)
58
60
  if @document
59
61
  _document.send name, *args, &block
60
- elsif @cached_attributes.has_key? name.to_s
62
+ elsif @cached_attributes && @cached_attributes.has_key? name.to_s
61
63
  @cached_attributes[name.to_s]
62
- else
64
+ elsif _document
63
65
  if defined? Rails
64
66
  Rails.logger.debug("#{@cached_attributes['_type']}[:#{name}] is not cached (called from #{caller(1).first})")
65
67
  end
66
68
 
67
69
  _document.send name, *args, &block
70
+ else
71
+ super
68
72
  end
69
73
  end
70
74
 
71
75
  private
72
76
  def _document
73
- @document ||= @cached_attributes['_type'].constantize.find(@cached_attributes['_id'])
77
+ @document ||= @cached_attributes && @cached_attributes['_type'].constantize.find(@cached_attributes['_id'])
78
+ end
74
79
  end
75
80
  end
76
81
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid_cached_document}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Matthew Gibbons"]
12
- s.date = %q{2010-04-07}
12
+ s.date = %q{2010-04-08}
13
13
  s.description = %q{Adds support for caching Mongoid documents as a field within other Mongoid documents, with complete control over which fields to cache.}
14
14
  s.email = %q{mhgibbons@me.com}
15
15
  s.extra_rdoc_files = [
@@ -54,4 +54,30 @@ describe Mongoid::CachedDocument do
54
54
  @cached_document.title == 'Title'
55
55
  end
56
56
  end
57
+
58
+ describe "tesing for equality" do
59
+ before :each do
60
+ @cachable_document = mock(CachableDocument)
61
+
62
+ @cachable_document.stub(:id).and_return 42
63
+ @cachable_document.stub(:class).and_return CachableDocument
64
+
65
+ @cached_document = Mongoid::CachedDocument.get('_type' => 'CachableDocument', '_id' => 42)
66
+ end
67
+
68
+ it "#== delegates to the document" do
69
+ CachableDocument.should_receive(:find).with(42).and_return(@cachable_document)
70
+ @cached_document == @cachable_document
71
+ end
72
+
73
+ it "#eql? delegates to the document" do
74
+ CachableDocument.should_receive(:find).with(42).and_return(@cachable_document)
75
+ @cached_document.eql? @cachable_document
76
+ end
77
+
78
+ it "#equal? delegates to the document" do
79
+ CachableDocument.should_receive(:find).with(42).and_return(@cachable_document)
80
+ @cached_document.equal? @cachable_document
81
+ end
82
+ end
57
83
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Matthew Gibbons
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-07 00:00:00 +01:00
17
+ date: 2010-04-08 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency