cache_replace 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MTdiNzUyNjYyM2NkYzA2ZjhmMjcyNDE3Nzg0M2Q4NGViZjM0NzY3ZA==
5
+ data.tar.gz: !binary |-
6
+ NGZlMDZlYWNhOTVkNTE4ZTFiNzAyMGE1NTNmMDQyMTNhMTAwM2E2Yg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ M2FkMmIyY2Q2YTVjNzBlYzMwZjg4ODY2ZmU4NjI2NjE5MTE3ZDcyNDQ5MjEy
10
+ MDY0OTFlMGU1ZjI1YWU2MjJhNGZjNjJjMmM2Zjc3N2M0MzA4NGZkNDdkY2I5
11
+ NDU3YzdmMTVkODg5OWI2MmE1MGZjNjQyZDk1NzdkMjA5NDk2OWU=
12
+ data.tar.gz: !binary |-
13
+ NzlkNjU0ODkxMGU0OWE4OGYzZDliZTE0MDE3Mzg4NTQ4NDMyMjRhMDhlM2Mz
14
+ NGMyYzMzMGQ0NmU4ZjVlZjIwNDNjY2JmYzQ1Y2U5YmMyODVhYzE4MWExNGU0
15
+ ODRiZjJhMTUwZmU3YTgyZGFhYTE0ODVkOWYzOWEyZjRlMjZjZTQ=
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ Gemfile.lock
6
+ coverage
7
+ doc/
8
+ lib/bundler/man
9
+ pkg
10
+ rdoc
11
+ spec/reports
12
+ test/tmp
13
+ test/version_tmp
14
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tee Parham
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # Rails rendering extension for more server-side html caching
2
+
3
+ ## Why do I need this?
4
+
5
+ To squeeze the last drop out of server-side HTML caching in Rails. Use this only after you have configured as
6
+ much in-memory caching as possible using standard techniques such as Russian Doll caching.
7
+
8
+ ## Install
9
+
10
+ Add this line to your Gemfile:
11
+
12
+ gem 'cache_replace'
13
+
14
+ Add this line to a helper file, likely your ApplicationHelper:
15
+
16
+ include CacheReplace
17
+
18
+ ## Use
19
+
20
+ This gem allows you to easily cache a partial of static html and replace inner dynamic html. Here is an example
21
+ scenario where this is helpful:
22
+
23
+ You have some html that would be cached, except for some uncacheable code nested in the DOM. For example:
24
+
25
+ ##### file.html.haml:
26
+ ```
27
+ = render 'container'
28
+ ```
29
+
30
+ ##### _container.html.haml:
31
+ ```
32
+ .lots
33
+ .of
34
+ .htmls
35
+ = render 'dynamic'
36
+ ```
37
+
38
+ ##### _dynamic.html.haml:
39
+ ```
40
+ = complicated_uncacheable_stuff
41
+ ```
42
+
43
+ Sad times. You can't cache anything without resorting to madness. Oh snap! But you can:
44
+
45
+ ##### file.html.haml:
46
+ ```
47
+ = render_cached 'container', replace: 'dynamic'
48
+ ```
49
+
50
+ ##### _container.html.haml:
51
+ ```
52
+ - cache "container" do
53
+ .lots
54
+ .of
55
+ .htmls
56
+ = cache_replace_key 'dynamic'
57
+ ```
58
+
59
+ ##### _dynamic.html.haml:
60
+ ```
61
+ = complicated_uncacheable_stuff
62
+ ```
63
+
64
+ In the above example, you could also remove the `_dynamic.html.haml` file and do this:
65
+
66
+ ##### file.html.haml:
67
+ ```
68
+ = render_cached 'container', replace: {dynamic: complicated_uncacheable_stuff}
69
+ ```
70
+
71
+ ## Options
72
+
73
+ `render_cached` provides 4 calling styles:
74
+
75
+ #### Single partial to replace
76
+
77
+ ```ruby
78
+ render_cached "container", replace: "inner"
79
+ ```
80
+
81
+ #### Array of partials to replace
82
+ ```ruby
83
+ render_cached "container", replace: ["inner"]
84
+ ```
85
+
86
+ #### Map of keys to replace with values
87
+ ```ruby
88
+ render_cached "container", replace: {special_link: special_link(object)}
89
+ ```
90
+
91
+ #### Yield to a hash of keys to replace with values
92
+ ```ruby
93
+ render_cached "container" do
94
+ {special_link: special_link(object)}
95
+ end
96
+ ```
97
+
98
+ ## Contribute
99
+
100
+ 1. Fork it
101
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
102
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
103
+ 4. Push to the branch (`git push origin my-new-feature`)
104
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rake/testtask'
4
+
5
+ desc 'Run tests'
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.pattern = 'test/**/*_test.rb'
9
+ t.verbose = false
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cache_replace/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cache_replace"
8
+ spec.version = CacheReplace::VERSION
9
+ spec.authors = ["Tee Parham"]
10
+ spec.email = ["tee@neighborland.com"]
11
+ spec.description = %q{Rails rendering extension for more server-side html caching}
12
+ spec.summary = %q{Rails rendering extension for more server-side html caching}
13
+ spec.homepage = "https://github.com/teeparham/cache_replace"
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
+ # need #raw
21
+ spec.add_dependency "actionpack", ">= 3.2"
22
+
23
+ spec.add_development_dependency "bundler", ">= 1.3"
24
+ spec.add_development_dependency "rake", ">= 10.0"
25
+ spec.add_development_dependency "test-unit", ">= 2.5"
26
+ spec.add_development_dependency "mocha", ">= 0.13"
27
+ spec.add_development_dependency "shoulda", ">= 3.3"
28
+ spec.add_development_dependency "pry", ">= 0.9"
29
+ end
@@ -0,0 +1,58 @@
1
+ require 'cache_replace/version'
2
+
3
+ module CacheReplace
4
+ # Supports 4 options:
5
+ #
6
+ # 1. Single partial to replace
7
+ #
8
+ # render_cached "container", replace: "inner"
9
+ #
10
+ # 2. Array of partials to replace
11
+ #
12
+ # render_cached "container", replace: ["inner"]
13
+ #
14
+ # 3. Map of keys to replace with values
15
+ #
16
+ # render_cached "container", replace: {special_link: special_link(object)}
17
+ #
18
+ # 4. Yield to a hash of keys to replace with values
19
+ #
20
+ # render_cached "container" do
21
+ # {special_link: special_link(object)}
22
+ # end
23
+ #
24
+ def render_cached(static_partial, options={})
25
+ replace = options.delete(:replace)
26
+ fragment = render(static_partial, options)
27
+
28
+ case replace
29
+ when Hash
30
+ replace_from_hash fragment, replace
31
+ when NilClass
32
+ replace_from_hash fragment, yield
33
+ else
34
+ replace = *replace
35
+ replace.each do |key|
36
+ fragment.sub! cache_replace_key(key), render(key, options)
37
+ end
38
+ end
39
+
40
+ raw fragment
41
+ end
42
+
43
+ CACHE_FRAGMENT_KEY = '____>>'
44
+
45
+ # string key containing the partial file name or placeholder key
46
+ # it should return a string that your renderer will never contain
47
+ def cache_replace_key(key)
48
+ raw "#{CACHE_FRAGMENT_KEY}#{key.to_s}"
49
+ end
50
+
51
+ private
52
+
53
+ def replace_from_hash(fragment, hash)
54
+ hash.each do |key, value|
55
+ fragment.sub! cache_replace_key(key), value
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module CacheReplace
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,57 @@
1
+ require 'test_helper'
2
+
3
+ class CacheReplaceTest < Test::Unit::TestCase
4
+ class FakeRenderer
5
+ include CacheReplace
6
+
7
+ # mock ActionView::Helpers::OutputSafetyHelper
8
+ def raw(value)
9
+ value
10
+ end
11
+ end
12
+
13
+ setup do
14
+ @renderer = FakeRenderer.new
15
+ end
16
+
17
+ context "#cache_replace_key" do
18
+ should "return key with prefix" do
19
+ assert_equal CacheReplace::CACHE_FRAGMENT_KEY + "some/thing", @renderer.cache_replace_key("some/thing")
20
+ end
21
+ end
22
+
23
+ context "#render_cached" do
24
+ setup do
25
+ @renderer.stubs(:render).with("container", {}).returns "Fanny pack #{@renderer.cache_replace_key('inner')} viral mustache."
26
+ @renderer.stubs(:render).with("inner", {}).returns "quinoa hoodie"
27
+ end
28
+
29
+ should "render with single partial" do
30
+ assert_equal "Fanny pack quinoa hoodie viral mustache.", @renderer.render_cached("container", replace: "inner")
31
+ end
32
+
33
+ should "pass options to inner render" do
34
+ @renderer.expects(:render).with("container", variable: "x").returns ""
35
+ @renderer.expects(:render).with("inner", variable: "x").returns ""
36
+ @renderer.render_cached("container", variable: "x", replace: "inner")
37
+ end
38
+
39
+ should "render with array of partials" do
40
+ @renderer.stubs(:render).with("container", {}).
41
+ returns "#{@renderer.cache_replace_key('inner')} #{@renderer.cache_replace_key('other')} viral mustache."
42
+ @renderer.stubs(:render).with("other", {}).returns "high life"
43
+ assert_equal "quinoa hoodie high life viral mustache.", @renderer.render_cached("container", replace: ["inner", "other"])
44
+ end
45
+
46
+ should "render with map of keys" do
47
+ assert_equal "Fanny pack keytar viral mustache.", @renderer.render_cached("container", replace: {inner: "keytar"})
48
+ end
49
+
50
+ should "render with hash block" do
51
+ output = @renderer.render_cached("container") do
52
+ {inner: "keytar"}
53
+ end
54
+ assert_equal "Fanny pack keytar viral mustache.", output
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,5 @@
1
+ require 'test/unit'
2
+ require 'shoulda'
3
+ require 'mocha/setup'
4
+ require 'cache_replace'
5
+ require 'pry'
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cache_replace
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tee Parham
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: test-unit
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '2.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '2.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mocha
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0.13'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0.13'
83
+ - !ruby/object:Gem::Dependency
84
+ name: shoulda
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '3.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '3.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0.9'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0.9'
111
+ description: Rails rendering extension for more server-side html caching
112
+ email:
113
+ - tee@neighborland.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - cache_replace.gemspec
124
+ - lib/cache_replace.rb
125
+ - lib/cache_replace/version.rb
126
+ - test/cache_replace_test.rb
127
+ - test/test_helper.rb
128
+ homepage: https://github.com/teeparham/cache_replace
129
+ licenses:
130
+ - MIT
131
+ metadata: {}
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ! '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 2.0.3
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: Rails rendering extension for more server-side html caching
152
+ test_files: []