cache_rocket 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -11
- data/lib/cache_rocket.rb +6 -10
- data/lib/cache_rocket/fragment.rb +10 -2
- data/lib/cache_rocket/version.rb +1 -1
- data/test/cache_rocket_test.rb +13 -1
- data/test/fragment_test.rb +7 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 02591106ca977ec2f38b633c6003fbbe730d7714
|
4
|
+
data.tar.gz: 61d116127812127f1eceb606b2af0fca5f83acfb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd1fca1866a158eae06686d10f73ab57de4eee8fb5d2ca593c215ef80b37258cfdb608998bd1476164a12bf433d634313015fb5486e329409b56f494c51be7dc
|
7
|
+
data.tar.gz: 1a227ce2c6238563950171be071e3d1027e4dc148ad2e4c50b806c8c2a781a3ab8d23e2ab06b6ee0bc971a8c134757a9913d2c0e6d295b6b088e9a1411a541fe
|
data/README.md
CHANGED
@@ -1,11 +1,9 @@
|
|
1
1
|
[![Gem Version](https://badge.fury.io/rb/cache_rocket.png)][gem]
|
2
2
|
[![Build Status](https://api.travis-ci.org/teeparham/cache_rocket.png)][build]
|
3
|
-
[![Coverage Status](https://coveralls.io/repos/teeparham/cache_rocket.badge.png)][coverage]
|
4
3
|
[![Code Climate](https://codeclimate.com/github/teeparham/cache_rocket.png)][climate]
|
5
4
|
|
6
5
|
[gem]: http://badge.fury.io/rb/cache_rocket
|
7
6
|
[build]: https://travis-ci.org/teeparham/cache_rocket
|
8
|
-
[coverage]: https://coveralls.io/r/teeparham/cache_rocket
|
9
7
|
[climate]: https://codeclimate.com/github/teeparham/cache_rocket
|
10
8
|
|
11
9
|
# Rails rendering extension for server-side html caching
|
@@ -31,10 +29,10 @@ include CacheRocket
|
|
31
29
|
|
32
30
|
## Use
|
33
31
|
|
34
|
-
This gem allows you to
|
32
|
+
This gem allows you to cache a partial of static html and replace inner dynamic html. Here is an example
|
35
33
|
scenario:
|
36
34
|
|
37
|
-
You have some html that would
|
35
|
+
You have some html that you would like to cache, but cannot because of some uncacheable code nested in the DOM. For example:
|
38
36
|
|
39
37
|
##### file.html.haml:
|
40
38
|
```haml
|
@@ -79,12 +77,12 @@ In the above example, you could also remove the `_dynamic.html.haml` file like s
|
|
79
77
|
|
80
78
|
##### file.html.haml:
|
81
79
|
```haml
|
82
|
-
= render_cached 'container', replace: {dynamic: complicated_uncacheable_stuff}
|
80
|
+
= render_cached 'container', replace: { dynamic: complicated_uncacheable_stuff }
|
83
81
|
```
|
84
82
|
|
85
83
|
## Options
|
86
84
|
|
87
|
-
`render_cached` provides
|
85
|
+
`render_cached` provides several calling styles:
|
88
86
|
|
89
87
|
#### Single partial to replace
|
90
88
|
|
@@ -94,18 +92,31 @@ render_cached "container", replace: "inner"
|
|
94
92
|
|
95
93
|
#### Array of partials to replace
|
96
94
|
```ruby
|
97
|
-
render_cached "container", replace: ["inner"]
|
95
|
+
render_cached "container", replace: ["inner", "footer"]
|
98
96
|
```
|
99
97
|
|
100
|
-
####
|
98
|
+
#### Hash of keys to replace with values
|
101
99
|
```ruby
|
102
|
-
render_cached "container", replace: {key_name: a_helper_method(object)}
|
100
|
+
render_cached "container", replace: { key_name: a_helper_method(object) }
|
103
101
|
```
|
104
102
|
|
105
|
-
####
|
103
|
+
#### Block containing a hash of keys to replace with values
|
106
104
|
```ruby
|
107
105
|
render_cached "container" do
|
108
|
-
{key_name: a_helper_method(object)}
|
106
|
+
{ key_name: a_helper_method(object) }
|
107
|
+
end
|
108
|
+
```
|
109
|
+
|
110
|
+
#### Render a collection with hash of keys, using a Proc for each collection item
|
111
|
+
```ruby
|
112
|
+
render_cached "container", collection: objects,
|
113
|
+
replace: { key_name: ->(object){ a_helper_method(object) } }
|
114
|
+
```
|
115
|
+
|
116
|
+
#### Render a collection with block syntax
|
117
|
+
```ruby
|
118
|
+
render_cached "container", collection: objects do
|
119
|
+
{ key_name: ->(object){ a_helper_method(object) } }
|
109
120
|
end
|
110
121
|
```
|
111
122
|
|
data/lib/cache_rocket.rb
CHANGED
@@ -34,24 +34,20 @@ module CacheRocket
|
|
34
34
|
# render_cached "partial", collection: objects, replace: { key_name: ->(object){a_method(object)} }
|
35
35
|
#
|
36
36
|
def render_cached(partial, options={})
|
37
|
-
|
37
|
+
replace_hash = options.delete(:replace)
|
38
38
|
collection = options.delete(:collection)
|
39
39
|
|
40
40
|
fragment = Fragment.new(render(partial, options))
|
41
41
|
|
42
|
-
case
|
42
|
+
case replace_hash
|
43
43
|
when Hash
|
44
|
-
|
45
|
-
fragment.replace_collection collection, replace
|
46
|
-
else
|
47
|
-
fragment.replace_from_hash replace
|
48
|
-
end
|
44
|
+
fragment.replace replace_hash, collection
|
49
45
|
when NilClass
|
50
46
|
raise ArgumentError.new(ERROR_MISSING_KEY_OR_BLOCK) unless block_given?
|
51
|
-
fragment.
|
47
|
+
fragment.replace yield, collection
|
52
48
|
else
|
53
|
-
|
54
|
-
|
49
|
+
key_array = *replace_hash
|
50
|
+
key_array.each do |key|
|
55
51
|
fragment.gsub! cache_replace_key(key), render(key, options)
|
56
52
|
end
|
57
53
|
end
|
@@ -16,6 +16,16 @@ module CacheRocket
|
|
16
16
|
self.value.gsub! key, value
|
17
17
|
end
|
18
18
|
|
19
|
+
def replace(hash, collection)
|
20
|
+
if collection
|
21
|
+
replace_collection collection, hash
|
22
|
+
else
|
23
|
+
replace_from_hash hash
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
19
29
|
def replace_from_hash(hash)
|
20
30
|
hash.each do |key, value|
|
21
31
|
gsub! cache_replace_key(key), value.to_s
|
@@ -32,8 +42,6 @@ module CacheRocket
|
|
32
42
|
self.value = html
|
33
43
|
end
|
34
44
|
|
35
|
-
private
|
36
|
-
|
37
45
|
def replace_item_hash(item, hash)
|
38
46
|
item_fragment = self.value.dup
|
39
47
|
|
data/lib/cache_rocket/version.rb
CHANGED
data/test/cache_rocket_test.rb
CHANGED
@@ -79,7 +79,19 @@ class CacheRocketTest < Test::Unit::TestCase
|
|
79
79
|
assert_equal "Hi Snoop.Hi Boo.",
|
80
80
|
@renderer.render_cached("partial", collection: dogs, replace: {dog: ->(dog){dog_name(dog)} })
|
81
81
|
end
|
82
|
-
|
82
|
+
|
83
|
+
should "replace collection using hash block with Proc" do
|
84
|
+
def dog_name(dog) dog end
|
85
|
+
@renderer.stubs(:render).with("partial", {}).returns "Hi #{@renderer.cache_replace_key(:dog)}."
|
86
|
+
dogs = %w[Snoop Boo]
|
87
|
+
|
88
|
+
rendered = @renderer.render_cached("partial", collection: dogs) do
|
89
|
+
{ dog: ->(dog){dog_name(dog)} }
|
90
|
+
end
|
91
|
+
|
92
|
+
assert_equal "Hi Snoop.Hi Boo.", rendered
|
93
|
+
end
|
94
|
+
|
83
95
|
should "raise ArgumentError with invalid syntax" do
|
84
96
|
@renderer.stubs(:render).with("container").returns("")
|
85
97
|
assert_raise(ArgumentError) do
|
data/test/fragment_test.rb
CHANGED
@@ -9,32 +9,30 @@ module CacheRocket
|
|
9
9
|
end
|
10
10
|
|
11
11
|
context "#gsub!" do
|
12
|
-
should "
|
12
|
+
should "substitute value" do
|
13
13
|
fragment = Fragment.new("hello there, hello.")
|
14
14
|
assert_equal "yo there, yo.", fragment.gsub!("hello", "yo")
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
context "#
|
19
|
-
should "replace cache keys" do
|
18
|
+
context "#replace" do
|
19
|
+
should "replace cache keys from hash" do
|
20
20
|
cr_key = Fragment.new(nil).cache_replace_key(:xx)
|
21
21
|
fragment = Fragment.new("hey #{cr_key} hey.")
|
22
|
-
fragment.
|
22
|
+
fragment.replace({xx: "yo"}, nil)
|
23
23
|
assert_equal "hey yo hey.", fragment.value
|
24
24
|
end
|
25
|
-
end
|
26
25
|
|
27
|
-
|
28
|
-
should "replace with Proc" do
|
26
|
+
should "replace collection with Proc" do
|
29
27
|
def last5(object)
|
30
28
|
object.last(5)
|
31
29
|
end
|
32
|
-
replace_hash = { something: ->(
|
30
|
+
replace_hash = { something: ->(object){ last5(object)} }
|
33
31
|
collection = %w[xxTiger xxSkunk]
|
34
32
|
cr_key = Fragment.new(nil).cache_replace_key(:something)
|
35
33
|
fragment = Fragment.new("hey #{cr_key} hey.")
|
36
34
|
assert_equal "hey Tiger hey.hey Skunk hey.",
|
37
|
-
fragment.
|
35
|
+
fragment.replace(replace_hash, collection)
|
38
36
|
end
|
39
37
|
end
|
40
38
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cache_rocket
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tee Parham
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-08-
|
11
|
+
date: 2013-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|