cache_rocket 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +43 -31
- data/lib/cache_rocket/fragment.rb +3 -4
- data/lib/cache_rocket/key.rb +3 -4
- data/lib/cache_rocket/version.rb +1 -1
- data/lib/cache_rocket.rb +9 -11
- metadata +7 -45
- data/.gitignore +0 -14
- data/.ruby-version +0 -1
- data/.travis.yml +0 -5
- data/Gemfile +0 -7
- data/Rakefile +0 -12
- data/cache_rocket.gemspec +0 -28
- data/test/cache_rocket_test.rb +0 -118
- data/test/fragment_test.rb +0 -40
- data/test/key_test.rb +0 -18
- data/test/test_helper.rb +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 773ba9d38b86d5eef1b88d160ea2b51ed524b0f2
|
4
|
+
data.tar.gz: 1d20fdb597fe139dc8f5eb2bee78a369a476e251
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 26b198780bd4a790d8f3935c589c96ba241d3897e07423ec313821bdc24c70a69dd4a914af21f86f924c330ee725ddb1bd6b56176b4387e8a32664d8d1e87ec8
|
7
|
+
data.tar.gz: 17fd751c32a819d3a371d5356698e756fa4d2c88a751ae2f4b49913ddb811f29f6482bd29124a342cac2afa78cf77a554d1adfc66407dfec46880ddb995ecb78
|
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
[![Gem Version](https://badge.fury.io/rb/cache_rocket.
|
2
|
-
[![Build Status](https://
|
3
|
-
[![Coverage Status](
|
4
|
-
[![Code Climate](
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/cache_rocket.svg)][gem]
|
2
|
+
[![Build Status](https://travis-ci.org/neighborland/cache_rocket.svg?branch=master)][build]
|
3
|
+
[![Coverage Status](http://img.shields.io/coveralls/neighborland/cache_rocket.svg)][coverage]
|
4
|
+
[![Code Climate](http://img.shields.io/codeclimate/github/neighborland/cache_rocket.svg)][climate]
|
5
5
|
|
6
|
-
[gem]: http://
|
6
|
+
[gem]: http://rubygems.org/gems/cache_rocket
|
7
7
|
[build]: https://travis-ci.org/neighborland/cache_rocket
|
8
8
|
[coverage]: https://coveralls.io/r/neighborland/cache_rocket
|
9
9
|
[climate]: https://codeclimate.com/github/neighborland/cache_rocket
|
@@ -12,18 +12,18 @@
|
|
12
12
|
|
13
13
|
CacheRocket improves fragment caching efficiency in Rails.
|
14
14
|
CacheRocket allows caching more generic html fragments and allowing the contents of the cached fragments
|
15
|
-
to be replaced with dynamic content.
|
16
|
-
CacheRocket is a technique that may be used with other Rails caching strategies such as Russian Doll caching.
|
15
|
+
to be replaced with dynamic content.
|
17
16
|
|
18
17
|
### Install
|
19
18
|
|
20
|
-
Add
|
19
|
+
Add the gem to your Gemfile:
|
21
20
|
|
22
21
|
```ruby
|
23
22
|
gem 'cache_rocket'
|
24
23
|
```
|
25
24
|
|
26
|
-
|
25
|
+
Include the CacheRocket module so your views can use the `render_cached` method.
|
26
|
+
Most likely you would put this in your `ApplicationHelper`:
|
27
27
|
|
28
28
|
```ruby
|
29
29
|
include CacheRocket
|
@@ -31,17 +31,19 @@ include CacheRocket
|
|
31
31
|
|
32
32
|
### Use
|
33
33
|
|
34
|
-
|
34
|
+
CacheRocket allows you to cache a fragment of html and replace inner html.
|
35
|
+
You inject dynamic content into a static, cached outer partial.
|
36
|
+
You cache the donut and replace the donut hole.
|
35
37
|
|
36
38
|
Assume you have some html that you would like to cache, but cannot because of some uncacheable code nested in the DOM.
|
37
39
|
For example:
|
38
40
|
|
39
41
|
##### file.html.haml:
|
40
42
|
```haml
|
41
|
-
= render '
|
43
|
+
= render 'outer'
|
42
44
|
```
|
43
45
|
|
44
|
-
#####
|
46
|
+
##### _outer.html.haml:
|
45
47
|
```haml
|
46
48
|
.lots
|
47
49
|
.of
|
@@ -51,20 +53,20 @@ For example:
|
|
51
53
|
|
52
54
|
##### _inner.html.haml:
|
53
55
|
```haml
|
54
|
-
=
|
56
|
+
= uncacheable_content
|
55
57
|
```
|
56
58
|
|
57
59
|
In the scenario above, you can't cache anything. With `cache_rocket`, you can. Replace `render`
|
58
|
-
with `render_cached` in `file`, specify the partial to replace in `
|
60
|
+
with `render_cached` in `file`, specify the partial to replace in `outer`, and cache `outer`:
|
59
61
|
|
60
62
|
##### file.html.haml:
|
61
63
|
```haml
|
62
|
-
= render_cached '
|
64
|
+
= render_cached 'outer', replace: 'inner'
|
63
65
|
```
|
64
66
|
|
65
|
-
#####
|
67
|
+
##### _outer.html.haml:
|
66
68
|
```haml
|
67
|
-
- cache '
|
69
|
+
- cache 'outer' do
|
68
70
|
.lots
|
69
71
|
.of
|
70
72
|
.htmls
|
@@ -73,19 +75,19 @@ with `render_cached` in `file`, specify the partial to replace in `container`, a
|
|
73
75
|
|
74
76
|
##### _inner.html.haml:
|
75
77
|
``` haml
|
76
|
-
=
|
78
|
+
= uncacheable_content
|
77
79
|
```
|
78
80
|
|
79
81
|
In this example, you could remove the `_inner.html.haml` file altogether, like so:
|
80
82
|
|
81
83
|
##### file.html.haml:
|
82
84
|
```haml
|
83
|
-
= render_cached '
|
85
|
+
= render_cached 'outer', replace: { inner: uncacheable_content }
|
84
86
|
```
|
85
87
|
|
86
|
-
#####
|
88
|
+
##### _outer.html.haml:
|
87
89
|
```haml
|
88
|
-
- cache '
|
90
|
+
- cache 'outer' do
|
89
91
|
.lots
|
90
92
|
.of
|
91
93
|
.htmls
|
@@ -99,43 +101,53 @@ In this example, you could remove the `_inner.html.haml` file altogether, like s
|
|
99
101
|
#### Single partial to replace
|
100
102
|
|
101
103
|
```ruby
|
102
|
-
render_cached '
|
104
|
+
render_cached 'outer', replace: 'inner'
|
103
105
|
```
|
104
106
|
|
105
107
|
#### Array of partials to replace
|
106
108
|
```ruby
|
107
|
-
render_cached '
|
109
|
+
render_cached 'outer', replace: ['inner', 'footer']
|
108
110
|
```
|
109
111
|
|
110
112
|
#### Hash of keys to replace with values
|
111
113
|
```ruby
|
112
|
-
render_cached '
|
114
|
+
render_cached 'outer', replace: { key_name: a_helper_method(object) }
|
113
115
|
```
|
114
116
|
|
115
117
|
#### Block containing a hash of keys to replace with values
|
116
118
|
```ruby
|
117
|
-
render_cached '
|
119
|
+
render_cached 'outer' do
|
118
120
|
{ key_name: a_helper_method(object) }
|
119
121
|
end
|
120
122
|
```
|
121
123
|
|
122
124
|
#### Render a collection with hash of keys, using a Proc for each collection item
|
123
125
|
```ruby
|
124
|
-
render_cached '
|
125
|
-
replace: { key_name: ->(object){ a_helper_method(object) } }
|
126
|
+
render_cached 'outer', collection: objects,
|
127
|
+
replace: { key_name: -> (object) { a_helper_method(object) } }
|
126
128
|
```
|
127
129
|
|
128
130
|
#### Render a collection with block syntax
|
129
131
|
```ruby
|
130
|
-
render_cached '
|
131
|
-
{ key_name: ->(object){ a_helper_method(object) } }
|
132
|
+
render_cached 'outer', collection: objects do
|
133
|
+
{ key_name: -> (object) { a_helper_method(object) } }
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
137
|
+
#### Render a collection with block syntax with multiple keys
|
138
|
+
```ruby
|
139
|
+
render_cached 'outer', collection: objects do
|
140
|
+
{
|
141
|
+
key_1: -> (object) { a_helper_method(object) },
|
142
|
+
key_2: -> (item) { item.name },
|
143
|
+
}
|
132
144
|
end
|
133
145
|
```
|
134
146
|
|
135
147
|
### YMMV
|
136
148
|
|
137
149
|
`cache_rocket` is not magic. It should not be used in all situations.
|
138
|
-
Benchmark your page
|
150
|
+
Benchmark your page rendering times before and after to see if it helps.
|
139
151
|
|
140
152
|
### Benefits
|
141
153
|
|
@@ -190,7 +202,7 @@ If the cached content is rarely retrieved, `cache_replace` can help:
|
|
190
202
|
```
|
191
203
|
|
192
204
|
```haml
|
193
|
-
=
|
205
|
+
= render_cached 'common_interests', replace: { something: 'common_interests/inner' }
|
194
206
|
```
|
195
207
|
|
196
208
|
#### Faster first page loads
|
@@ -24,7 +24,7 @@ module CacheRocket
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
|
27
|
+
private
|
28
28
|
|
29
29
|
def replace_from_hash(hash)
|
30
30
|
hash.each do |key, value|
|
@@ -43,7 +43,7 @@ module CacheRocket
|
|
43
43
|
end
|
44
44
|
|
45
45
|
def replace_item_hash(item, hash)
|
46
|
-
item_fragment =
|
46
|
+
item_fragment = value.dup
|
47
47
|
|
48
48
|
hash.each do |key, proc|
|
49
49
|
item_fragment.gsub! cache_replace_key(key), proc.call(item)
|
@@ -51,6 +51,5 @@ module CacheRocket
|
|
51
51
|
|
52
52
|
item_fragment
|
53
53
|
end
|
54
|
-
|
55
54
|
end
|
56
|
-
end
|
55
|
+
end
|
data/lib/cache_rocket/key.rb
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
module CacheRocket
|
2
2
|
module Key
|
3
|
-
CACHE_REPLACE_KEY_OPEN
|
3
|
+
CACHE_REPLACE_KEY_OPEN = "<cr "
|
4
4
|
|
5
5
|
# string key containing the partial file name or placeholder key.
|
6
6
|
# It is a tag that should never be returned to be rendered by the
|
7
7
|
# client, but if so, it will be hidden since CR is not a valid html tag.
|
8
8
|
def cache_replace_key(key)
|
9
|
-
"#{CACHE_REPLACE_KEY_OPEN}#{key
|
9
|
+
"#{CACHE_REPLACE_KEY_OPEN}#{key}>".html_safe
|
10
10
|
end
|
11
|
-
|
12
11
|
end
|
13
|
-
end
|
12
|
+
end
|
data/lib/cache_rocket/version.rb
CHANGED
data/lib/cache_rocket.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
1
|
+
require "active_support/core_ext/string"
|
2
|
+
require "cache_rocket/key"
|
3
|
+
require "cache_rocket/fragment"
|
4
|
+
require "cache_rocket/version"
|
5
5
|
|
6
6
|
module CacheRocket
|
7
7
|
include Key
|
8
8
|
|
9
9
|
ERROR_MISSING_KEY_OR_BLOCK = "You must either pass a `replace` key or a block to render_cached."
|
10
|
-
|
10
|
+
|
11
11
|
# Supports 5 options:
|
12
12
|
#
|
13
|
-
# 1. Single partial to replace.
|
13
|
+
# 1. Single partial to replace.
|
14
14
|
# "inner" is the key name and "_inner.*" is the partial file name.
|
15
15
|
#
|
16
16
|
# render_cached "container", replace: "inner"
|
@@ -33,7 +33,7 @@ module CacheRocket
|
|
33
33
|
#
|
34
34
|
# render_cached "partial", collection: objects, replace: { key_name: ->(object){a_method(object)} }
|
35
35
|
#
|
36
|
-
def render_cached(partial, options={})
|
36
|
+
def render_cached(partial, options = {})
|
37
37
|
replace_hash = options.delete(:replace)
|
38
38
|
collection = options.delete(:collection)
|
39
39
|
|
@@ -43,16 +43,14 @@ module CacheRocket
|
|
43
43
|
when Hash
|
44
44
|
fragment.replace replace_hash, collection
|
45
45
|
when NilClass
|
46
|
-
raise
|
46
|
+
raise(ArgumentError, ERROR_MISSING_KEY_OR_BLOCK) unless block_given?
|
47
47
|
fragment.replace yield, collection
|
48
48
|
else
|
49
|
-
|
50
|
-
key_array.each do |key|
|
49
|
+
[*replace_hash].each do |key|
|
51
50
|
fragment.gsub! cache_replace_key(key), render(key, options)
|
52
51
|
end
|
53
52
|
end
|
54
53
|
|
55
54
|
fragment.to_s.html_safe
|
56
55
|
end
|
57
|
-
|
58
56
|
end
|
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.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tee Parham
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-12-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -30,56 +30,28 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '10.
|
33
|
+
version: '10.3'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '10.
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: minitest
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
40
|
+
version: '10.3'
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: mocha
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
58
44
|
requirements:
|
59
45
|
- - "~>"
|
60
46
|
- !ruby/object:Gem::Version
|
61
|
-
version: '1.
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - "~>"
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '1.0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: pry
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - "~>"
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0.9'
|
47
|
+
version: '1.1'
|
76
48
|
type: :development
|
77
49
|
prerelease: false
|
78
50
|
version_requirements: !ruby/object:Gem::Requirement
|
79
51
|
requirements:
|
80
52
|
- - "~>"
|
81
53
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
54
|
+
version: '1.1'
|
83
55
|
description: Rails rendering extension for server-side html caching
|
84
56
|
email:
|
85
57
|
- tee@neighborland.com
|
@@ -87,22 +59,12 @@ executables: []
|
|
87
59
|
extensions: []
|
88
60
|
extra_rdoc_files: []
|
89
61
|
files:
|
90
|
-
- ".gitignore"
|
91
|
-
- ".ruby-version"
|
92
|
-
- ".travis.yml"
|
93
|
-
- Gemfile
|
94
62
|
- LICENSE.txt
|
95
63
|
- README.md
|
96
|
-
- Rakefile
|
97
|
-
- cache_rocket.gemspec
|
98
64
|
- lib/cache_rocket.rb
|
99
65
|
- lib/cache_rocket/fragment.rb
|
100
66
|
- lib/cache_rocket/key.rb
|
101
67
|
- lib/cache_rocket/version.rb
|
102
|
-
- test/cache_rocket_test.rb
|
103
|
-
- test/fragment_test.rb
|
104
|
-
- test/key_test.rb
|
105
|
-
- test/test_helper.rb
|
106
68
|
homepage: https://github.com/neighborland/cache_rocket
|
107
69
|
licenses:
|
108
70
|
- MIT
|
@@ -123,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
85
|
version: '0'
|
124
86
|
requirements: []
|
125
87
|
rubyforge_project:
|
126
|
-
rubygems_version: 2.
|
88
|
+
rubygems_version: 2.5.1
|
127
89
|
signing_key:
|
128
90
|
specification_version: 4
|
129
91
|
summary: Rails rendering extension for server-side html caching
|
data/.gitignore
DELETED
data/.ruby-version
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
ruby-2.1.0
|
data/.travis.yml
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED
data/cache_rocket.gemspec
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'cache_rocket/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "cache_rocket"
|
8
|
-
spec.version = CacheRocket::VERSION
|
9
|
-
spec.authors = ["Tee Parham"]
|
10
|
-
spec.email = ["tee@neighborland.com"]
|
11
|
-
spec.description = %q{Rails rendering extension for server-side html caching}
|
12
|
-
spec.summary = %q{Rails rendering extension for server-side html caching}
|
13
|
-
spec.homepage = "https://github.com/neighborland/cache_rocket"
|
14
|
-
spec.license = "MIT"
|
15
|
-
|
16
|
-
spec.files = `git ls-files`.split($/)
|
17
|
-
spec.test_files = `git ls-files -- {test}/*`.split("\n")
|
18
|
-
spec.require_paths = ["lib"]
|
19
|
-
|
20
|
-
spec.required_ruby_version = ">= 1.9.3"
|
21
|
-
|
22
|
-
spec.add_dependency "actionpack", ">= 3.2"
|
23
|
-
|
24
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
-
spec.add_development_dependency "minitest"
|
26
|
-
spec.add_development_dependency "mocha", "~> 1.0"
|
27
|
-
spec.add_development_dependency "pry", "~> 0.9"
|
28
|
-
end
|
data/test/cache_rocket_test.rb
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class CacheRocketTest < MiniTest::Spec
|
4
|
-
class FakeRenderer
|
5
|
-
include CacheRocket
|
6
|
-
end
|
7
|
-
|
8
|
-
before do
|
9
|
-
@renderer = FakeRenderer.new
|
10
|
-
end
|
11
|
-
|
12
|
-
describe "#cache_replace_key" do
|
13
|
-
it "return key with prefix" do
|
14
|
-
assert_equal CacheRocket::CACHE_REPLACE_KEY_OPEN + "some/thing>", @renderer.cache_replace_key("some/thing")
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
describe "#render_cached" do
|
19
|
-
before do
|
20
|
-
@renderer.stubs(:render).with("container", {}).returns "Fanny pack #{@renderer.cache_replace_key('inner')} viral mustache."
|
21
|
-
@renderer.stubs(:render).with("inner", {}).returns "quinoa hoodie"
|
22
|
-
end
|
23
|
-
|
24
|
-
it "render with single partial" do
|
25
|
-
assert_equal "Fanny pack quinoa hoodie viral mustache.", @renderer.render_cached("container", replace: "inner")
|
26
|
-
end
|
27
|
-
|
28
|
-
it "pass options to inner render" do
|
29
|
-
@renderer.expects(:render).with("container", variable: "x").returns ""
|
30
|
-
@renderer.expects(:render).with("inner", variable: "x").returns ""
|
31
|
-
@renderer.render_cached("container", variable: "x", replace: "inner")
|
32
|
-
end
|
33
|
-
|
34
|
-
it "render with array of partials" do
|
35
|
-
@renderer.stubs(:render).with("container", {}).
|
36
|
-
returns "#{@renderer.cache_replace_key('inner')} #{@renderer.cache_replace_key('other')} viral mustache."
|
37
|
-
@renderer.stubs(:render).with("other", {}).returns "high life"
|
38
|
-
|
39
|
-
assert_equal "quinoa hoodie high life viral mustache.",
|
40
|
-
@renderer.render_cached("container", replace: ["inner", "other"])
|
41
|
-
end
|
42
|
-
|
43
|
-
it "render with map of keys" do
|
44
|
-
assert_equal "Fanny pack keytar viral mustache.", @renderer.render_cached("container", replace: {inner: "keytar"})
|
45
|
-
end
|
46
|
-
|
47
|
-
it "render with map of keys with a nil value" do
|
48
|
-
assert_equal "Fanny pack viral mustache.", @renderer.render_cached("container", replace: {inner: nil})
|
49
|
-
end
|
50
|
-
|
51
|
-
it "render with hash block" do
|
52
|
-
output = @renderer.render_cached("container") do
|
53
|
-
{inner: "keytar"}
|
54
|
-
end
|
55
|
-
assert_equal "Fanny pack keytar viral mustache.", output
|
56
|
-
end
|
57
|
-
|
58
|
-
it "replace every instance of key in inner partial" do
|
59
|
-
@renderer.stubs(:render).with("container", {}).
|
60
|
-
returns "#{@renderer.cache_replace_key('inner')} #{@renderer.cache_replace_key('inner')} viral mustache."
|
61
|
-
|
62
|
-
assert_equal "quinoa hoodie quinoa hoodie viral mustache.",
|
63
|
-
@renderer.render_cached("container", replace: "inner")
|
64
|
-
end
|
65
|
-
|
66
|
-
it "replace every instance of the keys with hash values" do
|
67
|
-
@renderer.stubs(:render).with("container", {}).
|
68
|
-
returns "I like #{@renderer.cache_replace_key('beer')}, #{@renderer.cache_replace_key('beer')} and #{@renderer.cache_replace_key('food')}."
|
69
|
-
|
70
|
-
assert_equal "I like stout, stout and chips.",
|
71
|
-
@renderer.render_cached("container", replace: {food: "chips", beer: 'stout'})
|
72
|
-
end
|
73
|
-
|
74
|
-
it "replace collection with Proc in replace key" do
|
75
|
-
def dog_name(dog) dog end
|
76
|
-
|
77
|
-
@renderer.stubs(:render).with("partial", {}).returns "Hi #{@renderer.cache_replace_key(:dog)}."
|
78
|
-
dogs = %w[Snoop Boo]
|
79
|
-
|
80
|
-
assert_equal "Hi Snoop.Hi Boo.",
|
81
|
-
@renderer.render_cached("partial", collection: dogs, replace: {dog: ->(dog) { dog_name(dog) } })
|
82
|
-
end
|
83
|
-
|
84
|
-
it "replace collection using hash block with Proc" do
|
85
|
-
def dog_name(dog) dog end
|
86
|
-
|
87
|
-
@renderer.stubs(:render).with("partial", {}).returns "Hi #{@renderer.cache_replace_key(:dog)}."
|
88
|
-
dogs = %w[Snoop Boo]
|
89
|
-
|
90
|
-
rendered = @renderer.render_cached("partial", collection: dogs) do
|
91
|
-
{ dog: ->(dog) { dog_name(dog) } }
|
92
|
-
end
|
93
|
-
|
94
|
-
assert_equal "Hi Snoop.Hi Boo.", rendered
|
95
|
-
end
|
96
|
-
|
97
|
-
it "replace collection with multiple procs" do
|
98
|
-
def dog_name(dog) dog end
|
99
|
-
def reverse(dog) dog.reverse end
|
100
|
-
|
101
|
-
@renderer.stubs(:render).with("partial", {}).returns "#{@renderer.cache_replace_key(:reverse)} #{@renderer.cache_replace_key(:dog)}."
|
102
|
-
dogs = %w[Snoop Boo]
|
103
|
-
|
104
|
-
rendered = @renderer.render_cached("partial", collection: dogs) do
|
105
|
-
{ dog: ->(dog) { dog_name(dog) }, reverse: ->(dog) { reverse(dog) } }
|
106
|
-
end
|
107
|
-
|
108
|
-
assert_equal "poonS Snoop.ooB Boo.", rendered
|
109
|
-
end
|
110
|
-
|
111
|
-
it "raise ArgumentError with invalid syntax" do
|
112
|
-
@renderer.stubs(:render).with("container").returns("")
|
113
|
-
assert_raises(ArgumentError) do
|
114
|
-
@renderer.render_cached("container")
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
data/test/fragment_test.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module CacheRocket
|
4
|
-
class FragmentTest < MiniTest::Spec
|
5
|
-
describe "#to_s" do
|
6
|
-
it "equal value" do
|
7
|
-
assert_equal "yo", Fragment.new("yo").to_s
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
describe "#gsub!" do
|
12
|
-
it "substitute value" do
|
13
|
-
fragment = Fragment.new("hello there, hello.")
|
14
|
-
assert_equal "yo there, yo.", fragment.gsub!("hello", "yo")
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
describe "#replace" do
|
19
|
-
it "replace cache keys from hash" do
|
20
|
-
cr_key = Fragment.new(nil).cache_replace_key(:xx)
|
21
|
-
fragment = Fragment.new("hey #{cr_key} hey.")
|
22
|
-
fragment.replace({xx: "yo"}, nil)
|
23
|
-
assert_equal "hey yo hey.", fragment.value
|
24
|
-
end
|
25
|
-
|
26
|
-
it "replace collection with Proc" do
|
27
|
-
def last5(object)
|
28
|
-
object.last(5)
|
29
|
-
end
|
30
|
-
replace_hash = { something: ->(object){ last5(object)} }
|
31
|
-
collection = %w[xxTiger xxSkunk]
|
32
|
-
cr_key = Fragment.new(nil).cache_replace_key(:something)
|
33
|
-
fragment = Fragment.new("hey #{cr_key} hey.")
|
34
|
-
assert_equal "hey Tiger hey.hey Skunk hey.",
|
35
|
-
fragment.replace(replace_hash, collection)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
40
|
-
end
|
data/test/key_test.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module CacheRocket
|
4
|
-
class KeyTest < MiniTest::Spec
|
5
|
-
|
6
|
-
describe "#cache_replace_key" do
|
7
|
-
it "return key with prefix" do
|
8
|
-
class KeyFake
|
9
|
-
include Key
|
10
|
-
end
|
11
|
-
|
12
|
-
key = KeyFake.new
|
13
|
-
assert_equal CacheRocket::Key::CACHE_REPLACE_KEY_OPEN + "some/thing>", key.cache_replace_key("some/thing")
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|