basic_assumption 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc
CHANGED
@@ -13,7 +13,7 @@ It represents an idiom for writing certain kinds of code in a declarative way.
|
|
13
13
|
Particularly in your Rails apps, it's worth checking out. BasicAssumption is
|
14
14
|
another implementation of the idiom. It exists largely because I felt like
|
15
15
|
practicing writing a gem, though it does have a differentiating feature or two.
|
16
|
-
Check the
|
16
|
+
Check the +Configuration+ options for enabling compatibility with the
|
17
17
|
DecentExposure API.
|
18
18
|
|
19
19
|
== Install BasicAssumption
|
@@ -34,18 +34,18 @@ For Rails 3, in your Gemfile:
|
|
34
34
|
|
35
35
|
It's possible to use BasicAssumption in your Rails 3 app without requiring
|
36
36
|
'basic_assumption/rails', and will in fact allow more flexibility if you don't,
|
37
|
-
but will
|
37
|
+
but will incur the cost of additional setup. See the file
|
38
38
|
'lib/basic_assumption/rails.rb' for guidance.
|
39
39
|
|
40
40
|
To use the library in another context, it is enough to extend the
|
41
|
-
BasicAssumption module
|
41
|
+
BasicAssumption module inside the class you would like it available.
|
42
42
|
|
43
43
|
== Examples
|
44
44
|
|
45
45
|
=== Inside a Rails controller
|
46
46
|
|
47
47
|
The presumed most-common use case for BasicAssumption is in a Rails app. By
|
48
|
-
default, BasicAssumption is extended
|
48
|
+
default, BasicAssumption is extended within ActionController::Base, making it
|
49
49
|
available inside your controllers.
|
50
50
|
|
51
51
|
The most important (of the few) methods made available in controller classes is
|
@@ -98,15 +98,48 @@ created by +assume+ whenever a block is not passed. Here is a simple example:
|
|
98
98
|
...
|
99
99
|
end
|
100
100
|
|
101
|
-
This will provide the same behavior as the similar example above.
|
102
|
-
|
103
|
-
a discussion.
|
101
|
+
This will provide the same behavior as the similar example above. It is also
|
102
|
+
possible to pass a proc or lambda directly.
|
104
103
|
|
105
|
-
|
104
|
+
Finally, a symbol may be passed if it corresponds to a
|
105
|
+
specifically-defined helper class that came packaged with the library or was
|
106
|
+
provided by the application.
|
107
|
+
|
108
|
+
=== In Rails
|
109
|
+
|
110
|
+
In Rails, a useful default is already active out of the box. It attempts to
|
111
|
+
guess the name of a class derived from ActiveRecord::Base and do a find on it
|
112
|
+
based on an id available in the +params+ of the request. Because of this, the
|
113
|
+
following two constructs would be equivalent in your controllers:
|
114
|
+
|
115
|
+
assume(:film) { Film.find(params[:film_id] || params[:id]) }
|
116
|
+
# The above line is exactly the same as:
|
117
|
+
assume(:film)
|
118
|
+
|
119
|
+
Please see +SimpleRails+ for details of implementation. Because finding on :id
|
120
|
+
could be considered dangerous, there is another default available for use,
|
121
|
+
+CautiousRails+, that will only find on :name_id. (In the example above,
|
122
|
+
this would be :film_id.) Enable that for one of your controllers like so:
|
123
|
+
|
124
|
+
class FilmController < ActionController::Base
|
125
|
+
default_assumption :cautious_rails
|
126
|
+
end
|
127
|
+
|
128
|
+
Default assumptions are inherited by derived classes.
|
129
|
+
|
130
|
+
=== Supplying custom default behavior classes
|
106
131
|
|
107
132
|
There is an ability to provide custom, modular default extensions to
|
108
|
-
BasicAssumption
|
109
|
-
|
133
|
+
BasicAssumption and then use them by passing a symbol, like so:
|
134
|
+
|
135
|
+
class WidgetController < ActionController::Base
|
136
|
+
default_assumption :my_custom_default_class
|
137
|
+
end
|
138
|
+
|
139
|
+
The symbol is converted to a class in the same manner as Rails classify/
|
140
|
+
constantize operates, but it is looked up in the
|
141
|
+
BasicAssumption::DefaultAssumption namespace. Please see the documentation for
|
142
|
+
+DefaultAssumption+ for a discussion and +SimpleRails+ for an example of this.
|
110
143
|
|
111
144
|
=== Configuration
|
112
145
|
|
@@ -114,8 +147,6 @@ There are a couple of simple configuration settings that can be set inside of
|
|
114
147
|
a configuration block that can be used in places such as Rails initializer
|
115
148
|
blocks. For more information, see +Configuration+.
|
116
149
|
|
117
|
-
More are yet to come...
|
118
|
-
|
119
150
|
== Issues
|
120
151
|
|
121
152
|
=== Memoization
|
@@ -28,14 +28,10 @@ module BasicAssumption
|
|
28
28
|
# provides a +block+ instance method, and use the result as the default
|
29
29
|
# behavior. See the +SimpleRails+ class for an example.
|
30
30
|
module DefaultAssumption
|
31
|
-
# Stores the +default+ as the default behavior for class +klass+ that
|
32
|
-
# will be used by calls to +assume+.
|
33
31
|
def self.register(klass, default) #:nodoc:
|
34
32
|
registry[klass.object_id] = strategy(default)
|
35
33
|
end
|
36
34
|
|
37
|
-
# Returns the default behavior used by calls to +assume+ for the class
|
38
|
-
# +klass+.
|
39
35
|
def self.resolve(klass) #:nodoc:
|
40
36
|
while !registry.has_key?(klass.object_id)
|
41
37
|
klass = klass.superclass
|
@@ -44,7 +40,7 @@ module BasicAssumption
|
|
44
40
|
registry[klass.object_id]
|
45
41
|
end
|
46
42
|
|
47
|
-
class << self
|
43
|
+
class << self
|
48
44
|
attr_accessor :default #:nodoc:
|
49
45
|
|
50
46
|
protected
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module BasicAssumption
|
2
|
+
module DefaultAssumption
|
3
|
+
# Custom default behavior in the context of Rails.
|
4
|
+
class CautiousRails < BasicAssumption::DefaultAssumption::Base
|
5
|
+
# Returns a block that will attempt to find an instance of
|
6
|
+
# an ActiveRecord model based on the name that was given to
|
7
|
+
# BasicAssumption#assume and an id value in the parameters.
|
8
|
+
# It will not find based on params[:id], only params[:model_id].
|
9
|
+
# The following two examples would be equivalent:
|
10
|
+
#
|
11
|
+
# class WidgetController < ActionController::Base
|
12
|
+
# assume :widget
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# class WidgetController < ActionController::Base
|
16
|
+
# assume(:widget) { Widget.find(params[:widget_id]) }
|
17
|
+
# end
|
18
|
+
def block
|
19
|
+
Proc.new do |name|
|
20
|
+
model_class = name.to_s.classify.constantize
|
21
|
+
model_class.find(params["#{name}_id"])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'active_support'
|
3
|
+
require 'basic_assumption/default_assumption/cautious_rails'
|
4
|
+
|
5
|
+
describe BasicAssumption::DefaultAssumption::CautiousRails do
|
6
|
+
class Model; end
|
7
|
+
|
8
|
+
context "#block" do
|
9
|
+
let(:default) { BasicAssumption::DefaultAssumption::CautiousRails.new }
|
10
|
+
let(:params) { stub(:[] => 42) }
|
11
|
+
|
12
|
+
before(:each) do
|
13
|
+
Model.stub!(:find)
|
14
|
+
default.stub!(:params).and_return(params)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "looks for a params[model_id] in its calling context" do
|
18
|
+
params.should_receive(:[]).with('model_id').and_return(nil)
|
19
|
+
default.block.call(:model)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "does not look for params[id] in its calling context" do
|
23
|
+
params.should_receive(:[]).with('id').never
|
24
|
+
default.block.call(:model)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "attempts to find a model instance based off the given name" do
|
28
|
+
Model.should_receive(:find).with(42).and_return(:model)
|
29
|
+
default.block.call(:model).should eql(:model)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 3
|
9
|
+
version: 0.3.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Matt Yoho
|
@@ -73,6 +73,7 @@ files:
|
|
73
73
|
- lib/basic_assumption/configuration.rb
|
74
74
|
- lib/basic_assumption/default_assumption.rb
|
75
75
|
- lib/basic_assumption/default_assumption/base.rb
|
76
|
+
- lib/basic_assumption/default_assumption/cautious_rails.rb
|
76
77
|
- lib/basic_assumption/default_assumption/class_resolver.rb
|
77
78
|
- lib/basic_assumption/default_assumption/rails.rb
|
78
79
|
- lib/basic_assumption/default_assumption/simple_rails.rb
|
@@ -115,5 +116,6 @@ test_files:
|
|
115
116
|
- spec/lib/basic_assumption/configuration_spec.rb
|
116
117
|
- spec/lib/basic_assumption/default_assumption_spec.rb
|
117
118
|
- spec/lib/basic_assumption/default_assumption/base_spec.rb
|
119
|
+
- spec/lib/basic_assumption/default_assumption/cautious_rails_spec.rb
|
118
120
|
- spec/lib/basic_assumption/default_assumption/class_resolver_spec.rb
|
119
121
|
- spec/lib/basic_assumption/default_assumption/simple_rails_spec.rb
|