keynote 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of keynote might be problematic. Click here for more details.
- data/lib/keynote.rb +1 -12
- data/lib/keynote/cache.rb +15 -25
- data/lib/keynote/version.rb +1 -1
- data/spec/keynote_spec.rb +22 -24
- metadata +2 -2
data/lib/keynote.rb
CHANGED
@@ -14,11 +14,6 @@ require "keynote/cache"
|
|
14
14
|
# @see file:README.md
|
15
15
|
module Keynote
|
16
16
|
class << self
|
17
|
-
# @return [Boolean] When `use_caching` is enabled, repeatedly invoking
|
18
|
-
# the same presenter with the same parameters will always return the
|
19
|
-
# same object. This is disabled by default for now.
|
20
|
-
attr_accessor :use_caching
|
21
|
-
|
22
17
|
# Create or retrieve a presenter wrapping zero or more objects.
|
23
18
|
#
|
24
19
|
# The first parameter is a Rails view context, but you'll usually access
|
@@ -56,11 +51,7 @@ module Keynote
|
|
56
51
|
name = presenter_name_from_object(objects[0])
|
57
52
|
end
|
58
53
|
|
59
|
-
|
60
|
-
Cache.fetch(name, view, *objects) do
|
61
|
-
presenter_from_name(name).new(view, *objects)
|
62
|
-
end
|
63
|
-
else
|
54
|
+
Cache.fetch(name, view, *objects) do
|
64
55
|
presenter_from_name(name).new(view, *objects)
|
65
56
|
end
|
66
57
|
end
|
@@ -75,6 +66,4 @@ module Keynote
|
|
75
66
|
"#{name.to_s.camelize}Presenter".constantize
|
76
67
|
end
|
77
68
|
end
|
78
|
-
|
79
|
-
Keynote.use_caching = false
|
80
69
|
end
|
data/lib/keynote/cache.rb
CHANGED
@@ -10,44 +10,34 @@ module Keynote
|
|
10
10
|
# Return a cached presenter for the given parameters, or yield and cache
|
11
11
|
# the block's return value for next time.
|
12
12
|
#
|
13
|
-
# The cached presenters are stored in an instance variable on the
|
14
|
-
#
|
15
|
-
#
|
13
|
+
# The cached presenters are stored in an instance variable on the current
|
14
|
+
# view context, so they'll be garbage-collected when the view context goes
|
15
|
+
# out of scope.
|
16
16
|
#
|
17
17
|
# @param [Symbol] name
|
18
18
|
# @param [ActionView::Base] view
|
19
19
|
# @param [Array] objects
|
20
20
|
# @return [Keynote::Presenter]
|
21
21
|
def fetch(name, view, *objects)
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
return(yield)
|
22
|
+
# If we don't have a view, just bail out and return a fresh presenter
|
23
|
+
# every time.
|
24
|
+
if view.nil?
|
25
|
+
return yield
|
27
26
|
end
|
28
27
|
|
29
|
-
#
|
30
|
-
|
31
|
-
if (cache = first.instance_variable_get(:@_keynote_cache)).nil?
|
28
|
+
# Initialize our cache on the view context if it doesn't already exist.
|
29
|
+
if (cache = view.instance_variable_get(:@_keynote_cache)).nil?
|
32
30
|
cache = {}
|
33
|
-
|
31
|
+
view.instance_variable_set(:@_keynote_cache, cache)
|
34
32
|
end
|
35
33
|
|
36
|
-
#
|
37
|
-
#
|
34
|
+
# Key each entry by the name of the presenter and the object_id of each
|
35
|
+
# of the objects involved.
|
38
36
|
key = [name, *objects.map(&:object_id)]
|
39
37
|
|
40
|
-
# If we
|
41
|
-
#
|
42
|
-
|
43
|
-
if (presenter = cache[key])
|
44
|
-
presenter.view = view
|
45
|
-
else
|
46
|
-
presenter = yield
|
47
|
-
cache[key] = presenter
|
48
|
-
end
|
49
|
-
|
50
|
-
presenter
|
38
|
+
# If we have a cached presenter, return it; if not, yield, store the
|
39
|
+
# result, and return that.
|
40
|
+
cache[key] or (cache[key] = yield)
|
51
41
|
end
|
52
42
|
end
|
53
43
|
end
|
data/lib/keynote/version.rb
CHANGED
data/spec/keynote_spec.rb
CHANGED
@@ -59,56 +59,54 @@ describe Keynote do
|
|
59
59
|
end
|
60
60
|
|
61
61
|
describe "caching" do
|
62
|
-
describe "
|
63
|
-
|
64
|
-
|
62
|
+
describe "when there is a view context" do
|
63
|
+
let(:view) { Object.new }
|
64
|
+
let(:view_2) { Object.new }
|
65
65
|
|
66
|
-
it "should cache on the
|
66
|
+
it "should cache based on the models" do
|
67
67
|
model_1 = Normal.new
|
68
68
|
model_2 = Normal.new
|
69
69
|
|
70
|
-
presented_1 = Keynote.present(
|
71
|
-
presented_2 = Keynote.present(
|
70
|
+
presented_1 = Keynote.present(view, model_1)
|
71
|
+
presented_2 = Keynote.present(view, model_1)
|
72
72
|
|
73
73
|
presented_1.must_be :equal?, presented_2
|
74
74
|
|
75
|
-
presented_3 = Keynote.present(
|
76
|
-
presented_4 = Keynote.present(
|
77
|
-
presented_5 = Keynote.present(
|
75
|
+
presented_3 = Keynote.present(view, :combined, model_1, model_2)
|
76
|
+
presented_4 = Keynote.present(view, :combined, model_1, model_2)
|
77
|
+
presented_5 = Keynote.present(view, :combined, model_2, model_1)
|
78
78
|
|
79
79
|
presented_3.wont_be :equal?, presented_1
|
80
80
|
presented_3.must_be :equal?, presented_4
|
81
81
|
presented_3.wont_be :equal?, presented_5
|
82
82
|
end
|
83
83
|
|
84
|
-
it "
|
85
|
-
presenter_1 = Keynote.present(
|
86
|
-
presenter_2 = Keynote.present(
|
84
|
+
it "should cache even if there are no models" do
|
85
|
+
presenter_1 = Keynote.present(view, :empty)
|
86
|
+
presenter_2 = Keynote.present(view, :empty)
|
87
87
|
|
88
|
-
presenter_1.
|
88
|
+
presenter_1.must_be :equal?, presenter_2
|
89
89
|
end
|
90
90
|
|
91
|
-
it "should
|
91
|
+
it "should be scoped to the specific view context" do
|
92
92
|
model = Normal.new
|
93
93
|
|
94
|
-
presenter_1 = Keynote.present(
|
95
|
-
presenter_1.view.must_equal
|
94
|
+
presenter_1 = Keynote.present(view, model)
|
95
|
+
presenter_1.view.must_equal view
|
96
96
|
|
97
|
-
presenter_2 = Keynote.present(
|
98
|
-
presenter_2.
|
99
|
-
presenter_2.view.must_equal
|
97
|
+
presenter_2 = Keynote.present(view_2, model)
|
98
|
+
presenter_2.wont_be :equal?, presenter_1
|
99
|
+
presenter_2.view.must_equal view_2
|
100
100
|
end
|
101
101
|
end
|
102
102
|
|
103
|
-
describe "
|
104
|
-
before { Keynote.use_caching = false }
|
105
|
-
|
103
|
+
describe "when there's no view context" do
|
106
104
|
it "shouldn't cache" do
|
107
105
|
model_1 = Normal.new
|
108
106
|
model_2 = Normal.new
|
109
107
|
|
110
|
-
presented_1 = Keynote.present(
|
111
|
-
presented_2 = Keynote.present(
|
108
|
+
presented_1 = Keynote.present(nil, model_1)
|
109
|
+
presented_2 = Keynote.present(nil, model_1)
|
112
110
|
|
113
111
|
presented_1.wont_be :equal?, presented_2
|
114
112
|
end
|
metadata
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
name: keynote
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Ryan Fitzgerald
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-12-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
version_requirements: !ruby/object:Gem::Requirement
|