condi 0.0.7 → 0.0.8

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.
Files changed (3) hide show
  1. data/lib/condi.rb +13 -5
  2. data/test/test_condi.rb +28 -2
  3. metadata +2 -2
@@ -30,24 +30,32 @@ module Condi
30
30
  # @param [Proc] block {} or do...end block.
31
31
  # @note A *synonym* is an alias for defining methods that return values other than true or false.
32
32
  # @note You are not required to end a predicate with a question mark, however it is conventional in Ruby to do so.
33
- # @note Predicates can only be called during the request context they are defined in, otherwise a RuntimeError is raised. This restriction prevents the associated closures from inadvertently leaking previous request data when the controller classes are cached (i.e. in production).
33
+ # @note Predicates can only be called during the request scope they are defined in, otherwise a RuntimeError is raised. This restriction prevents the associated closures from inadvertently leaking previous request data when the controller classes are cached (i.e. in production).
34
+ # @note Predicates are semantically lambdas and may contain returns.
34
35
  # @see the full example in the <a href="index.html">README</a>.
35
36
  def predicate(method_name, &block)
36
37
  self.class.instance_eval do
37
38
  # this is the request id at the moment the predicate is defined
38
39
  request_id = eval("request.object_id",block.binding)
39
40
 
41
+ # We need to keep the block impl as a method/lambda so that it supports returns
42
+ # by using this particular invocation of define_method...
43
+ method_name_impl = "impl_#{method_name}".to_sym
44
+ define_method(method_name_impl, &block)
45
+
46
+ # Next, this invocation of define_method creates a Proc, which
47
+ # wraps the impl and allows us to check the request id.
40
48
  define_method(method_name) do |*args|
41
49
  # this is the request id at the moment the predicate is called
42
50
  check_request_id = request.object_id
43
-
44
51
  # if they don't match, raise an error!
45
52
  unless check_request_id == request_id
46
- #debugger
47
- raise RuntimeError, "predicate '#{method_name}' cannot be called outside of the request it was defined in (#{request_id}). please redefine the predicate in this request (#{check_request_id}).", caller(2)
53
+ raise RuntimeError, "predicate '#{method_name}' cannot be called outside of the request scope it was defined in (#{request_id}). please redefine the predicate in this request scope (#{check_request_id}).", caller(2)
48
54
  end
49
- block.call(*args)
55
+ send(method_name_impl, *args)
50
56
  end
57
+
58
+ # finally, expose the wrapped predicate to the view.
51
59
  helper_method(method_name)
52
60
  end
53
61
  end
@@ -142,7 +142,7 @@ class CondiTest < Test::Unit::TestCase
142
142
  end
143
143
 
144
144
 
145
- # fix for https://github.com/coldnebo/condi/issues/1
145
+ # test for https://github.com/coldnebo/condi/issues/1
146
146
  def test_lifespan
147
147
  @controller_instance.request = Object.new # simulate the first request
148
148
  @controller_instance.instance_eval do
@@ -160,4 +160,30 @@ class CondiTest < Test::Unit::TestCase
160
160
  end
161
161
  end
162
162
 
163
- end
163
+
164
+ # test for https://github.com/coldnebo/condi/issues/2
165
+ def test_returns
166
+ @controller_instance.instance_eval do
167
+ def my_action
168
+ first_path = true
169
+ predicate(:can_return?) do
170
+ if first_path
171
+ return true
172
+ else
173
+ return false
174
+ end
175
+ end
176
+ end
177
+ end
178
+ @controller_instance.my_action
179
+
180
+ # don't raise a LocalJumpError: unexpected return!
181
+ assert_nothing_raised do
182
+ @controller_instance.can_return?
183
+ end
184
+
185
+ # also ensure the correct value was returned.
186
+ assert @controller_instance.can_return? == true
187
+ end
188
+
189
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: condi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -36,7 +36,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
36
36
  version: '0'
37
37
  segments:
38
38
  - 0
39
- hash: -2574211362377765017
39
+ hash: 737326911507565152
40
40
  required_rubygems_version: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements: