cells 3.9.1 → 3.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f2a369ee523b519082124fc42f359172124f6e45
4
- data.tar.gz: bc788476880fec68c6134ab8111f5ea62d519b26
3
+ metadata.gz: db50f24112bb37938584135fab9266353fe79b7d
4
+ data.tar.gz: dbccc6a922a93e77ee3d3e9ec44f87f95835eed0
5
5
  SHA512:
6
- metadata.gz: 54a6bbc71fd9fadae7fd54a940bf1c160b9675e03eff6b2320f5dd4c590fc22229f692e6f7db77b06ed3c64ef09186cee732cbda52e7a6c55295a7c46dfea7c0
7
- data.tar.gz: 2271f4d6188a31b7b2c64617fffaf83adc30c8c3cff7c18124072fb8aff1c03c5f3fb267b87cf313099eb5a70c21571bd35a5f324428c632ab7319a61bbd188a
6
+ metadata.gz: 0f4b0a3a5ccb60348172268dd14b64c5e246549ccc49b3f3aa3da15f8b40dd96785bd92b7ad875fb881f632dfbfaae93d6b507decd1de22b2821147313e1e81f
7
+ data.tar.gz: 74d5ab523f9075541a9472a20eb3ffdfe3d420e42f26a4e9be22769b955351c3fa3b03f47108a16b0e440825e28c9ca2e0b91cbb63cdb0b94a2f67203631ca14
@@ -1,9 +1,40 @@
1
- h2. 3.9.1
1
+ ## 3.10.0
2
+
3
+ * API CHANGE: Blocks passed to `::cache` and `::cache ... if: ` no longer receive the cell instance as the first argument. Instead, they're executed in cell instance context. Change your code like this:
4
+ ```ruby
5
+ cache :show do |cell, options|
6
+ cell.version
7
+ end
8
+ # and
9
+ cache :show, if: lambda {|cell, options| .. }
10
+ ```
11
+ should become
12
+
13
+ ```ruby
14
+ cache :show do |options|
15
+ version
16
+ end
17
+ # and
18
+ cache :show, if: lambda {|options| .. }
19
+ ```
20
+
21
+ Since the blocks are run in cell context, `self` will point to what was `cell` before.
22
+
23
+
24
+ * `::cache` doesn't accept a `Proc` instance anymore, only blocks (was undocumented anyway).
25
+ * Use [`uber` gem](https://github.com/apotonick/uber) for inheritable class attributes and dynamic options.
26
+
27
+ ## 3.9.2
28
+
29
+ * Autoload `Cell::Rails::ViewModel`.
30
+ * Implement dynamic cache options by allowing lambdas that are executed at render-time - Thanks to @bibendi for this idea.
31
+
32
+ ## 3.9.1
2
33
 
3
34
  * Runs with Rails 4.1 now.
4
35
  * Internal changes on `Layouts` to prepare 4.1 compat.
5
36
 
6
- h2. 3.9.0
37
+ ## 3.9.0
7
38
 
8
39
  * Cells in engines are now recognized under Rails 4.0.
9
40
  * Introducing @#cell@ and @#cell_for@ to instantiate cells in ActionController and ActionView.
@@ -11,43 +42,43 @@ h2. 3.9.0
11
42
  * Add @Cell::Base#process_args@ which is called in the initializer to handle arguments passed into the constructor.
12
43
  * Setting @controller in your @Cell::TestCase@ no longer get overridden by us.
13
44
 
14
- h2. 3.8.8
45
+ ## 3.8.8
15
46
 
16
47
  * Maintenance release.
17
48
 
18
- h2. 3.8.7
49
+ ## 3.8.7
19
50
 
20
51
  * Cells runs with Rails 4.
21
52
 
22
- h2. 3.8.6
53
+ ## 3.8.6
23
54
 
24
55
  * @cell/base@ can now be required without trouble.
25
56
  * Generated test files now respect namespaced cells.
26
57
 
27
- h2. 3.8.5
58
+ ## 3.8.5
28
59
 
29
60
  * Added @Cell::Rails::HelperAPI@ module to provide the entire Rails view "API" (quotes on purpose!) in cells running completely outside of Rails. This makes it possible to use gems like simple_form in any Ruby environment, especially interesting for people using Sinatra, webmachine, etc.
30
61
  * Moved @Caching.expire_cache_key@ to @Rails@. Use @Caching.expire_cache_key_for(key, cache_store, ..)@ if you want to expire caches outside of Rails.
31
62
 
32
- h2. 3.8.4
63
+ ## 3.8.4
33
64
 
34
65
  * Added @Cell::Rack@ for request-dependent Cells. This is also the new base class for @Cells::Rails@.
35
66
  * Removed deprecation warning from @TestCase#cell@ as it's signature is not deprecated.
36
67
  * Added the @base_cell_class@ config option to generator for specifying an alternative base class.
37
68
 
38
- h2. 3.8.3
69
+ ## 3.8.3
39
70
 
40
71
  * Added @Engines.existent_directories_for@ to prevent Rails 3.0 from crashing when it detects engines.
41
72
 
42
- h2. 3.8.2
73
+ ## 3.8.2
43
74
 
44
75
  * Engines should work in Rails 3.0 now, too.
45
76
 
46
- h2. 3.8.1
77
+ ## 3.8.1
47
78
 
48
79
  * Make it work with Rails 3.2 by removing deprecated stuff.
49
80
 
50
- h2. 3.8.0
81
+ ## 3.8.0
51
82
 
52
83
  * @Cell::Base@ got rid of the controller dependency. If you want the @ActionController@ instance around in your cell, use @Cell::Rails@ - this should be the default in a standard Rails setup. However, if you plan on using a Cell in a Rack middleware or don't need the controller, use @Cell::Base@.
53
84
  * New API (note that @controller@ isn't the first argument anymore):
@@ -57,11 +88,11 @@ h2. 3.8.0
57
88
  * @DEFAULT_VIEW_PATHS@ is now in @Cell::Base@.
58
89
  * Removed the monkey-patch that made state-args work in Rails <= 3.0.3. Upgrade to +3.0.4.
59
90
 
60
- h2. 3.7.1
91
+ ## 3.7.1
61
92
 
62
93
  * Works with Rails 3.2, too. Hopefully.
63
94
 
64
- h2. 3.7.0
95
+ ## 3.7.0
65
96
 
66
97
  h3. Changes
67
98
  * Cache settings using @Base.cache@ are now inherited.
@@ -69,24 +100,24 @@ h3. Changes
69
100
  * Removed @#options@ in favor of state-args. If you still want the old behaviour, include the @Deprecations@ module in your cell.
70
101
  * The build process is now instantly delegated to Base.build_for on the concrete cell class.
71
102
 
72
- h2. 3.6.8
103
+ ## 3.6.8
73
104
 
74
105
  h3. Changes
75
106
  * Removed <code>@opts</code>.
76
107
  * Deprecated @#options@ in favour of state-args.
77
108
 
78
- h2. 3.6.7
109
+ ## 3.6.7
79
110
 
80
111
  h3. Changes
81
112
  * Added @view_assigns@ to TestCase.
82
113
 
83
- h2. 3.6.6
114
+ ## 3.6.6
84
115
 
85
116
  h3. Changes
86
117
  * Added the @:format@ option for @#render@ which should be used with caution. Sorry for that.
87
118
  * Removed the useless @layouts/@ view path from Cell::Base.
88
119
 
89
- h2. 3.6.5
120
+ ## 3.6.5
90
121
 
91
122
  h3. Bugfixes
92
123
  * `Cell::TestCase#invoke` now properly accepts state-args.
@@ -95,13 +126,13 @@ h3. Changes
95
126
  * Added the `:if` option to `Base.cache` which allows adding a conditional proc or instance method to the cache definition. If it doesn't return true, caching for that state is skipped.
96
127
 
97
128
 
98
- h2. 3.6.4
129
+ ## 3.6.4
99
130
 
100
131
  h3. Bugfixes
101
132
  * Fixes @ArgumentError: wrong number of arguments (1 for 0)@ in @#render_cell@ for Ruby 1.8.
102
133
 
103
134
 
104
- h2. 3.6.3
135
+ ## 3.6.3
105
136
 
106
137
  h3. Bugfixes
107
138
  * [Rails 3.0] Helpers are now properly included (only once). Thanks to [paneq] for a fix.
@@ -111,30 +142,30 @@ h3. Changes
111
142
  * `cells/test_case` is no longer required as it should be loaded automatically.
112
143
 
113
144
 
114
- h2. 3.6.2
145
+ ## 3.6.2
115
146
 
116
147
  h3. Bugfixes
117
148
  * Fixed cells.gemspec to allow Rails 3.x.
118
149
 
119
- h2. 3.6.1
150
+ ## 3.6.1
120
151
 
121
152
  h3. Changes
122
153
  * Added the @:format@ option allowing @#render@ to set different template types, e.g. @render :format => :json@.
123
154
 
124
155
 
125
- h2. 3.6.0
156
+ ## 3.6.0
126
157
 
127
158
  h3. Changes
128
159
  * Cells runs with Rails 3.0 and 3.1.
129
160
 
130
161
 
131
- h2. 3.5.6
162
+ ## 3.5.6
132
163
 
133
164
  h3. Changes
134
165
  * Added a generator for slim. Use it with `-e slim` when generating.
135
166
 
136
167
 
137
- h2. 3.5.5
168
+ ## 3.5.5
138
169
 
139
170
  h3. Bugfixes
140
171
  * The generator now places views of namespaced cells into the correct directory. E.g. `rails g Blog::Post display` puts views to `app/cells/blog/post/display.html.erb`.
@@ -143,7 +174,7 @@ h3. Changes
143
174
  * Gem dependencies changed, we now require @actionpack@ and @railties@ >= 3.0.0 instead of @rails@.
144
175
 
145
176
 
146
- h2. 3.5.4
177
+ ## 3.5.4
147
178
 
148
179
  h3. Bugfixes
149
180
  * state-args work even if your state method receives optional arguments or default values, like @def show(user, age=18)@.
@@ -154,7 +185,7 @@ h3. Changes
154
185
  * Cells::DEFAULT_VIEW_PATHS is now Cell::Base::DEFAULT_VIEW_PATHS. Note that Cells will set its view_paths to DEFAULT_VIEW_PATHS at initialization time. If you want to alter the view_paths, use Base.append_view_path and friends in a separate initializer.
155
186
 
156
187
 
157
- h2. 3.5.2
188
+ ## 3.5.2
158
189
 
159
190
  h3. Bugfixes
160
191
  * Controller#render_cell now accepts multiple args as options.
@@ -163,7 +194,7 @@ h3. Changes
163
194
  * Caching versioners now can accept state-args or options from the #render_cell call. This way, you don't have to access #options at all anymore.
164
195
 
165
196
 
166
- h2. 3.5.1
197
+ ## 3.5.1
167
198
 
168
199
  * No longer pass an explicit Proc but a versioner block to @Cell.Base.cache@. Example: @cache :show do "v1" end@
169
200
  * Caching.cache_key_for now uses @ActiveSupport::Cache.expand_cache_key@. Consequently, a key which used to be like @"cells/director/count/a=1/b=2"@ now is @cells/director/count/a=1&b=2@ and so on. Be warned that this might break your home-made cache expiry.
@@ -173,7 +204,7 @@ h3. Bugfixes
173
204
  * Passing options to @render :state@ in views finally works: @render({:state => :list_item}, item, i)@
174
205
 
175
206
 
176
- h2. 3.5.0
207
+ ## 3.5.0
177
208
 
178
209
  h3. Changes
179
210
  * Deprecated @opts, use #options now.
@@ -186,7 +217,7 @@ h3. Changes
186
217
  * Privatized @#find_family_view_for_state@, @#render_view_for@, and all *ize methods in Cell::Rails.
187
218
  * New signature: @#render_view_for(state, *args)@
188
219
 
189
- h2. 3.4.4
220
+ ## 3.4.4
190
221
 
191
222
  h3. Changes
192
223
  * Cells.setup now yields Cell::Base, so you can really call append_view_path and friends here.
@@ -194,7 +225,7 @@ h3. Changes
194
225
  * added TestCase#in_view to test helpers in a real cell view.
195
226
 
196
227
 
197
- h2. 3.4.3
228
+ ## 3.4.3
198
229
 
199
230
  h3. Changes
200
231
  * #render_cell now accepts a block which yields the cell instance before rendering.
data/Gemfile CHANGED
@@ -3,3 +3,6 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  #gem "rails" , :path => "../rayls"
6
+
7
+ gem "rails", "~> 3.2.12"
8
+ gem 'minitest', '4.7.5'
data/README.md CHANGED
@@ -130,6 +130,7 @@ This is where OOP comes back to your view.
130
130
  * __Inherit code__ into your cells by deriving more abstract cells.
131
131
  * __Inherit views__ from parent cells.
132
132
 
133
+
133
134
  ### Builders
134
135
 
135
136
  Let `render_cell` take care of creating the right cell. Just configure your super-cell properly.
@@ -152,29 +153,85 @@ will render the configured `UnauthorizedUserCell` instead of the original `Login
152
153
 
153
154
  ## Caching
154
155
 
155
- Cells do strict view caching. No cluttered fragment caching. Add
156
+ Cells allow you to cache per state. It's simple: the rendered result of a state method is cached and expired as you configure it.
157
+
158
+ To cache forever, don't configure anything
159
+
160
+ ```ruby
161
+ class CartCell < Cell::Rails
162
+ cache :show
163
+
164
+ def show
165
+ render
166
+ end
167
+ ```
168
+
169
+ This will run `#show` only once, after that the rendered view comes from the cache.
170
+
171
+
172
+ ### Cache Options
173
+
174
+ Note that you can pass arbitrary options through to your cache store. Symbols are evaluated as instance methods, callable objects (e.g. lambdas) are evaluated in the cell instance context allowing you to call instance methods and access instance variables. All arguments passed to your state (e.g. via `render_cell`) are propagated to the block.
175
+
176
+ ```ruby
177
+ cache :show, :expires_in => 10.minutes
178
+ ```
179
+
180
+ If you need dynamic options evaluated at render-time, use a lambda.
181
+
182
+ ```ruby
183
+ cache :show, :tags => lambda { |*args| tags }
184
+ ```
185
+
186
+ If you don't like blocks, use instance methods instead.
156
187
 
157
188
  ```ruby
158
189
  class CartCell < Cell::Rails
159
- cache :show, :expires_in => 10.minutes
190
+ cache :show, :tags => :cache_tags
191
+
192
+ def cache_tags(*args)
193
+ # do your magic..
194
+ end
195
+ ```
196
+
197
+ ### Conditional Caching
198
+
199
+ The +:if+ option lets you define a condition. If it doesn't return a true value, caching for that state is skipped.
200
+
201
+ ```ruby
202
+ cache :show, :if => lambda { |*| has_changed? }
160
203
  ```
161
204
 
162
- and your cart will be re-rendered after 10 minutes.
205
+ ### Cache Keys
163
206
 
164
- You can expand the state's cache key - why not use a versioner block to do just this?
207
+ You can expand the state's cache key by appending a versioner block to the `::cache` call. This way you can expire state caches yourself.
165
208
 
166
209
  ```ruby
167
210
  class CartCell < Cell::Rails
168
- cache :show do |cell, options|
211
+ cache :show do |options|
169
212
  options[:items].md5
170
213
  end
171
214
  ```
172
215
 
173
216
  The block's return value is appended to the state key: `"cells/cart/show/0ecb1360644ce665a4ef"`.
174
217
 
175
- Check the [API to learn more](http://rdoc.info/gems/cells/Cell/Caching/ClassMethods#cache-instance_method).
176
218
 
177
- *Reminder*: If you want to test it in `development`, you need to put `config.action_controller.perform_caching = true` in `development.rb` to see the effect
219
+ ### Inheritance
220
+
221
+ Cache configuration is inherited to derived cells.
222
+
223
+
224
+ ### A Note On Fragment Caching
225
+
226
+ Fragment caching is [not implemented in Cells per design](http://nicksda.apotomo.de/2011/02/rails-misapprehensions-caching-views-is-not-the-views-job/) - Cells tries to move caching to the class layer enforcing an object-oriented design rather than cluttering your views with caching blocks.
227
+
228
+ If you need to cache a part of your view, implement that as another cell state.
229
+
230
+
231
+ ### Testing Caching
232
+
233
+ If you want to test it in `development`, you need to put `config.action_controller.perform_caching = true` in `development.rb` to see the effect.
234
+
178
235
 
179
236
  ## Testing
180
237
 
@@ -221,7 +278,7 @@ rake spec:cells
221
278
 
222
279
  # View Models
223
280
 
224
- Cells 3.9 brings a new dialect to cells: view models (still experimental!).
281
+ Cells 3.9 brings a new dialect to cells: view models.
225
282
 
226
283
  Think of a view model as a cell decorating a model or a collection. In this mode, helpers are nothing more than instance methods of your cell class, making helpers predictable and scoped.
227
284
 
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
21
21
 
22
22
  s.add_dependency "actionpack", ">= 3.0"
23
23
  s.add_dependency "railties", ">= 3.0"
24
+ s.add_dependency "uber", "~> 0.0.4"
24
25
 
25
26
  s.add_development_dependency "rake"
26
27
  s.add_development_dependency "haml"
@@ -1,63 +1,30 @@
1
1
  require 'active_support/concern'
2
2
  require 'active_support/cache'
3
+ require 'uber/options'
4
+ require 'uber/inheritable_attr'
3
5
 
4
6
  module Cell
5
7
  module Caching
6
8
  extend ActiveSupport::Concern
7
9
 
8
10
  included do
9
- class_attribute :version_procs, :conditional_procs, :cache_options
11
+ extend Uber::InheritableAttr
12
+ inheritable_attr :version_procs
13
+ inheritable_attr :conditional_procs
14
+ inheritable_attr :cache_options
15
+
10
16
  self.version_procs = {}
11
17
  self.conditional_procs = {}
12
- self.cache_options = {}
18
+ self.cache_options = Uber::Options.new({})
13
19
  end
14
20
 
15
21
  module ClassMethods
16
- # Caches the rendered view of +state+.
17
- #
18
- # Examples:
19
- #
20
- # This will cache forever.
21
- #
22
- # class CartCell < Cell::Base
23
- # cache :show
24
- #
25
- # You can also pass options to the caching engine as known from Rails caching.
26
- #
27
- # cache :show, :expires_in => 10.minutes
28
- #
29
- # The +:if+ option lets you define a conditional proc or instance method. If it doesn't
30
- # return a true value, caching for that state is skipped.
31
- #
32
- # cache :show, :if => proc { |cell, options| options[:enable_cache] }
33
- #
34
- # If you need your own granular cache keys, pass a versioner block.
35
- #
36
- # cache :show do |cell, options|
37
- # "user/#{cell.options[:id]}"
38
- # end
39
- #
40
- # This will result in a cache key like <tt>cells/cart/show/user/1</tt>.
41
- #
42
- # Alternatively, use an instance method.
43
- #
44
- # cache :show, :versioner
45
- # def versioner(options)
46
- # "user/#{options[:id]}"
47
- # end
48
- #
49
- # Two things to mention here.
50
- # * The return value of the method/block is <em>appended</em> to the state cache key.
51
- # * You may return a string, a hash, an array, ActiveSupport::Caching will compile it.
52
- #
53
- # == Inheritance
54
- # Please note that cache configuration is inherited to derived cells.
55
22
  def cache(state, *args, &block)
56
23
  options = args.extract_options!
57
24
 
58
- self.conditional_procs = conditional_procs.merge(state => options.delete(:if))
59
- self.version_procs = version_procs.merge(state => (args.first || block))
60
- self.cache_options = cache_options.merge(state => options)
25
+ self.conditional_procs[state] = Uber::Options::Value.new(options.delete(:if) || true)
26
+ self.version_procs[state] = Uber::Options::Value.new(args.first || block)
27
+ self.cache_options[state] = Uber::Options.new(options)
61
28
  end
62
29
 
63
30
  # Computes the complete, namespaced cache key for +state+.
@@ -68,11 +35,9 @@ module Cell
68
35
  def expire_cache_key_for(key, cache_store, *args)
69
36
  cache_store.delete(key, *args)
70
37
  end
71
-
72
-
73
- protected
74
- # Compiles cache key and adds :cells namespace to +key+, according to the
75
- # ActiveSupport::Cache.expand_cache_key API.
38
+
39
+
40
+ private
76
41
  def expand_cache_key(key)
77
42
  ::ActiveSupport::Cache.expand_cache_key(key, :cells)
78
43
  end
@@ -81,45 +46,29 @@ module Cell
81
46
 
82
47
  def render_state(state, *args)
83
48
  return super(state, *args) unless cache?(state, *args)
84
-
85
- key = self.class.state_cache_key(state, call_state_versioner(state, *args))
86
- options = self.class.cache_options[state]
87
-
49
+
50
+ key = self.class.state_cache_key(state, self.class.version_procs[state].evaluate(self, *args))
51
+ options = self.class.cache_options.eval(state, self, *args)
52
+
88
53
  cache_store.fetch(key, options) do
89
54
  super(state, *args)
90
55
  end
91
56
  end
92
-
57
+
93
58
  def cache_configured?
94
59
  @cache_configured
95
60
  end
96
61
  attr_writer :cache_configured
97
-
62
+
98
63
  attr_accessor :cache_store # we want to use DI to set a cache store in cell/rails.
99
-
64
+
100
65
  def cache?(state, *args)
101
- cache_configured? and state_cached?(state) and call_state_conditional(state, *args)
66
+ cache_configured? and state_cached?(state) and self.class.conditional_procs[state].evaluate(self, *args)
102
67
  end
103
-
104
- protected
68
+
69
+ private
105
70
  def state_cached?(state)
106
71
  self.class.version_procs.has_key?(state)
107
72
  end
108
-
109
- def call_proc_or_method(state, method, *args)
110
- return method.call(self, *args) if method.kind_of?(Proc)
111
- send(method, *args)
112
- end
113
-
114
- def call_state_versioner(state, *args)
115
- method = self.class.version_procs[state] or return
116
- call_proc_or_method(state, method, *args)
117
- end
118
-
119
- def call_state_conditional(state, *args)
120
- method = self.class.conditional_procs[state] or return true
121
- call_proc_or_method(state, method, *args)
122
- end
123
-
124
73
  end
125
74
  end
@@ -51,5 +51,7 @@ module Cell
51
51
  end
52
52
  end
53
53
  include DSL
54
+
55
+ autoload :ViewModel, "cell/rails/view_model"
54
56
  end
55
57
  end
@@ -1,3 +1,3 @@
1
1
  module Cells
2
- VERSION = '3.9.1'
2
+ VERSION = '3.10.0'
3
3
  end
@@ -1,4 +1,4 @@
1
1
  h1
2
- <%= class_name %>#<%= @state %>
2
+ |<%= class_name %>#<%= @state %>
3
3
  p
4
- Find me in <%= @path %>
4
+ |Find me in <%= @path %>
@@ -87,8 +87,8 @@ class DeprecationsTest < MiniTest::Spec
87
87
  render :text => args[:int]
88
88
  end
89
89
 
90
- cache :count do |cell, i|
91
- (cell.options[:int] % 2)==0 ? {:count => "even"} : {:count => "odd"}
90
+ cache :count do |i|
91
+ (options[:int] % 2)==0 ? {:count => "even"} : {:count => "odd"}
92
92
  end
93
93
  end
94
94
 
@@ -126,6 +126,7 @@ class CachingUnitTest < MiniTest::Spec
126
126
  describe ".expire_cache_key" do
127
127
  before :each do
128
128
  @key = @class.state_cache_key(:tock)
129
+ puts "====== key is #{@key}"
129
130
  assert_equal "1", render_cell(:director, :tock)
130
131
  assert_equal "1", @class.cache_store.read(@key)
131
132
  end
@@ -157,79 +158,6 @@ class CachingUnitTest < MiniTest::Spec
157
158
  assert_not @class.cache_store.read(@key)
158
159
  end
159
160
  end
160
-
161
-
162
- describe ".cache" do
163
- let (:proc) { Proc.new {} }
164
- let (:parent) { Class.new(Cell::Base) }
165
- let (:brother) { Class.new(parent) }
166
- let (:sister) { Class.new(parent) }
167
-
168
- it "accept a state name, only" do
169
- @class.cache :count
170
-
171
- assert_not @class.version_procs[:count]
172
- assert_equal({}, @class.cache_options[:count])
173
- end
174
-
175
- it "accept state and cache options" do
176
- @class.cache :count, :expires_in => 10.minutes
177
-
178
- assert_not @class.version_procs[:count]
179
- assert_equal({:expires_in => 10.minutes}, @class.cache_options[:count])
180
- end
181
-
182
- it "accept args and versioner block" do
183
- @class.cache :count, :expires_in => 10.minutes do "v1" end
184
-
185
- assert_kind_of Proc, @class.version_procs[:count]
186
- assert_equal({:expires_in => 10.minutes}, @class.cache_options[:count])
187
- end
188
-
189
- it "stil accept a versioner proc, only" do
190
- @class.cache :count, proc
191
-
192
- assert_equal proc, @class.version_procs[:count]
193
- assert_equal({}, @class.cache_options[:count])
194
- end
195
-
196
- it "stil accept a versioner block" do
197
- @class.cache :count do "v1" end
198
-
199
- assert_kind_of Proc, @class.version_procs[:count]
200
- assert_equal({}, @class.cache_options[:count])
201
- end
202
-
203
- it "inherit caching configuration" do
204
- parent.cache :inherited_cache_configuration
205
-
206
- assert parent.version_procs.has_key?(:inherited_cache_configuration)
207
- assert brother.version_procs.has_key?(:inherited_cache_configuration)
208
- end
209
-
210
- it "not overwrite caching configuration in the parent class" do
211
- brother.cache :inherited_cache_configuration
212
-
213
- puts parent.version_procs.inspect
214
- assert ! parent.version_procs.has_key?(:inherited_cache_configuration)
215
- assert brother.version_procs.has_key?(:inherited_cache_configuration)
216
- end
217
-
218
- it "not overwrite caching configuration in a sibbling class" do
219
- sister.cache :inherited_cache_configuration
220
-
221
- assert ! brother.version_procs.has_key?(:inherited_cache_configuration)
222
- assert sister.version_procs.has_key?(:inherited_cache_configuration)
223
- end
224
-
225
- it "overwrite caching configuration in a child class" do
226
- @class.cache :inherited_cache_configuration
227
- brother.cache :inherited_cache_configuration, proc
228
-
229
- assert ! parent.version_procs[:inherited_cache_configuration]
230
- assert_equal proc, brother.version_procs[:inherited_cache_configuration]
231
- end
232
- end
233
161
  end
234
162
 
235
163
  class CachingFunctionalTest < MiniTest::Spec
@@ -287,7 +215,7 @@ class CachingFunctionalTest < MiniTest::Spec
287
215
  end
288
216
 
289
217
  it "compute the key with a block receiving state-args" do
290
- @class.cache :count do |cell, int|
218
+ @class.cache :count do |int|
291
219
  (int % 2)==0 ? {:count => "even"} : {:count => "odd"}
292
220
  end
293
221
  # example cache key: cells/director/count/count=odd
@@ -301,7 +229,7 @@ class CachingFunctionalTest < MiniTest::Spec
301
229
  it "compute the key with an instance method" do
302
230
  @class.cache :count, :version
303
231
  @class.class_eval do
304
- private
232
+ private
305
233
  def version(int)
306
234
  (int % 2)==0 ? {:count => "even"} : {:count => "odd"}
307
235
  end
@@ -313,8 +241,8 @@ class CachingFunctionalTest < MiniTest::Spec
313
241
  assert_equal "2", render_cell(:director, :count, 4)
314
242
  end
315
243
 
316
- it "allow returning strings, too" do
317
- @class.cache :count do |cell, int|
244
+ it "allows returning strings, too" do
245
+ @class.cache :count do |int|
318
246
  (int % 2)==0 ? "even" : "odd"
319
247
  end
320
248
 
@@ -325,7 +253,7 @@ class CachingFunctionalTest < MiniTest::Spec
325
253
  end
326
254
 
327
255
  it "be able to use caching conditionally" do
328
- @class.cache :count, :if => proc { |cell, int| (int % 2) != 0 }
256
+ @class.cache :count, :if => lambda { |int| (int % 2) != 0 }
329
257
 
330
258
  assert_equal "1", render_cell(:director, :count, 1)
331
259
  assert_equal "2", render_cell(:director, :count, 2)
@@ -381,4 +309,156 @@ class CachingFunctionalTest < MiniTest::Spec
381
309
  end
382
310
  end
383
311
  end
312
+
313
+
314
+ def cache_store(&block)
315
+ Object.new.instance_eval do
316
+ @block = block
317
+ def fetch(key, options)
318
+ @block.call(key, options)
319
+ end
320
+ self
321
+ end
322
+ end
323
+
324
+ def cached(&block)
325
+ cs = cache_store(&block)
326
+
327
+ @cell.instance_eval do
328
+ def count(*)
329
+ "this should never be returned!"
330
+ end
331
+
332
+ @cache_store = cs
333
+ def cache_store
334
+ @cache_store
335
+ end
336
+ end
337
+
338
+ @cell
339
+ end
340
+
341
+ # ::cache
342
+
343
+ describe "with state name, only" do
344
+ it do
345
+ @class.cache :count
346
+
347
+ cached do |key, options|
348
+ if options == {} and key == "cells/director/count/"
349
+ "cached!"
350
+ end
351
+ end.render_state(:count, 1, 2, 3).must_equal "cached!"
352
+ end
353
+ end
354
+
355
+ describe "with cache store options" do
356
+ it do
357
+ @class.cache :count, :expires_in => 10.minutes
358
+
359
+ cached do |key, options|
360
+ if options == {:expires_in => 600}
361
+ "cached!"
362
+ end
363
+ end.render_state(:count, 1, 2, 3).must_equal "cached!"
364
+ end
365
+ end
366
+
367
+ describe "with versioner block" do
368
+ it do
369
+ @class.cache :count do "v3" end
370
+
371
+ cached do |key, options|
372
+ if options == {} and key == "cells/director/count/v3"
373
+ "cached!"
374
+ end
375
+ end.render_state(:count).must_equal "cached!"
376
+ end
377
+
378
+ it "is executed in instance context" do
379
+ @class.cache :count do version end
380
+
381
+ @cell.instance_eval do
382
+ def version; "v3"; end
383
+ end
384
+
385
+ cached do |key, options|
386
+ if options == {} and key == "cells/director/count/v3"
387
+ "cached!"
388
+ end
389
+ end.render_state(:count).must_equal "cached!"
390
+ end
391
+ end
392
+
393
+ describe "with store options and versioner block" do
394
+ it do
395
+ @class.cache :count, :expires_in => 10.minutes do "v1" end
396
+
397
+ cached do |key, options|
398
+ if options == {:expires_in => 600} and key == "cells/director/count/v1"
399
+ "cached!"
400
+ end
401
+ end.render_state(:count, 1, 2, 3).must_equal "cached!"
402
+ end
403
+ end
404
+
405
+ describe "lambda and options as options" do
406
+ it "runs lamda at render-time" do
407
+ @class.cache :count, :expires_in => 9, :tags => lambda { |one, two, three| "#{one},#{two},#{three}" }
408
+
409
+ cached do |key, options|
410
+ if options == {:expires_in => 9, :tags => "1,2,3"}
411
+ "cached!"
412
+ end
413
+ end.render_state(:count, 1, 2, 3).must_equal "cached!"
414
+ end
415
+ end
416
+ end
417
+
418
+
419
+ class CachingInheritanceTest < CachingFunctionalTest
420
+ class DirectorCell < ::DirectorCell
421
+ cache :count, :expires_in => 10.minutes do
422
+ "v1"
423
+ end
424
+ end
425
+
426
+ class DirectorsSonCell < DirectorCell
427
+ end
428
+
429
+ class DirectorsDaughterCell < ::DirectorCell
430
+ cache :count, :expires_in => 9.minutes do
431
+ "v2"
432
+ end
433
+ end
434
+
435
+ it do
436
+ @cell = DirectorCell.new(@controller)
437
+
438
+ cached do |key, options|
439
+ if options == {:expires_in => 600} and key == "cells/caching_inheritance_test/director/count/v1"
440
+ "cached!"
441
+ end
442
+ end.render_state(:count).must_equal "cached!"
443
+ end
444
+
445
+ it do
446
+ @cell = DirectorsDaughterCell.new(@controller)
447
+
448
+ cached do |key, options|
449
+ if options == {:expires_in => 540} and key == "cells/caching_inheritance_test/directors_daughter/count/v2"
450
+ "cached!"
451
+ end
452
+ end.render_state(:count).must_equal "cached!"
453
+ end
454
+
455
+ it do
456
+ @cell = DirectorsSonCell.new(@controller)
457
+
458
+ cached do |key, options|
459
+ if options == {:expires_in => 600} and key == "cells/caching_inheritance_test/directors_son/count/v1"
460
+ "cached!"
461
+ end
462
+ end.render_state(:count).must_equal "cached!"
463
+ end
384
464
  end
@@ -1,5 +1,4 @@
1
- require 'test_helper'
2
- require 'cell/rails/view_model'
1
+ require 'test_helper'
3
2
 
4
3
  class Song < OpenStruct
5
4
  extend ActiveModel::Naming
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cells
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.9.1
4
+ version: 3.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Sutterer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-26 00:00:00.000000000 Z
11
+ date: 2014-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: uber
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.0.4
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.4
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -133,7 +147,7 @@ extra_rdoc_files: []
133
147
  files:
134
148
  - .gitignore
135
149
  - .travis.yml
136
- - CHANGES.textile
150
+ - CHANGES.md
137
151
  - Gemfile
138
152
  - README.md
139
153
  - Rakefile