basic_assumption 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/HISTORY.rdoc +3 -0
- data/README.rdoc +2 -3
- data/lib/basic_assumption.rb +33 -18
- data/lib/basic_assumption/default_assumption.rb +1 -1
- data/lib/basic_assumption/default_assumption/action.rb +56 -0
- data/lib/basic_assumption/default_assumption/class_resolver.rb +19 -13
- data/lib/basic_assumption/default_assumption/name.rb +32 -0
- data/lib/basic_assumption/default_assumption/owner_attributes.rb +36 -0
- data/lib/basic_assumption/default_assumption/rails.rb +109 -25
- data/lib/basic_assumption/rails.rb +2 -3
- data/lib/basic_assumption/version.rb +1 -1
- data/spec/basic_assumption/default_assumption/class_resolver_spec.rb +46 -30
- data/spec/basic_assumption/default_assumption/rails_spec.rb +179 -22
- data/spec/basic_assumption_spec.rb +5 -3
- metadata +50 -41
- data/lib/basic_assumption/default_assumption/restful_rails.rb +0 -112
- data/spec/basic_assumption/default_assumption/restful_rails_spec.rb +0 -280
@@ -1,49 +1,65 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'basic_assumption/default_assumption/class_resolver'
|
3
3
|
|
4
|
-
class ::ExampleClass
|
4
|
+
class BasicAssumption::DefaultAssumption::ExampleClass
|
5
5
|
attr_reader :foo, :bar
|
6
|
+
|
6
7
|
def initialize(foo=nil, bar=nil)
|
7
8
|
@foo = foo; @bar = bar
|
8
9
|
end
|
9
10
|
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
context '#camelize, given a symbol or string' do
|
14
|
-
it 'returns an upper camel-cased string' do
|
15
|
-
resolver.camelize('camel').should eql('Camel')
|
16
|
-
end
|
17
|
-
it 'treats underscores as word boundaries and removes them' do
|
18
|
-
resolver.camelize('camel_case_word').should eql('CamelCaseWord')
|
19
|
-
end
|
12
|
+
module Foo
|
13
|
+
class ExampleClass
|
20
14
|
end
|
21
|
-
|
22
|
-
|
23
|
-
|
15
|
+
end
|
16
|
+
|
17
|
+
class TopLevelExampleClass
|
18
|
+
end
|
19
|
+
|
20
|
+
describe BasicAssumption::DefaultAssumption::ClassResolver do
|
21
|
+
|
22
|
+
context '#klass' do
|
23
|
+
it 'returns a class from the BasicAssumption::DefaultAssumption namespace' do
|
24
|
+
resolver = BasicAssumption::DefaultAssumption::ClassResolver.new(:example_class, 'BasicAssumption::DefaultAssumption')
|
25
|
+
resolver.klass.should equal(BasicAssumption::DefaultAssumption::ExampleClass)
|
24
26
|
end
|
25
|
-
|
26
|
-
|
27
|
+
|
28
|
+
context "specifying a namespace" do
|
29
|
+
it 'returns a class from that namespace' do
|
30
|
+
resolver = BasicAssumption::DefaultAssumption::ClassResolver.new(:example_class, 'Foo')
|
31
|
+
resolver.klass.should equal(Foo::ExampleClass)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'returns a class from the top-level namespace by default' do
|
35
|
+
resolver = BasicAssumption::DefaultAssumption::ClassResolver.new(:top_level_example_class)
|
36
|
+
resolver.klass.should equal(TopLevelExampleClass)
|
37
|
+
end
|
27
38
|
end
|
28
39
|
end
|
40
|
+
|
29
41
|
context '#instance' do
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
it 'returns an instance of the resolved class' do
|
35
|
-
@resolver.instance.should be_a_kind_of(ExampleClass)
|
42
|
+
|
43
|
+
it 'returns a class instance from the BasicAssumption::DefaultAssumption namespace' do
|
44
|
+
resolver = BasicAssumption::DefaultAssumption::ClassResolver.new(:example_class, 'BasicAssumption::DefaultAssumption')
|
45
|
+
resolver.instance.should be_a_kind_of(BasicAssumption::DefaultAssumption::ExampleClass)
|
36
46
|
end
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
47
|
+
|
48
|
+
it 'raises an error if no such constant is found' do
|
49
|
+
resolver = BasicAssumption::DefaultAssumption::ClassResolver.new(:no_such_class)
|
50
|
+
expect { resolver.instance }.to raise_error(NameError)
|
41
51
|
end
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
52
|
+
|
53
|
+
context "specifying a namespace" do
|
54
|
+
it 'returns a class instance from that namespace' do
|
55
|
+
resolver = BasicAssumption::DefaultAssumption::ClassResolver.new(:example_class, 'Foo')
|
56
|
+
resolver.instance.should be_a_kind_of(Foo::ExampleClass)
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'returns a class instance from the top-level namespace' do
|
60
|
+
resolver = BasicAssumption::DefaultAssumption::ClassResolver.new(:top_level_example_class)
|
61
|
+
resolver.instance.should be_a_kind_of(TopLevelExampleClass)
|
62
|
+
end
|
47
63
|
end
|
48
64
|
end
|
49
65
|
end
|
@@ -2,45 +2,202 @@ require 'spec_helper'
|
|
2
2
|
require 'active_support'
|
3
3
|
require 'basic_assumption/default_assumption/rails'
|
4
4
|
|
5
|
-
class Model
|
5
|
+
class Model
|
6
|
+
attr_accessor :age, :color, :owner_id
|
7
|
+
def initialize(hash = {})
|
8
|
+
hash.each do |k, v|
|
9
|
+
self.send("#{k}=", v)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
6
13
|
|
7
14
|
describe BasicAssumption::DefaultAssumption::Rails do
|
8
15
|
|
16
|
+
let(:default) { BasicAssumption::DefaultAssumption::Rails.new(:model, {}, request) }
|
17
|
+
let(:request) { stub(:params => params, :get? => true) }
|
18
|
+
let(:params) { {'id' => 42} }
|
19
|
+
|
9
20
|
context "#block" do
|
10
|
-
let(:
|
11
|
-
let(:params) { stub(:[] => 42) }
|
21
|
+
let(:relation) { stub("where", :find => nil) }
|
12
22
|
|
13
23
|
before(:each) do
|
14
|
-
Model.stub!(:
|
15
|
-
default.stub!(:params).and_return(params)
|
24
|
+
Model.stub!(:where).and_return(relation)
|
16
25
|
end
|
17
26
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
27
|
+
it "attempts to find a model instance based off the given name" do
|
28
|
+
relation.should_receive(:find).with(42).and_return(:model)
|
29
|
+
BasicAssumption::DefaultAssumption::Rails.new(:model, {}, request).result.should eql(:model)
|
30
|
+
end
|
31
|
+
|
32
|
+
context "when passed an alternative model name" do
|
33
|
+
it "finds a model instance based off the alternative name" do
|
34
|
+
relation.should_receive(:find).with(42).and_return(:model)
|
35
|
+
BasicAssumption::DefaultAssumption::Rails.new(:my_model, {:as => :model}, request).result.should eql(:model)
|
23
36
|
end
|
24
37
|
end
|
25
38
|
|
26
|
-
context "when
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
39
|
+
context "when the name given to assume is plural" do
|
40
|
+
let(:name) { :models }
|
41
|
+
|
42
|
+
%w(create destroy edit index new show update).each do |action|
|
43
|
+
let(:params) { {'action' => action} }
|
44
|
+
|
45
|
+
context "when action is #{action}" do
|
46
|
+
it "finds all the records of the model class" do
|
47
|
+
Model.should_receive(:where)
|
48
|
+
default.block.call(name, {})
|
49
|
+
end
|
50
|
+
end
|
31
51
|
end
|
52
|
+
|
32
53
|
end
|
33
54
|
|
34
|
-
|
35
|
-
|
36
|
-
|
55
|
+
context "when the name given to assume is singular" do
|
56
|
+
let(:name) { :model }
|
57
|
+
|
58
|
+
context "in the index action" do
|
59
|
+
let(:params) { { 'action' => 'index' } }
|
60
|
+
|
61
|
+
before { params['model'] = {:initializer => 'value'} }
|
62
|
+
it "creates a new model instance and passes in appropriate params" do
|
63
|
+
Model.should_receive(:new).with({:initializer => 'value'}).and_return(name)
|
64
|
+
default.block.call(name, {}).should eql(name)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "in the show action" do
|
69
|
+
let(:params) { { 'id' => 42, 'action' => 'show' } }
|
70
|
+
|
71
|
+
it "attempts to find a model instance based off the given name" do
|
72
|
+
relation.should_receive(:find).with(42).and_return(name)
|
73
|
+
default.block.call(name, {}).should eql(:model)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "in the edit action" do
|
78
|
+
let(:params) { { 'id' => 42, 'action' => 'edit' } }
|
79
|
+
let(:model) { stub("model", :attributes= => {}) }
|
80
|
+
|
81
|
+
before { relation.should_receive(:find).with(42).and_return(model) }
|
82
|
+
|
83
|
+
it "attempts to find a model instance based off the given name" do
|
84
|
+
default.block.call(name, {}).should eql(model)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "sets the updated attributes" do
|
88
|
+
model.should_receive(:attributes=).once
|
89
|
+
default.block.call(name, {})
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context "in the update action" do
|
94
|
+
let(:params) { {'id' => 42, 'action' => 'update'} }
|
95
|
+
let(:model) { stub("model", :attributes= => {}) }
|
96
|
+
|
97
|
+
before { relation.should_receive(:find).with(42).and_return(model) }
|
98
|
+
|
99
|
+
it "attempts to find a model instance based off the given name" do
|
100
|
+
default.block.call(name, {}).should eql(model)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "sets the updated attributes" do
|
104
|
+
model.should_receive(:attributes=).once
|
105
|
+
default.block.call(name, {})
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context "in the destroy action" do
|
110
|
+
let(:params) { { 'id' => 42, 'action' => 'destroy' } }
|
111
|
+
|
112
|
+
it "attempts to find a model instance based off the given name" do
|
113
|
+
relation.should_receive(:find).with(42).and_return(name)
|
114
|
+
default.block.call(name, {}).should eql(:model)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
context "in the create action" do
|
119
|
+
let(:params) do
|
120
|
+
{
|
121
|
+
'action' => 'create',
|
122
|
+
'model' => { 'age' => 27, 'color' => 'blue' }
|
123
|
+
}
|
124
|
+
end
|
125
|
+
|
126
|
+
context "the model instance" do
|
127
|
+
subject { default.block.call(name, {}) }
|
128
|
+
its(:age) { should be(27) }
|
129
|
+
its(:color) { should eql('blue') }
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
context "in the new action" do
|
134
|
+
let(:params) do
|
135
|
+
{
|
136
|
+
'action' => 'new',
|
137
|
+
'model' => { 'age' => 27, 'color' => 'blue' }
|
138
|
+
}
|
139
|
+
end
|
140
|
+
|
141
|
+
context "the model instance" do
|
142
|
+
subject { default.block.call(name, {}) }
|
143
|
+
its(:age) { should be(27) }
|
144
|
+
its(:color) { should eql('blue') }
|
145
|
+
end
|
146
|
+
end
|
37
147
|
end
|
148
|
+
end
|
38
149
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
150
|
+
context "ensuring ownership" do
|
151
|
+
class User
|
152
|
+
def id
|
153
|
+
5678
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
class Controller
|
158
|
+
def owner
|
159
|
+
User.new
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
let(:params) { {'id' => 123} }
|
164
|
+
|
165
|
+
it "accepts a symbol" do
|
166
|
+
Model.should_receive(:where).with(:user_id => 5678).and_return(stub(:find => nil))
|
167
|
+
|
168
|
+
default = BasicAssumption::DefaultAssumption::Rails.new(:model, {:owner => :owner, :controller => Controller.new}, request)
|
169
|
+
|
170
|
+
default.result
|
171
|
+
end
|
172
|
+
|
173
|
+
it "accepts a proc" do
|
174
|
+
Model.should_receive(:where).with(:user_id => 5678).and_return(stub(:find => nil))
|
175
|
+
|
176
|
+
default = BasicAssumption::DefaultAssumption::Rails.new(:model, {:owner => proc { owner }, :controller => Controller.new}, request)
|
177
|
+
|
178
|
+
default.result
|
179
|
+
end
|
180
|
+
|
181
|
+
context "with a specified column name" do
|
182
|
+
|
183
|
+
it "obeys the column name" do
|
184
|
+
Model.should_receive(:where).with(:user_id => 5678).and_return(stub(:find => nil))
|
185
|
+
|
186
|
+
owner_context = {:column_name => :user_id, :object => proc { owner }}
|
187
|
+
default = BasicAssumption::DefaultAssumption::Rails.new(:model, {:owner => owner_context, :controller => Controller.new}, request)
|
188
|
+
|
189
|
+
default.result
|
190
|
+
end
|
191
|
+
|
192
|
+
it "obeys the column name given a proc" do
|
193
|
+
Model.should_receive(:where).with(:owner_id => 5678).and_return(stub(:find => nil))
|
194
|
+
|
195
|
+
owner_context = {:column_name => :owner_id, :object => :owner}
|
196
|
+
default = BasicAssumption::DefaultAssumption::Rails.new(:model, {:owner => owner_context, :controller => Controller.new}, request)
|
197
|
+
|
198
|
+
default.result
|
43
199
|
end
|
44
200
|
end
|
45
201
|
end
|
202
|
+
|
46
203
|
end
|
@@ -185,9 +185,11 @@ describe BasicAssumption do
|
|
185
185
|
controller_class.class_eval do
|
186
186
|
assume(:model)
|
187
187
|
end
|
188
|
-
controller_instance.stub(:params => {'
|
189
|
-
|
190
|
-
|
188
|
+
controller_instance.stub(:request => stub(:params => {'id' => 123}, :get? => true))
|
189
|
+
relation = stub("where")
|
190
|
+
relation.should_receive(:find).with(123)
|
191
|
+
::Model.stub(:where => relation)
|
192
|
+
controller_instance.model
|
191
193
|
end
|
192
194
|
end
|
193
195
|
end
|
metadata
CHANGED
@@ -1,30 +1,40 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: basic_assumption
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
version: 0.5.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.2
|
6
5
|
platform: ruby
|
7
|
-
authors:
|
6
|
+
authors:
|
8
7
|
- Matt Yoho
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
11
|
+
date: 2015-08-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.0
|
27
|
+
description: "\n Allows a simple declarative idiom for accessing resources in controllers
|
28
|
+
and views\n via a well-defined interface that increases testability and reduces
|
29
|
+
shared state.\n "
|
18
30
|
email: mby@mattyoho.com
|
19
31
|
executables: []
|
20
|
-
|
21
32
|
extensions: []
|
22
|
-
|
23
|
-
extra_rdoc_files:
|
33
|
+
extra_rdoc_files:
|
24
34
|
- HISTORY.rdoc
|
25
35
|
- MIT-LICENSE
|
26
36
|
- README.rdoc
|
27
|
-
files:
|
37
|
+
files:
|
28
38
|
- HISTORY.rdoc
|
29
39
|
- MIT-LICENSE
|
30
40
|
- README.rdoc
|
@@ -32,51 +42,51 @@ files:
|
|
32
42
|
- lib/basic_assumption/configuration.rb
|
33
43
|
- lib/basic_assumption/configuration/active_record.rb
|
34
44
|
- lib/basic_assumption/default_assumption.rb
|
45
|
+
- lib/basic_assumption/default_assumption/action.rb
|
35
46
|
- lib/basic_assumption/default_assumption/base.rb
|
36
47
|
- lib/basic_assumption/default_assumption/class_resolver.rb
|
48
|
+
- lib/basic_assumption/default_assumption/name.rb
|
49
|
+
- lib/basic_assumption/default_assumption/owner_attributes.rb
|
37
50
|
- lib/basic_assumption/default_assumption/rails.rb
|
38
|
-
- lib/basic_assumption/default_assumption/restful_rails.rb
|
39
51
|
- lib/basic_assumption/rails.rb
|
40
52
|
- lib/basic_assumption/rspec.rb
|
41
53
|
- lib/basic_assumption/version.rb
|
42
54
|
- rails/init.rb
|
43
|
-
- spec/spec_helper.rb
|
44
|
-
- spec/basic_assumption_spec.rb
|
45
55
|
- spec/basic_assumption/configuration_spec.rb
|
46
|
-
- spec/basic_assumption/default_assumption_spec.rb
|
47
56
|
- spec/basic_assumption/default_assumption/base_spec.rb
|
48
57
|
- spec/basic_assumption/default_assumption/class_resolver_spec.rb
|
49
58
|
- spec/basic_assumption/default_assumption/rails_spec.rb
|
50
|
-
- spec/basic_assumption/
|
51
|
-
|
59
|
+
- spec/basic_assumption/default_assumption_spec.rb
|
60
|
+
- spec/basic_assumption_spec.rb
|
61
|
+
- spec/spec_helper.rb
|
52
62
|
homepage: http://github.com/mattyoho/basic_assumption
|
53
63
|
licenses: []
|
54
|
-
|
64
|
+
metadata: {}
|
55
65
|
post_install_message:
|
56
|
-
rdoc_options:
|
57
|
-
- --charset=UTF-8
|
58
|
-
require_paths:
|
66
|
+
rdoc_options:
|
67
|
+
- "--charset=UTF-8"
|
68
|
+
require_paths:
|
59
69
|
- lib
|
60
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
-
|
62
|
-
requirements:
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
63
72
|
- - ">="
|
64
|
-
- !ruby/object:Gem::Version
|
65
|
-
version:
|
66
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
-
|
68
|
-
requirements:
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
69
77
|
- - ">="
|
70
|
-
- !ruby/object:Gem::Version
|
71
|
-
version:
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
72
80
|
requirements: []
|
73
|
-
|
74
81
|
rubyforge_project:
|
75
|
-
rubygems_version:
|
82
|
+
rubygems_version: 2.2.3
|
76
83
|
signing_key:
|
77
|
-
specification_version:
|
78
|
-
summary: Allows a simple declarative idiom for accessing resources in controllers
|
79
|
-
|
84
|
+
specification_version: 4
|
85
|
+
summary: Allows a simple declarative idiom for accessing resources in controllers
|
86
|
+
and views, cleaning up controller code and removing the need to explicitly reference
|
87
|
+
instance variables inside views. Custom default behavior can be defined in a pluggable
|
88
|
+
manner.
|
89
|
+
test_files:
|
80
90
|
- spec/spec_helper.rb
|
81
91
|
- spec/basic_assumption_spec.rb
|
82
92
|
- spec/basic_assumption/configuration_spec.rb
|
@@ -84,4 +94,3 @@ test_files:
|
|
84
94
|
- spec/basic_assumption/default_assumption/base_spec.rb
|
85
95
|
- spec/basic_assumption/default_assumption/class_resolver_spec.rb
|
86
96
|
- spec/basic_assumption/default_assumption/rails_spec.rb
|
87
|
-
- spec/basic_assumption/default_assumption/restful_rails_spec.rb
|